Decision making and branching(if-else statement) in C

In this section we are going to discuss the concepts of branching in C program.

Here we will cover:

  • if statement
  • if-else statement
  • switch statement
  • goto statement
  • conditional operator statement
Here in this post we are just going to show how "if statement, if else statements and if else ladder works
"
We will be dealing with switch, goto and conditional operator in our next post.

In a program, in certain conditions, we may have to change the order of execution or repeat a group of statement until the specified condition is satisfied or met.
C language supports this functionality by using the above mentioned statements.

Simple IF statement:


syntax:    if (test expression)
                          {
                              statements/codes;
                           }
                statements/codes;
The above shown syntax is very easy to understand and it is widely used in C program whenever it is needed.
Here if  the test expression is true than and then only the block of codes inside the curly braces just after if is executed other wise it is skipped.

If-statement flow chart
The above image shows the flow chart of a simple if - statement. It can be easily seen that if the condition, which is test expression, is true than the statement block inside the if - statement is executed and then the rest of program is executed below if statement. Otherwise, if the test expression is false than the program execution will skip the if statement blocks.




The IF-Else Statement

It is an extension of If statement where one of the two block of codes are executed compulsorily, unlike If statement where if the test condition comes to be false than the block of codes aren't executed and the program continuous with the rest of the line of codes.
syntax:    if (test expression)
                          {
                              statements/codes;
                           }
               else
                          {
                              statements/codes;
                           }
                statements/codes;

If- else statement flow chart
Here the above image shows the flow of program containing if-else statements. Here if the test expression in the if statement is true than the statement block inside the if statement are executed else the statement blocks inside the else condition are executed.

Nesting of If-Else statement

  • When there are some conditioned that are needed to be checked inside another condition than this kind of nesting If-Else statements are used.
  • for example : If we want to check weather the number is even or odd inside a condition where we first check weather it is positive or negative, then this kind of nesting is used.

Syntax:
      if (test - condition-1)
         {
            if (test condition-2)
                   {
                      statement -1;
                     }
             else
                    {
                      statement-2;
                     }
          }
     else
           {
              statement-3;
            }
           statement-x;

Flow chart:
Nested If-else statement
Here the above image shows the flow chart of Nested if- else statement of the syntax example given above that. It shows that if the first test condition-1 is true than the test condition-2 inside that if statement is tested and executed if true, else the else part, inside the first if part, is executed, and if the first test condition is false than the statements inside the first if part will be ignored and the statement -3 will be executed and then the program will continue with statement -x.


The Else-if Ladder

  • The else if ladder is the chain of If statements in which multiple conditions are checked of which only one condition must be true, else the else part will be executed.
  • For example: If we want to write a menu driven program and we want to select only one function,for example a simple calculator in which only one function i.e either addition subtraction, multiplication or division should be chosen by user to operate on two numbers than we can use this kind of statements.(The program of this question will be posted later)
  • well the above example can be easily solved using the Switch case statement (which will be discussed in later posts)
Syntax :
     if(condition 1)
          statement-1;
                   else if(condition -2)
                   statement -2;
                         else if(condition -2)
                         statement -2;
                              else if(condition -2)
                              statement -2;
                                  else if(condition -2)
                                  statement -2;
                                         else 
                                         default -statement;

  • The above ladder is known as else-if ladder.
  • Here the conditions are checked from first to last.
  • If one of the conditions are satisfied than the rest of the else -if statements and the last else statements are neglected and skipped.
  • In some situation, where no condition is satisfied, the statement inside the last else statement are executed.
In the next post we will discuss about  switch statement, goto operator and the conditional operator

Popular posts from this blog

Windows phone Wifi connectivity problem.

Create Excel file using Java: Apache POI - PART - 3: Bar Chart