FLOW OF CONTROL


FLOW OF CONTROL



Conditional Statements, if statement, if else , nested if, switch statement, Looping statement, while loop, do-while loop, for loop, Jump Statements, goto, break, continue

Statements

Statements are the instructions given to the computer to perform any kind of action. Action may be in the form of data movement, decision making etc. Statements form the smallest executable unit within a C++ program. Statements are always terminated by semicolon.

Compound Statement

A compound statement is a grouping of statements in which each individual statement ends with a semi-colon. The group of statements is called block. Compound statements are enclosed between the pair of braces ({}.). The opening brace ({) signifies the beginning and closing brace (}) signifies the end of the block.

Null Statement

Writing only a semicolon indicates a null statement. Thus ';' is a null or empty statement. This is quite useful when the syntax of the language needs to specify a statement but the logic of the program does not need any statement. This statement is generally used in for and while looping statements.

Conditional Statements

Sometimes the program needs to be executed depending upon a particular condition. C++ provides the following statements for implementing the selection control structure.
  • if statement
  • if else statement
  • nested if statement
  • switch statement

if statement

syntax of the if statement
if (condition)
{
  statement(s);
}
From the flowchart it is clear that if the if condition is true, statement is executed; otherwise it is skipped. The statement may either be a single or compound statement.
if-else

if else statement

syntax of the if - else statement
if (condition) 
  statement1; 
else 
  statement2;
From the above flowchart it is clear that the given condition is evaluated first. If the condition is true, statement1  is executed. If the condition is false, statement2 is executed. It should be kept in mind that statement and statement2 can be single or compound statement.

Nested if statement

The if block may be nested in another if or else block. This is called nesting of if or else block.
syntax of the nested if statement
if(condition 1) 

  if(condition 2) 
  { 
    statement(s); 
  } 
}
if(condition 1) 
  statement 1; 
else if (condition 2) 
  statement2; 
else statement3;

switch statement

The if and if-else statements permit two way branching whereas switch statement permits multiple branching. The syntax of switch statement is:
switch (var / expression) 

   case constant1 : statement 1; 
   break; 
   case constant2 : statement2; 
   break; 
   default: statement3; 
   break; 
}
The execution of switch statement begins with the evaluation of expression. If the value of expression matches with the constant then the statements following this statement execute sequentially till it executes break. The break statement transfers control to the end of the switch statement. If the value of expression does not match with any constant, the statement with default is executed.
Some important points about switch statement
  • The expression of switch statement must be of type integer or character type.
  • The default case need not to be used at last case. It can be placed at any place.
  • The case values need not to be in specific order.

Looping statement

It is also called a Repetitive control structure. Sometimes we require a set of statements to be executed a number of times by changing the value of one or more variables each time to obtain a different result. This type of program execution is called looping. C++ provides the following construct
  • while loop
  • do-while loop
  • for loop

While loop

Syntax of while loop
while(condition)
{
  statement(s); 
}
The flow diagram indicates that a condition is first evaluated. If the condition is true, the loop body is executed and the condition is re-evaluated. Hence, the loop body is executed repeatedly as long as the condition remains true. As soon as the condition becomes false, it comes out of the loop and goes to the statement next to the ‘while’ loop.
while do-while loop

do-while loop

Syntax of do-while loop
do
{
  statements;
} while (condition);
Note : That the loop body is always executed at least once. One important difference between the while loop and the do-while loop the relative ordering of the conditional test and loop body execution. In the while loop, the loop repetition test is performed before each execution the loop body; the loop body is not executed at all if the initial test fail. In the do-while loop, the loop termination test is Performed after each execution of the loop body. hence, the loop body is always executed least once.
for loop

for loop

It is a count controlled loop in the sense that the program knows in advance how many times the loop is to be executed.
syntax of for loop
for (initialization; decision; increment/decrement)
{
  statement(s);
}
The flow diagram indicates that in for loop three operations take place:
  • Initialization of loop control variable
  • Testing of loop control variable
  • Update the loop control variable either by incrementing or decrementing.
Operation (i) is used to initialize the value. On the other hand, operation (ii) is used to test whether the condition is true or false. If the condition is false, the program executes the body of the loop and then the value of loop control variable is updated. Again it checks the condition and so on. If the condition is true, it gets out of the loop.

Jump Statements

The jump statements unconditionally transfer program control within a function.
  • goto statement
  • break statement
  • continue statement
goto statement
syntax of goto statement
goto pqr:
pqr:
pqr is known as label. It is a user defined identifier. After the execution of goto statement, the control transfers to the line after label pqr.
break statement
syntax of break statement
The break statement can be used in a switch statement and in any of the loops. It causes program execution to pass to the next statement following the switch or the loop.
continue statement
The continue statement is used in loops and causes a program to skip the rest of the body of the loop.
while (condition)            
{
  Statement 1; 
    If (condition)
      continue;     
    statement; 
}
The continue statement skips rest of the loop body and starts a new iteration.
exit ( ) function
The execution of a program can be stopped at any point with exit ( ) and a status code can be informed to the calling program. The general format is
exit (code) ;
where code is an integer value. The code has a value 0 for correct execution. The value of the code varies depending upon the operating system. It requires a process.h header file.

No comments:

Post a Comment