0% found this document useful (0 votes)
40 views

Control and Structure (Part 3)

The document discusses various flow control statements in C++ including break, goto, continue, and exit. It provides examples of using break to exit loops early, goto to jump to labeled statements, continue to skip to the next iteration of a loop, and exit to terminate the entire program. Specifically, it shows examples of using these statements with while, do-while, and for loops to control the flow of a program that totals integers input by the user until they enter 0.

Uploaded by

Adib Idris
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Control and Structure (Part 3)

The document discusses various flow control statements in C++ including break, goto, continue, and exit. It provides examples of using break to exit loops early, goto to jump to labeled statements, continue to skip to the next iteration of a loop, and exit to terminate the entire program. Specifically, it shows examples of using these statements with while, do-while, and for loops to control the flow of a program that totals integers input by the user until they enter 0.

Uploaded by

Adib Idris
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Control and Structure

Part 3 : Break, Goto, Continue, Exit ()


Break
• When ‘break’ statement is encountered, the
flow straightaway breaks from the current
loop and go to the statement after the loop
While + break
#include <iostream>
#include <string>
using namespace std;

int main()
{
int number=0;
while (1)
{
cout << "*" ;
number = number +1;
if (number ==100)
break;
}
cout <<endl <<"Enough"<<endl;
}
Do-while + break
#include <iostream>
#include <string>
using namespace std;

int main()
{
int number=0;

do
{
cout << "*" ;
number = number +1;
if (number ==100)
break;
}
while (1);
cout <<endl <<"Enough"<<endl;
}
For + break
#include <iostream>
#include <string>
using namespace std;

int main()
{
int number=0;

for (number=0;number>=0;number++)
{
cout << "*" ;
if (number ==100)
break;
}
cout <<endl <<"Enough"<<endl;
}
Example
• A program to average ‘undetermined’ number
of numbers.
START

Count =0

yes

Count <=100?

no

Display “Input
numbers:….(0 to exit)”

Input “numbers”

yes
Numbers=0? Break

no

Count +1 Average = Total / count

Total = total + numbers

END
Totalling integer input
#include <iostream>
#include <string>
using namespace std;

int main()
{
int number;
int total = 0;

do
{
cout << "Input number (0 to exit):"<<endl;
cin >> number;
if (number==0)
break;
total = total+number;
cout << total <<endl;
}
while (1);
cout << "Final total="<<total<<endl;
}
Go-to
• The goto statement transfers control to the
location specified by label. The goto statement
must be in the same function as the label it is
referring, it may appear before or after the
label.
While + goto
#include <iostream>
#include <string>
using namespace std;

int main()
{
int number=0;
while (1)
{
cout << "*" ;
number = number +1;
if (number ==100)
goto enough;
}
enough: cout <<endl <<"Enough"<<endl;
}
Do-while + goto
#include <iostream>
#include <string>
using namespace std;

int main()
{
int number=0;

do
{
cout << "*" ;
number = number +1;
if (number ==100)
goto enough;
}
while (1);
enough: cout <<endl <<"Enough"<<endl;
}
For + goto
#include <iostream>
#include <string>
using namespace std;

int main()
{
int number=0;

for (number=0;number>=0;number++)
{
cout << "*" ;
if (number ==100)
goto enough;
}
enough: cout <<endl <<"Enough"<<endl;
}
Totalling integer input
#include <iostream>
#include <string>
using namespace std;

int main()
{
int number;
int total = 0;

do
{
input: cout << "Input number (input 100 to exit):"<<endl;
cin >> number;
if (number==0)
goto input;
if (number==100)
break;
total = total+number;
cout << total <<endl;}

while (1);
cout << "Final total="<<total<<endl;
}
Continue
• The continue statement causes a jump, as if
by goto to the end of the loop body (it may
only appear within the loop body of for, range-
for, while, and do-while loops).
Totalling +ve integer input
#include <iostream>
#include <string>
using namespace std;

int main()
{
int number;
int total = 0;

do
{
cout << "Input number (0 to exit):"<<endl;
cin >> number;
if (number<0)
{continue;}
if (number==0)
break;
total = total+number;
cout << total <<endl;}

while (1);
cout << "Final total="<<total<<endl;
}
Totalling integer input
#include <iostream>
#include <string>
using namespace std;

int main()
{
int number;
int total = 0;

do
{
cout << "Input number (0 to exit):"<<endl;
cin >> number;
//if (number<0)
// {continue;}
if (number==0)
break;
total = total+number;
cout << total <<endl;}

while (1);
cout << "Final total="<<total<<endl;
}
Exit
• The C++ library function void exit() terminates
the calling process immediately
Totalling integer input
#include <iostream>
#include <string>
using namespace std;

int main()
{
int number;
int total = 0;

do
{
cout << "Input number (0 to exit):"<<endl;
cin >> number;
if (number==0)
{exit(0);}
total = total+number;
cout << total <<endl;}

while (1);
cout << "Final total="<<total<<endl;
}

You might also like