0% found this document useful (0 votes)
5 views16 pages

Programming - Lec. 2 (Breaking Control Statements)

The document provides an overview of breaking control statements in C++, including break, continue, and goto statements. It includes examples and exercises for students to practice writing C++ programs that utilize these control statements. The objective is to teach students effective handling of loop statements through practical coding tasks.

Uploaded by

Zaid Mustafa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Programming - Lec. 2 (Breaking Control Statements)

The document provides an overview of breaking control statements in C++, including break, continue, and goto statements. It includes examples and exercises for students to practice writing C++ programs that utilize these control statements. The objective is to teach students effective handling of loop statements through practical coding tasks.

Uploaded by

Zaid Mustafa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

‫كلية الهندسة‬/‫جامعة النهرين‬

‫قسم هندسة الطب الحياتي‬

Breaking Control
Statements

Lec-2

1st year
2nd Semester

1
Objectives

• Learn the student the use of braking control statements.

• Write several C++ programs which use of braking control


statements.
Breaking Control Statements
Breaking Control Statements
• For effective handling of the loop statements, C++ allows the use of the following types of
control break statements:

A. Break Control Statement: The break statement is used to terminate the control from the
loop statements of the case-switch structure. The break statement is normally used in the
switch-case loop and in each case condition; the break statement must be used. If not, the
control will be transferred to the subsequent case condition also. The general format of the
break statement is : (Break;)
Breaking Control Statements
• Example 1:

for ( i = 1; i < 100; i ++ ) Output:


{ 12345678910
cout << i;
if ( i == 10 ) break;
}

• Example 2:

for ( i = 1; i < 10; ++ i )


for ( j = 1; j < 20; ++ j )
{
cout << i * j << endl;
if ( j == 10 ) break;
}
Breaking Control Statements
B. Continue Control Statements : The continue is used to repeat the same operations once
again even if it checks the error. It is used for the inverse operation of the break statement.
Its general syntax is: ( continue; ).

• Example 1: Output
do x=2
{ n=3
cout<<" x= "; cin >> x; x=4
cout<<" n= "; cin >> n; n= -2
if ( n < 1 ) continue; 4
cout << x;
Breaking Control Statements
C. Goto Control Statement : The goto statement is used to alter the program execution
sequence by transferring the control to some other part of the program. Its general syntax is:
( goto label; )

There are two ways of using this statement:

1. Unconditional Goto: It is used just to transfer the control from one part of the program to
the other part without checking any condition. It is difficult in use.

2. Conditional Goto: It is used to transfer the control of the execution from one part of the
program to the other in certain conditional cases.
Breaking Control Statements
1. Unconditional Goto

Example : Write C++ program to check if zero or negative value found:

#include<iostream.h>
void main( )
{
Start: cout<<”***\n”;
Goto start;
}
Breaking Control Statements

2. Conditional Goto

Example : Write C++ program to check if


zero or negative value found:
Procedure
Breaking Control Statements
• Example 1: Write C++ program to check if zero or negative value found.
Breaking Control Statements
• Example 2: Repeat above example ( EX.1) using continue statement?
Breaking Control Statements
• Example 3: Write C++ program to read 7 marks, if pass in all marks (>=50) print “pass”
otherwise print “fail” using break statement?
Breaking Control Statements
• Example 4: Repeat above example ( EX.3) using goto statement?
Discussion
Discussion
1. Write C++ program to display the first 100 odd number using break and goto statements?

2. What is the output of the following C++ segment of code?

for ( I = 0; I < 8; I ++ )
{
if ( I % 2 == 0 ) cout << I + 1 << endl;
else if ( I % 3 == 0 ) continue;
else if ( I % 5 == 0 ) break;
cout << “end program \n”;
}
cout << “end …”;

You might also like