statements-if-else-break
statements-if-else-break

Lets    continue the statements  ………

NESTED IF-ELSE

here under an if statemnt 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 .

  • JUMP STATEMENTS

these statements allow to alter the flow of program by jumping from one statement to another and hence the name is given .

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 ull look carefully the output if not the same .Its 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 ….

NO COMMENTS