Let’s continue the statements ………

NESTED IF-ELSE

Here under an if statement we can use many more if statements

SYNTAX:

if(condition )//start of 1st if

  {-------------

    if (condition )     //start of 2nd if

      {------------

        if(condition ) //start of 3rd if

         {-----------

         }   //end of 3rd if

        else    //3rd if's else

       {-----------------

        }

      }                   //end of 2nd if

      else     //2nd if's else

      {--------------

       }

  } //end of 1st if

else   // 1 st if's else

 {

 }

this is nested if-else thumb rule: the if’s starts in ascending order and else start in descending order ,i.e  the last if will encounter its else first and so on…

EXAMPLE

statements-2.7

statements-2.8

nested if-else are very complicated (even I get confuse most of the times  😛 ) anyways its better to use it when no other option is available and one should try not to make it more complicated .

ITERATION STATEMENTS : LOOP

We have already discussed about it. Click to open Types of control loops

JUMP STATEMENTS

These statements allow to alter the flow of program by jumping from one statement to another and hence the name is given. Types of jump statements are the goto statement, the continue statement,the breakstatement, and thereturn statement, which are discussed in the following sectio

Break statement

Where ever break statement is encountered , the loop is broken and the flow of control goes to the statement after the loop .If written outside a loop then the program gets terminated as soon as it is encountered . It simply breaks free  from anywhere it is encountered .

EXAMPLE:

statements-2.9

statements-2.10

 

here in this example we have used a for loop , if break statement was absent then we would have got an output

 0
 1
 2
 3
 4
 5
 6

but if you’ll look carefully the output is not the same. It is because here we have used break statement. According to our code as soon as a 3 will be encountered the break statement will be executed and the compiler will come out of the loop .

hence the output is seen .

The rest of the topic is discussed in next post ….

Have something to add in Statements-2: Nested if-else and break statements ? Share/Ask into comments it in the comments.

Follow us on Facebook, Google Plus and Twitter

Previous                                      C Programming                                      Next