You are on page 1of 2

Experiment # 12

OBJECTIVE:

To experiment with iterative structures in C++

a) To be familiar with ‘continue’ statement.


b) To be familiar with ‘break’ statement.

Theory:

‘Continue’ Statement:

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

Example:

Q: Write a program that display the sum of the following series: 1+3+5+7+….100

#include <iostream>
#include<conio.h>
Void main()

{
Int sum;
for (int i=1; i<100;i++) Output:
{ The sum is 2500
If(i%2==0)
Sum=sum+i;
}
cout<<”the sum is”<< sum;
getch()
}

‘break’ statement:

The break statement is used in the body of the loop to exit from the loop. When this statement is
executed in the loop body, the remaining iterations of loop are skipped. The control directly
moves outside the body and the statement that comes immediately after the body is executed.
Example Output:
enter a number: 5
Q: Write a program that inputs two numbers from the you entered: 5
user using “for” loop. If the number is greater than 0, it enter a number: 10
is displayed and again user is asked to enter another you entered: 10
number. The program exit the loop if user entered enter a number: -5
number is 0 or negative (Hint: use ‘break’ statement)

#include <iostream>
#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
}

Cout<< “You entered …” << num << endl;


getch()
}

Exercise:
Q#01. Write a program that prints the following numbers using above programming techniques?
1, 2,3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20.

Q#02. xxxx

You might also like