1.7. Decision Making#

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Any non-zero and non-null values are assumed as true, and zero or null values are assumed as false values. C programming language provides the following tow main types of decision making statements.

  • if-else statement

  • switch statement

1.7.1. if-else statement#

The if-else statement allows the code inside if block to be executed if the given condition is true, otherwise the code inside else block is executed.

Following is the syntax of an if-else statement,

if(condition)
{
    // execute statement of if block when
    // condition is true
}
else
{
    // execute statement of else block when
    // condition is false
}

Following is an example that uses if-else statement to decide if a variable holds value smaller than 20 or not.

#include <stdio.h>

int main()
{
    int a = 10;
    if (a < 20)
    {
        printf("Given value is less than 20.\n");
    }
    else
    {
        printf("Given value is greater than 20.\n");
    }
    return 0;
}

1.7.2. The ? : operators#

Operators ? and : are known as conditional operators which can be used to replace if-else statements. It has the following general form

Exp1 ? Exp2 : Exp3;

Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon. The result of line above is computed by evaluating Exp1. If it is true, then Exp2 is evaluated and its value becomes the result. However, if Exp1 is false, then Exp3 is evaluated and its value becomes the result.

Following is an example that uses ? : operators to decide if a variable holds value smaller than 20 or not.

#include <stdio.h>

int main()
{
    int a = 10;
    printf("Given value is %s than 20.\n", a < 20 ? "less" : "greater");
    return 0;
}

1.7.3. switch statement#

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. Following is the syntax for a switch statement,

switch(variable or expression) {
   case value1 :
      statement(s);
      break; /* optional */
   case value2 :
      statement(s);
      break; /* optional */
   /* you can have any number of case statements */
   default : /* Optional */
   statement(s);
}

break in the syntax above is a keyword that is used to stop the execution inside a switch block. It helps to terminate the switch block and break out of it. And, the default keyword is used to specify the set of statements to execute if there is no case match. Following code shows the usage of switch statement.

#include <stdio.h>
 
int main () {

   char grade = 'B';
   switch(grade) {
        case 'A' :
            printf("Excellent!\n" );
            break;
        case 'B' :
            printf("Well done!\n" );
            break;
        case 'C' :
            printf("Good!\n" );
            break;
        case 'D' :
            printf("You passed!\n" );
            break;
        case 'F' :
            printf("Better try again!\n" );
            break;
        default :
            printf("Invalid grade.\n" );
   }
   printf("Your grade is  %c\n", grade );
 
   return 0;
}