"Z Code Writer" is an Online Blog for Programmer & Beginner. Here, You will Find tech, Build your Programming Skills and other Programming Language Like C, C++, HTML, Java Script Java Code, Web Designing and University Of Sialkot (USKT) CGPA / GPA Calculator etc.

Sunday 18 August 2019

Nested 'if' With Sequence of 'if' Statement

Nested 'if' With Sequence of 'if' Statement

      The nested 'if' structure may contain multiple 'if' statements that may be nested up to any level. Some 'if' statements are executed while other may be skipped.
In a sequence of 'if' statements, multiple 'if' statements are written one after the other. All 'if' statements are executed in an order. No 'if' statement is skipped.
Following program finds whether a number is positive, negative or zero:

         #include <stdio.h>
          void main (void)
         {
                  int num;
                  printf ("Enter a number ? ") ;
                  scanf ("%d" ,  &num) ;
                  if (num > 0)
                           printf ("The number is positive") ;
                  if (num < 0)
                            printf ("The number is negative") ;
                  if (num == 0)
                            printf ("The number is zero") ;
           }

      In the above program, three 'if' statements are given in a sequence. Suppose the user enters a positive number. The number is tested with first 'if' statement. The answer is true because number is positive. The message is displayed on the display screen. After displaying the message, other two 'if' statements will also be executed. Although the answers of these two 'if' statements will be false. There is no need to execute these 'if' statements. These statements should be skipped. Otherwise processor of computer has to consume extra time without any purpose.
      The 'if-else-if' structure can be used to handle this kind of situation.


The 'if-else-if' Statement

      The 'if-else-if' statement is also called multiple 'if-else' statement or nested 'if-else' statement. It is used to execute on block of statements from multiple blocks of statements. In this statement/structure, multiple conditions and multiple blocks of statements are given. When any given condition is true, the statement associated with that condition are executed. All other blocks of statements are ignored.
      The general syntax of nested 'if-else' is as follows:

                 if (condition-1)
                 { Block-1 }
                 else if (condition-2)
                        { Block-2 }
                 ------------------------
                 ------------------------
                 else if (condition-m)
                        { Block-m }
                        else
                        { Block-n }

      The use of "else" part of condition-m is optional.



Working of 'if-else-if' Structure


     The 'if-else-if' structure is executed as follows:
  • The conditions in this structure are evaluated in the given sequence until a true condition is found.
  • When true condition is found, the statements associated with that condition are executed. All remaining conditions are skipped.
  • If all conditions are false, then the block of statements following the last else is executed.

Flowchart of 'if-else-if' Structure

     The flowchart of nested 'if-else' structure is as follows:





No comments:

Post a Comment

Thanks For Visiting Here...!! I will Reply you as soon as possible.