Ticker

6/recent/ticker-posts

CONTROL STRUCTURE IN C++

C++ program is usually not limited to a linear sequence of instructions but it may bifurcate, repeat code or may have to take decisions during the process of coding. For that purpose, C++ provides control structures which are used to control the flow of program. 

 Before we discuss control structures, let us first discuss a new concept: the compound-statement or block, which is very much needed to understand well the flow of control in a program. 

 A block is a group of statements which are separated by semicolons (;) like all C++ statements, but grouped together in a block enclosed in braces: { }: for example: 


     statement1; 
     statement2; 
     statement3; 
     ... 

represents a compound statement or block. 

In C++ object oriented programming, the control structure can be classified into following three categories: 

  • Selection or conditional statement; 
  • Iterating or looping statement; 
  • Breaking statement; 

Selection or conditional statement

In this type of statement, the execution of a block depends on the next condition. If the condition evaluates to true, then one set of statement is executed, otherwise another set of statements is executed. C++ provides following types of selection statements: 

  • If; 
  • If-else; 
  • Nested if; 
  • Switch conditional

a) if statement: 

The syntax of if statement is

If (expression) 

(Body of if) 
Statements; 
}

Where, expression is the condition that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.

b) if-else statement: 

The syntax of if –else statement is

If (expression) 

     (Body of if) Statements 1; 
 } 
 else 
 { 
     (Body of else} Statement 2 
 } 

Where, expression is the condition that is being evaluated. If this condition is true, Introduction to C++ statement - 1 is executed. If it is false, then if statement is skipped and the body of else statement is executed.

c) Switch statement: 

Switch statement is used for multiple branch selection. The syntax of switch statement is

switch (expression) 

    case exp 1: First case body; 
                        Break; 
    case exp 2: Second case body; 
                        Break; 
    case exp 3: Third case body; 
                        Break; 
    default: 
                        default case body; 

Here, expression is the condition that is being evaluated. If the case 1 condition is true, First case body is executed, otherwise case exp 2 is checked and so on....If none of case expressions is true then the value of default case body is executed.

d) Nested if statement:

A nested if statement is a statement that has another if in its if‟s body or in its else‟s body. The syntax of switch statement is

if (expression 1) 
        statement 1; 
else if (expression 2) 
        statement 2; 
else if (expression 3) 
        statement 3; 



else 
        statement; 

Here, expression is the condition that is being evaluated. If the case 1 condition is true, First case body is executed, otherwise it is skipped and next else expression is evaluated and so on.....

Iterative or looping statement

In C++ , programming language looping statement is used to repeat a set of instructions until certain condition is fulfilled. The iteration statements are also called loops or looping statement. C++ allows following four kinds of iterative loops: 

for loop 

while loop 

do-while loop and 

nested loops

a) for loop 

This loop is easiest amongst all loops in C++ programming. The syntax of this loop is: 

for (initialization; condition; increase) 
 { 
        (body of the loop) statements; 

This loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration 

It works in the following way: 

initialization is executed. Generally, it is an initial value setting for a counter variable. This is executed only once. 

condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed). 

statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }. 

finally, whatever is specified in the increase field is executed and the loop gets back to step 2. 

b) while loop 

If we do not know the number of iterations before starting the loop then while loop is used. Its syntax is as follows: 

The functionality of this loop is simply to repeat statement while the condition set in expression is true. 

initialization; 
while (expression) 

            statement; increment; 

c) The do-while loop 

Unlike for and while loops, the do-while is an exit-controlled loop i.e., it evaluates its test – expression at the bottom of the loop after executing its loop-body statement. This means that a do-while loop always executes at least once, even when the test – expression evaluates to false initially. 

 Its syntax is given as:

do 

         statement 
} while (test condition); 

The do-while loop is usually used when the condition that has to determine the end of the loop is determined within the loop statement itself, like in the previous case, where the user input within the block is what is used to determine if the loop has to end.

d) Nested for loop 

If a loop is placed inside the same loop then it is called nested for loop in C++ programming language. To understand, it let us take the following program:

for (i=1; i<=n-i; i++) 
{         
        for (j=1; j<=n-i; j++) 
        
                 cout << " "; 
        
         for (k=1; k<=i; k++) 
        
                cout<< k; 
        
    cout << end; 
}

Breaking statement

Using break, we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end. In this section, we will discuss following breaking statements: 

break statement 

continue statement 

goto statement and 

exit statement 

 a) break statement 

The break statement is used to terminate the execution of the loop program. It terminates the loop in which it is written and transfers the control to the immediate next statement outside the loop. The break statement is normally used in the switch conditional statement. To understand, it let us take the following C++ program: 

// break loop example 

#include 
using namespace std; 
int main () 

         int n; 
         for (n=10; n>0; n--) 
         { 
                 cout << n << ", "; 
                 if (n==3) 
                 { 
                        cout << "countdown aborted!"; 
                         break; 
                 } 
         } 
         return 0; 
}

b) continue statement 

The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration. For example, we are going to skip the number 5 in our countdown:

// continue loop example 
#include 
using namespace std; 
int main () 

         for (int n=10; n>0; n--) 
        
                 if (n==5) 
                        continue; 
                  cout << n << ", "; 
         } 
         cout << "FIRE!\n";
         return 0; 
}

c) goto statement 

The goto statement is used to transfer control to some other parts of the program. It is used to alter the execution sequence of the program. To illustrate goto statement, let us take the following C++ program: 

// goto loop example 

#include 
using namespace std; 
int main () 

         int n=10; 
         loop: 
                cout << n << ", "; 
                 n--; 
                 if (n>0) 
                        goto loop; 
                 cout << "FIRE!\n"; 
         return 0; 
}

d) exit () statement

The exit statement is used to terminate the execution of the program. It is used when we want to stop the execution of the program depending on some condition. When we use exit () statement, we have to include other library functions such as process.h, or stdio.h file. To illustrate exit () statement, let us take the following C++ program: 

// Program for exit statement

#include <iostream.h>
#include <conio.h>
void main ()
{
        clrscr();
        int i, number;
        i = 1;
        while(i<5)
        {
                cout <<”Enter the number:";
                cin>>number;
                if number>5
                {
                        cout <<”The number is greater than five or equal to” <<end1;
                        exit();
                }
                cout << "The number is: “<<number<<end1;
                i++
        }
        getch();
}

ALSO SEARCH:

"control structure in C++"

"sequence control structure in C++"

"loop control structure in C++"

"selection control structure in C++"

"control structure in C++ language"

"loop control structure in C++"

"decision control structure in C++"

"unconditional control structure in C++"

"explain different control structure in C++"

"case control structure in C++"

"nested control structure in C++"

"iterative control structure in C++"

"repetition control structure in C++ programming"

"control flow structure in C++"