C-Decision Making Statements | with Examples



Decision making statements are needed to alter the sequence of the statements in the program depending upon certain circumstances. Decision can be made on the basis of success or failure of some logical conditions. They allow us to control the flow of our program. These conditions can be placed in the program using decision making statements. C language supports the following decision making/control statements.
  • The if statement. 
  • The if-else statement.
  • The if-else-if ladder statement.
  • The switch-case statement.

Decision making statements check the given condition and then executes its sub block if the condition happens to be true. (A block is a set of statements enclosed within opening brace and closing brace{and}).

THE IF STATEMENT

The general syntax of if statement is as:

 If(condition)
 {
     Statements;
     Statements;
     Statements;
     ………………..;
  }

The if statement is used to execute/skip a block of statements on the basis of truth or falsity of a condition. The condition to be checked is put inside the parenthesis which is preceded by keyword if.

Demo of if statement */
#include<stdio.h>
#include<conio.h>
main()
{
 int x;
 clrscr ();
 x=10;
 if (x>0)
     printf(“x is greater than 0”);
 getch();
}
Output:
      x is greater than 0

Explanation: if is a conditional execution statement. If is a keyword. The condition to be checked is put inside parenthesis. If the condition is true the first statement after the if gets executed, else it is skipped.

THE IF-ELSE STATEMENT

In all the above programs we didn’t write the other side of if condition i.e. we didn’t take the action when the condition fails. The if-else construct allows us to do this.

Its general syntax is

If(condition)
{
  statements;
  statements;
  statements;
  ………………
}
else
{
  statements;
  statements;
  statements;
  ……………….;
 }

If the condition within if true all the statements within the block following if are executed else they are skipped and else part get executed.

To check whether a number is +ve, -ve or zero */

#include<stdio.h>
#include<conio.h>
main()
{
 int a;
 clrscr ();
 printf (“Enter any number\n”);
 scanf(“%d”,&a);
 if(a==0)
   printf(“number is zero\n”);
       if(a>0)
         printf(“number is +ve\n”);
       else
         printf(“number is –ve\n”);
       getch();
Output:
Enter any number
 -34                   
 number is –ve

There is one small change in the program from the previous one. We have added a simple if condition which check whether number is zero or not.

The above program works fine but is less efficient. If the input happens to be zero then we need not check the later if and one else part we want only one part should get executed. 

There are few solutions to make the above program more efficient.

 NESTING OF IF-ELSE’S

Nesting of if-else means one if-else or simple if as the statement part of another if-else or simple if statement. There may be various syntaxes of nesting of if-else.

1. 
If(condition)
{
   If(condition)
  {
    Statements;
    Statements;
    Statements;
   }
else
     {
        Statements;
        Statements;
        Statements;
      }
}
2.
If(condition)
{
  If(condition)
      {
         Statements;
         Statements;
         Statements;
       }
   else
     {
         Statements;
         Statements;
         Statements;
     }
  }
else
{
    Statements;
    Statements;
    Statements;
}


3.

If(condition)
{
    If(condition)
    {
      Statements;
      Statements;
      Statements;
    }
    else
    {
       Statements;
       Statements;
       Statements;
     }
 }
else
{
  If(condition)
   {
     Statements;
     Statements;
     Statements;
    }
    else
    {
       Statements;
       Statements;
       Statements;
     }
} 
To check whether a year is leap or not     ver   1*/
#include<stdio.h>
#include<conio.h>
main()
{
  int year;
  clrscr();
  printf(“Enter any year\n”);
  scanf(“%d”,&year);
  if(year %100    ==0)
  {
     if (year %400   ==0 )
           printf(“The given year is leap year\n”);
     else
           printf(“The given year is not a leap year\n”);
    }
   else
   {
      if (year %4    ==0)
           printf(“The given year is leap year\n”)
      else
           printf(“The given year is not a leap year\n”);
     }
     getch();
}
Output:
            (first run)
              Enter any year
                2000
              The given year is leap year
            (second run)
               Enter any year
                 2005
               The given year is not a leap year

Explanation: A year is leap year if it is completely divisible by 100 and 400 or not divisible by 100 but divisible by 4.

Else-if ladder

The general syntax of else-if ladder is

      If(condition)
          Statement;
      else-if(condition)
          Statement;
      else-if(condition)
           Statement;
      else-if(condition)
           Statement;
If the first if condition is satisfied, then all its related statements are executed and all other else-if’s are skipped. The control reaches to first else-if only if the first if fails. Same for second, third and other else-if’s depending upon what your program required. That is out of this else-if ladder only one if condition will be satisfied.

Arrange three numbers in ascending order ver  1*/
#include<stdio.h>
#include<conio.h>
main()
{
  int a,b ,c,min,max,mid;
  clrscr();
  printf(“Enter the three numbers\n”);
  scanf(“%d%d%d”,&a,&b,&c);

  if (a>b   &&   b>c)
       max=a;
  else if (b>a   &&   b>c);
       max=c;
     
   if (a<b   &&   a<c)
       min=a;
   else if (b<a   &&   b<c)
       min=b;
    else if (c<a   &&   c<b)
        min=c;
     mid=(a+b+c)-(min+max);
     printf(“Ascending order is %d   %d    %d\n”,min,mid,max);
     getch();
}
Output:
               Enter the three numbers
                34       56       22
               Ascending order is 22     34       56
Explanation: In the variable max we have stored the maximum among three and in the variable  min  we have stored the minimum among three. The mid is calculated by subtracting (min+max) from the sum of a, b and c i.e. (a+b+c).

This program can also be made using conditional operator and is quite easy in writing as well as requires less amount of code.

SWITCH-CASE

Switch-case can be used to replace else-if ladder construct.

Switch(expression)
{
 Case choice1:
      Statements;
      break;
 Case choice2:
      Statements;
      break;
 Case choice3:
      statements;
      break;
      -------------------
      -------------------
      -------------------
      default:
}
The expression may be any integer or char type which yields only char or integer as result.

Choice1,choice2 and choice are the possible values which we are going to test with the expressions. In case none of the values from choice1 to choice matches with the value of expression the default  case is executed.


Comments