Decision control statement

Decision control statements are used when we have choice in our program. These segments are used when we need to alter the flow of a sequence of instructions. 
These statements help to jump from one section of a program to another section of the program.(when the condition gets wrong).

Types of decision control statements:-

 These are the following types of decision control statements -
  1. Simple if 
  2. If else 
  3. Else if ladder 
  4. Nested concept of if statement i.e. nested if 
  5. Switch case :-- with uses of break and continue statement 

IF STATEMENT

It is the simplest form of decision control statements and it is also used in most often case .. In programming term if statement describe as if the particular condition executes then only a particular statement works otherwise it will move to the next term after this if statement. 

SYNTEX OF IF STATEMENT :



  1. If(condition)
  2. {
    Statement 1.. 
    Statement 2. 
    Statement 3.. 
    And so on many more conditions….
    }
    Statement Y;



    EXPLANATION OF THIS SYNTEX OF STATEMENT


    This Syntex says that if this particular condition to which we have applied this if statement is true then only all those statements written inside this if statement will get executed . 
    Otherwise compiler will move to the next term after this if statement .

    Else if ladder

     C language supports multiple else-if statement to text additional apart from the initial text expression. The else if ladder construct works in the same way as a normal if statement

    SYNTEX OF ELSE IF LADDER If 



    1. ( text expression )
    2. statement 1; 
      Statement 2; 
      else if ( test statement 2 )
      {
       statement block N ;
      else if ( test expression 3) 
      {
       statement block M ; 
      }
       ……..and so on else 
      { statement block Z } 




    NESTED IF STATEMENT

    If statement within the block of another if statement is called nested if statement,

    SYNTEX OF NESTED IF STATEMENT


    1. If( text expression ) 
       {
      statement 1 ;
       If {test expression 2 ) 
      printf( “ this is the best site : “CODEMOB” ); 
        }
      }
      Statement Y ; 

      SWITCH STATEMENT 

      A switch case statement is a multi way statement is a way decision statement. Switch statement are used :
      1. When there is only one variable to evaluate in the expression 
      2. When one possibility is to be selected on the basis of input. Switch case statement advantage include 
      3. Easy to debug , read , understand and maintain 
      4. Execute faster than its equivalent if-else construct. 

      Example of switch case is

       

      /*C program to design calculator with basic operations using switch.*/

       

      #include <stdio.h> 

      int main()

      {

          int num1,num2;

          float result;

          char ch;    //to store operator choice

          

          printf("Enter first number: ");

          scanf("%d",&num1);

          printf("Enter second number: ");

          scanf("%d",&num2);

          

          printf("Choose operation to perform (+,-,*,/,%): ");

          scanf(" %c",&ch);

          

          result=0;

          switch(ch)   

          {

            case '+':

              result=num1+num2;

                  break;

                  

            case '-':

              result=num1-num2;

                  break;

              

            case '*':

              result=num1*num2;

                  break;

                  

        case '/':

      result=(float)num1/(float)num2;

                  break;

              

            case '%':

              result=num1%num2;

                  break;

              default:

                  printf("Invalid operation.\n");

          }

          printf("Result:%d %c %d=%f\n",num1,ch,num2,result);

          return 0;

      }

         Output-
      First run:
          Enter first number: 10
          Enter second number: 20 
          Choose operation to perform (+,-,*,/,%): +
          Result: 10 + 20 = 30.000000 
      
          Second run:
          Enter first number: 10
          Enter second number: 3
          Choose operation to perform (+,-,*,/,%): /
          Result: 10 / 3 = 3.333333 
      
          Third run:
          Enter first number: 10
          Enter second number: 3
          Choose operation to perform (+,-,*,/,%): >
          Invalid operation.
          Result: 10 > 3 = 0.000000