FLOW OF CONTROL
FLOW OF CONTROL Contents 1. Statements 2. Selection statements i) if statements ii) switch statement 3. Iteration statements i) for loop ii) while loop iii) do-while 4. Jumb statement i) goto ii) break iii) continue
STATEMENTS The instructions given to the computer to perform the action. { statement1 ; statement2 ; . } Statements are terminated with a semicolon. The simplest statement is the Empty or null statement.
SELECTION STATEMENT if statement nested ifs if-else-if ladder switch statement
if STATEMENT If the condition evaluates to true, a course-of-action is followed otherwise the course of action is ignored Syntax if(expresssion) { statement; }
Nested ifs STATEMENT Syntax if(expresssion1) { -- -- if(expression2) [else statement2;] } else body of else;
if-else-if STATEMENT Syntax if(expresssion1) { statement 1; } else if(expression2) statement2; statement 3;
switch STATEMENT Syntax switch(expresssion) { case constant 1 : statement sequence 1; break; case constant 2 : statement sequence 2; case constant 3 : statement sequence 3; : case constant -1 : statement sequence n-1; [default : statement sequence n ] }
ITERATION STATEMENT Allow a set of instructions to be performed repeatedly until a certain condition is fulfilled for loop while loop do-while loop
for Loop Syntax Execute for(intializationexpression(s); test-expression; update expressions) { body- of – the – loop; } Execute 4th Execute 2nd Execute 1st Execute 3rd
while Loop Syntax Initialization expression; while (test-expression) { statements; : update expressions; }
do-while Loop Always execute at least once Syntax Initialization expression; do { statements; : update expressions; } while (test-expression)
JUMP STATEMENT goto statement break statement continue
goto STATEMENT goto label; --- label : Transfer the program control to specified place by a label Syntax goto label; --- label : label is a user supplied identifier and can appear either before or after goto.
break STATEMENT Enables a program to skip over part of the code while (test exprsn) { statement 1; if (val >2000) break; : statement2 } statement3; for(inilialize; test- exprsn; update) { statement 1; if (val >2000) break; : statement2 } statement3; do { statement 1; if (val >2000) break; : statement2 } while(test exprsn) statement3;
continue STATEMENT Instead forcing termination, it forces the next iteration of the loop to take place, skipping any code in between while (test exprsn) { statement 1; if (val >2000) continue; : statement2 } statement3; for(inilialize; test- exprsn; update) { statement 1; if (val >2000) continue; : statement2 } statement3; do { statement 1; if (val >2000) continue; : statement2 } while(test exprsn) statement3;
Diagrammatic representation Flow of control Statements Selection Statements Iteration Statements Jump Statement if stme nts switch stmen ts for stmen ts while stmen ts do-while stmen ts goto stmen ts break stmen ts continue stmen ts