Responsive Ads Here

Decision Making in C

Decision making is about deciding the order of execution of statements based on certain conditions. In decision control statements (if-else and nested if), group of statements are executed when condition is true.  If condition is false, then else part statements are executed.

C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.

C language handles decision-making by supporting the following statements :

  • if statement
  • switch statement
  • conditional operator statement
  • goto statement

Decision Making with if statement :

The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are :


  • Simple if statement
  • If..else statement
  • Nested if statement
  • if-else-if ladder 

Simple if Statement :

An if statement consists of a Boolean expression followed by one or more statements.

Flowchart :
if-statement-in-java

Syntax :
       if (condition) 
       { 
           Statements; 
        }

The expression given inside the brackets after if is evaluated first. If the expression is true, then statements inside the curly braces that follow if(expression) will be executed. If the expression is false, the statements inside curly braces will not be executed and program control goes directly to statements after curly braces.

Example :

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

   /* local variable definition */
    int a = 10;
   /* check the boolean condition using if statement */
  if( a < 20 ) {

      /* if condition is true then print the following */
      printf("a is less than 20\n" );
   }
   
   printf("value of a is : %d\n", a);
   return 0;
}

Output :


a is less than 20 
value of a is : 10 


if..else Statement :

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Flowchart :

if-else-statement

Syntax :
      if( expression )
      {
          statement block1;
       }
       else
      {
          statement block2;
       }

If the 'expression' is true, the 'statement-block1' is executed, else 'statement-block1' is skipped and 'statement-block2' is executed.

Example :

#include <stdio.h>
int main( )
{
int x,y;
x=15;
y=18;
if (x > y ){
printf("x is greater than y");
}
else{
printf("y is greater than x");
}
        return 0;
}

Output :

y is greater than x


Nested if Statements :


In “nested if” control statement, if condition 1 is false, then condition 2 is checked and statements are executed if it is true. 
If condition 2 also gets failure, then else part is executed.



Flowchart :



nested-if

Syntax :
       if( expression )
       {
           if( expression1 )
           {
                statement block1;
            }
            else
            {
                 statement block2;
             }
        }
        else
       {
             statement block3;

        }

if 'expression' is false the 'statement-block3' will be executed, otherwise it continues to perform the test for 'expression 1' . If the 'expression 1' is true the 'statement-block1' is executed otherwise 'statement-block2' is executed.
Example :