Continue Statement || break Statement || Nested Loops

Continue Statement

The continue statement is used in the body of the loop to transfer the control to the start of the loop body. When this statement is executed in the loop body, the remaining statements of current iteration are not executed. The control directly moves to the next iteration.

Example:

int x;

for(x=1; x<=5; x++)

{

cout<<"Hello World! \n";

continue;

cout<<"Knowledge is power.";

}

Explanation

The above example has two cout statements. One statement is before the continue statement and one is after continue statement. The second statement is never executed. This is because each time continue statement is executed, the control moves back to the start of the body So "Knowledge is power" is never displayed.

C++ Program that inputs a number from the user using for loop. It displays the number if it is greater than 0 otherwise it inputs next number using continue statement.

include <iostream.h>

#include <conio.h>

void main()

{

int x, num,

for(x=1, x<=5; x++)

{

cout<<"Enter a number";

cin>>num;

if(num<=0)

continue;

cout<<"You entered "<<num<<endl;

}

getch();

}

Output:

Enter a number: 5

You entered: 5

Enter a number:-1

Enter a number: 0

Enter a number: -5

Enter a number: 100

You entered 100

break Statement

The break statement is used in the loop body to exit from the loop The remaining iterations of the loop are skipped when break statement is executed in the loop body The control directly moves outside the body and the statement after the body is executed.

Example:

for(int x=1; x<=5; x++)

{

cout<<"Questioning\n";

break;

cout<<"Gateway to knowledge";

}

cout<<"Bye";

Explanation

The above program uses break statement in for loop. The counter variable x indicates that the loop should execute for five times. But it is executed only once. In first iteration, the cout statement in the loop displays "Questioning and control moves to break statement. This statement moves the control out of the loop. So the message appears only once.

C++ Program that inputs number from the user using for loop. If the number is greater than 0, it is displayed and the next number is input. The program exits the loop if the number is 0 or negative using break statement.

include <iostream.h>

#include <conio.h>

void main()

{

int x, num;

for(x=1, x<=5; x++) 

{

cout<<"Enter a number";

cin>>num, if(num<=0);

break;

cout<<"You entered "<<num<<endl;

}

getch();

}

Output:

Enter a number: 5

You entered: 5

Enter a number: 10

You entered: 10

Enter a number: -5

Nested Loops

A loop within a loop is called nested loop. In nested loops, the inner loop is executed completely with each change in the value of counter variable of outer loop. The nesting can be done up to any level. The increase in level of nesting also increases the complexity of nested loop. Any loop can be used as inner loop of another loop. For example, while loop can be used as outer loop and for loop can be used as inner loop in nested loop.

Syntax:

The syntax of nested loop is as follows:

for (initializabon, condition, incrernent/decrement)

for (initialization, condition, increment/decrement)

{

statement(s);

}

Example

int i;

for(i=1, i<=2; i++)

for(j=1, j<=3; j++)

cout<<"Outer "<<i<<" Inner "<<j<<endl;

Post a Comment

0 Comments