C Decision Making Statements

The C language provides three types of decision-making constructs: if-else, the conditional expression ?:, and the switch statement.

Different structures are used while making decision in C programming whether to execute the block of code or not. The curly braces { and } are used for creating the block and statements are executed line by line. 

The following are the structures used while making a decision:

1. if statement

Both an if and an if-else are available in C. The <expression> can be any valid
expression. The parentheses around the expression are required, even if it is just a single
variable.

The following block of code is syntax for using if or if-else block:

if (<expression>) <statement> // simple form with no {}'s or else clause
if (<expression>) { // simple form with {}'s to group statements
    <statement>
    <statement>
}

For example, the assignment of value y to z takes place if x is less than y.

if (x < y)
    z = y;

Let us look at the more practical example. In following code, "You passed the test." will be printed if score is greater than 60.

if( score > 60 ) 
    printf(" You passed the test.");

if-else statement

The general form of if-else statement is:

if (<expression>) { // full if/else form
    <statement>
}
else {
    <statement>
}

The example from above can be extended as:

if( score > 60 )
    printf(" You passed the test.");
else 
    printf("Sorry, try again.");

So, the ouput will be "Sorry, try again." if score is less or equal to 60, else "You passed the test.".

if-else-if ladder

The general form of if-else-if ladder is as follows:

if(condition) 
    statement; 
else if(condition) 
    statement; 
else if(condition) 
    statement; 
...
else
    statement

One of statement gets executed when condition is true, else statement in else block will be executed.

if( score > 80 ){
  printf(" You passed the test with grade A.");
}
else if ( score > 70 && score <=80) {
  printf(" You passed the test with grade B.");
} 
else if( score > 60 && score <=70) {
  printf(" You passed the test with grade C.");
} 
else ( score > 50 && score <=60) {
  printf("Sorry, try again.");
}

For

int score = 72;

The output will be

You passed the with grade B.

And for 

int score = 32;

The ouput will be

Sorry, try again.

2. Conditional Expression -or- The Ternary Operator ( ?:)

The conditional expression can be used as a shorthand for some if-else statements. The general syntax of the conditional operator is:

<expression1> ? <expression2> : <expression3>


This is an expression, not a statement, so it represents a value. The operator works by evaluating expression1. If it is true (non-zero), it evaluates and returns expression2 .
Otherwise, it evaluates and returns expression3. The classic example of the ternary operator is to return the smaller of two variables.
Every once in a while, the following form is just what you needed. Instead of…
 

if (x < y) {
    min = x;
}
else {
    min = y;
}

You can use ternary operator to make shorthand of this as:

min = (x < y) ? x : y;

For example, the ouput of the followint two form would be the same.

//if-else statement
#include <stdio.h>
int main ()
{
    int num1 = 4, num2 = 9;
    int max;

    if ( num1 > num2){
        max = num1;
    }else{
        max = num2;
    }
    printf("Max: %d",max);
}

Output is:

Max : 9

Using ternary operator,

//if-else statement
#include <stdio.h>
int main ()
{
    int num1 = 4, num2 = 9;
    int max;

    max = (num1 > num2 )? num1 : num2;
    printf("Max: %d",max);
}

3. switch statement

It is just another form of if-else statement. The switch statement efficiently separates different blocks of code based on the value of an integer. The switch expression is evaluated, and then the flow of control jumps to the matching const-expression case. The case expressions are typically int or char constants. 

The following is the syntax to use the switch statement:

switch (<expression>) {
    case <const-expression-1>:
        <statement>
        break;
    case <const-expression-2>:
        <statement>
        break;
    case <const-expression-3>: // here we combine case 3 and 4
    case <const-expression-4>:
       <statement>
       break;
    default: // optional
        <statement>
}

The following example helps to understand more about switch statement:

int main()
{
    float temp_in_celsius = 34;
    char CHOICE ='F';
    float temp_converted;
    switch (CHOICE)
    {
    case 'F':
        temp_converted = (temp_in_celsius * 9/5)+ 32 ;
        break;
    case 'K':
        temp_converted = temp_in_celsius + 273;
        break;
    default:
        temp_converted = temp_in_celsius;
    }
    printf("%.3f Celsius is equal to %.3f F ",temp_in_celsius,temp_converted);
}

The output of the program is:

34 Celsius is equal to 93.2 F.

Here, expression CHOICE in switch statement matches to case with value F, so temp_converted is assigned value according to that block.

Note: three important features of the switch statement to be noted:

  1. The switch differs from the if in that switch can only test for equality, whereas if can evaluate any type of Boolean expression. That is, the switch looks only for a match between the value of the expression and one of its case constants.
  2. No two case constants in the same switch can have identical values. Of course, a switch statement and an enclosing outer switch can have case constants in common.
  3. A switch statement is usually more efficient than a set of nested ifs.
C Variables and Expression
C Iteration Statements