Control and Structure (Part 3)
Control and Structure (Part 3)
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
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;
}