You are on page 1of 7

Programming Logic and Design

TOPIC TITLE: C++ BREAK/CONTINUE STATEMENTS


SPECIFIC OBJECTIVES:

At the end of the topic session the students are expected to:

1. Describe what is C++ break statement.


2. Describe what is C++ continue statement.
3. Develop a C++ program using C++ break and continue statements.

MATERIALS/EQUIPMENT:

1. Power Point Presentation


2. PLD Module #16

TOPIC PRESENTATION:

Flow of discussion for C++ Break/Continue Statements:

1. Discuss the break statement.


2. Discuss the continue statement.
3. Discuss sample C++ program using break and continue statement

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 1 of 7


Programming Logic and Design
C++ Break Statement

The jump (break, continue) statements unconditionally transfer program control within a
function. C++ has four statements that perform an unconditional branch :

 return
 goto
 break
 continue

C++ break Statement

The break statement enables a program to skip over part of the code. A break statement
terminates the smallest enclosing while, do-while, for, or switch statement. Execution
resumes at the statement immediately following the body of the terminated statement.

The following code fragment gives you an example of a break statement :

int a, b, c, i;
for(i=0; i<20; i++)
{
cout << "Enter 2 numbers" ;
cin >> a >> b ;
if(b == 0)
break;
else
c = a/b ;
cout << "\n Quotient =" << c << "\n" ;
:

The above code fragment inputs two numbers. If the number b is zero, the loop
immediately terminated otherwise the numbers are repeated input and their quotients are
displayed.

If a break statement appears in a nested-loop structure, then it causes an exit from only
the very loop it appears in. For example :

:
for(i=0; i<10; i++)
{
j=0;
cout << "\n Enter character";
cin >> ch;

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 2 of 7


Programming Logic and Design
cout << "\n";
for( ; ; )
{
cout << ch;
j++ ;
if(j == 10)
break;
}
cout << "\n...." ;
}

The above code fragment inputs a character and prints it 10 times. The inner loop has an
infinite loop structure but the break statement terminates it as soon as j becomes 10 and
the control comes to the statement following the inner loop which prints a line of dashes.

A break used in switch statement will affect only that switch i.e., It will terminate only the
very switch it appears in. It does not affect any loop the switch happens to be in.

C++ continue Statement

The continue is another jump statement like the break statement as both the statements
skip over a part of the code. But the continue statement is somewhat different from break.
Instead of forcing termination, it forces the next iteration of the loop to take place,
skipping any code between.

For the for loop, continue causes the next iteration by updating the variable and then
causing the test-expression's evaluation. For the while and do-while loops, the program
control passes to the conditional tests.

Note - The continue statement skips the rest of the loop statements and causes the next
iteration of the loop.

The following code fragment gives you an example of continue statement :

:
int a, b, c, i;
for(i=0; i<20; i++)
{
cout << "\n Enter 2 numbers" ;
cin >> a >> b ;
if(b == 0)
{
cout << "\n The denominator cannot be zero" << "Enter again !";
continue;
}

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 3 of 7


Programming Logic and Design
else
c = a/b ;
cout << "\n Quotient =" << c << "\n" ;
}

Tip - Do not confuse the break (exits the block) and continue (exits the remaining
statement(s) ) statements.

A break statement inside a loop will abort the loop and transfer control to the statement
following the loop. A continue statement will just abandon the current iteration and let the
loop start the next iteration.

C++ break and continue Statement Example

Following example program uses two loops to perform the same thing, but replaces
break statement with continue. Have a look at one code and then the output to
understand the difference between break and continue statements :

/* C++ Jump Statements - C++ break and continue Statement */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"The loop with \'break\' produces output as:\n";
for(int i=1; i<=10; i++)
{
if((i%3)==0)
break;
else
cout<<i<<endl;
}
cout<<"\nThe loop with \'continue\' produce output as:\n";
for(i=1; i<=10; i++)
{
if((i%3)==0)
continue;
else
cout<<i<<endl;
}
getch();
}

When the C++ program is compile and executed, it will produce the following output :

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 4 of 7


Programming Logic and Design

C++ exit() function

Like you can break out of loops using a break statement, you can break out of a program
using library function of C++, the exit() function. This function causes the program to
terminate as soon as it is encountered, no matter where it appears in the program listing.
Following program illustrates the use of exit() function :

/* C++ Jump Statements - C++ exit() Function */

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int num, i;
cout<<"Enter the number: ";
cin>>num;
for(i=2; i<=num/2; i++)
{
if(num%i==0)
{
cout<<"\nNot a prime number.. !!";
getch();
exit(0);
}
}
cout<<"\nIt is a prime number.";
getch();
}

When the above C++ program is compile and executed, it will produce the following
output:

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 5 of 7


Programming Logic and Design

The above program accepts a number and tests whether it is prime or not. If the number
is divisible by any number from 2 to half of the number, the program flashes a message
that the number is not prime and exits from the program as it is caused by the exit()
function.

The exit() function as such does not have any return value. Its argument, which is 0 in the
above program, is returned to the operating system. This value can be tested in batch
files where ERROR LEVEL gives you the return value provided by exit() function.
Generally, the value 0 signifies a successful termination and any other number indicates
some error.

The exit() function has been defined under a header file process.h which must be
included in a program that uses exit() function.

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 6 of 7


Programming Logic and Design

REFERENCES:

 C++: From Problem Analysis to Program Design

 Introduction to C++ Programming Logic

Prepared by : FREDDIE M. TAMAYAO, MENG-CPE, CISSP Page 7 of 7

You might also like