"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.

Tuesday 30 July 2019

Nested 'if' Statement

Nested 'if' Statement

      An ' if ' statement within another ' if ' statement is called nested ' if ' statement. The ' if ' statement that contains another ' if ' statement is called outer ' if ' statement. The ' if ' statement used inside outer ' if ' statement is called inner ' if ' statement. The nested ' if ' statements are used when multiple conditions have to check. Nested can be up to any level. However, it increases the complicity of the logic of program.
      The general form of nested 'if' statement is given as follows:

                     if (condition-1)
                     {
                               if (condition-2)
                              {
                                        Block-1 ;
                               }
                               else
                              {
                                        Block-2 ;
                               }     
                       else
                      {
                                        Block-3 ;
                       }

      In the above syntax, the 'if' statement that has "condition-1" contains another 'if' statement that has "condition-2". The Block-1 represents the statement(s) of inner 'if' statement, while Block-2 represents the statement(s) of its 'else' part. The Block-3 represents the statement(s) of 'else' part of outer 'if' statement.

Flowchart of Nested ' if ' Structure

      The flowchart about general form of nested 'if' structure is as follows:
                        



Working of Nested ' if ' Structure


      The nested ' if ' structure is  executed as follows:
  • The condition of the outer ' if ' statement is evaluated first.
  • If condition of the outer ' if ' statement is true, inner 'if' statement is executed.
  1. If condition of the inner ' if ' statement is true, Block-1 is executed.
  1. If condition of the inner ' if ' statement is false, Block-2 is executed.
  • If condition of the outer ' if ' statement is false, Block-3 is executed.

Example

      Following statements check whether the value of variable 'a' is greater than the values of variables 'b' and 'c';
                      if (a  >  b)
                                  if (a  >  c)
                                          printf ("a is greater than b and c") ;
      In the above piece of code, the 'if' structure that has condition (a > c) is inner ' if ' structure. It comes inside the ' if ' structure that has condition (a > b).



No comments:

Post a Comment

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