Decision making and branching in cprogramming

Decision making and branching...


  • In programming the order of execution of instructions may have to be changed depending on certain conditions.
  • This involves a kind of decision making to see whether a particular condition has occurred or not and then direct the computer to execute certain instructions accordingly.


1. if statement:



  • if statement is a powerful decision making statement and is used to control the flow of execution of statements.

             syntax is

             if(expression)


  • The expression is evaluated and depending on whether the value of the expression is true mean non zero or false mean zero it transfers the control to a particular statement.


simple if statement is:


              if (test expression)
                {
             statement block;
                  }
             statement n;

     ex.

 if... else statement


            Syntax is:

                if (expression)
                     {
              statement block;
                      }
              else
                    {
             statement block;
                       }
             statement n;



Nesting of if....else statement:



  • When a series of conditions are to be checked, we may have to use more than one if... else statement in the nested form.


           if (condition 1)
             {
          if (condition 2)
            {
         statement block 1;
             }
         else
            {
          statement block 2;
          } statement m;
           }
               else
          {
        if (condition 3)
            {
           statement block 3;
            }
           else
            {
          statement block 4
           } statement n;
             }
       statement x;

2.  switch statement:



  • When many conditions are to be checked then using nested if...else is very difficult, confusing and cumbersome.
  •  C has another useful built in decision making statement known as switch. This statement can be used as multiway decision statement.
  • The switch statement tests the value of a given variable or expression against a list of case values and when a match is found, a block of statements associated with that case is executed.


             Syntax:

              switch (expression)
                {
             case value 1 :
             statement block 1;
             break;
             case value 2:
              statement block 2;
              break;
              :
              :
              default:
              default block;
                }

3.  Conditional Operators or Ternary Operator



  • They are also called as Ternary Operator .
  • They also called as ?: operator
  • Ternary Operators takes on 3 Argument

          Syntax :

             expression 1 ? expression 2 : expression 3
       where

                 expression1 is Condition
                 expression2 is Statement Followed if Condition is True
                 expression3 is Statement Followed if Condition is False
            ex.

4.  goto statement:



  • This statement is used to branch unconditionally from one point to another in the program.
  • This statement goto requires a label to locate the place where the branch is to be made.
  • A label is any valid identifier and must be followed by a colon. 
  • The label is placed immediately before the statement where the control is to be transferred.


Different ways of using goto statement :

Syntax:

goto label;
(1)Forward jump: In this the position of the label is after the goto statement.
statement;

(2) Backward jump: In this, the position of the label is before the goto statement
label:
statement n;
      goto label;

Example:

1 comment: