Rev B Lecture 4
In the previous lecture we discussed how the computer can be programmed to make decisions. In this section we are going to learn how to program for tasks that require repetitive steps. Fortunately, unlike humans, the computer is very patient and does an excellent job of repetitive work. This of course assumes that the correct code was implemented. There are a number of C++ structures to cover this function.
Examples are adding huge amount of numbers, or reading data from a file, or introducing a time delay by implementing numerous loops.
1. While Loop
a) while (expression)
statement1;
statement2;
expression = true statemen1 executes over and over until not true.
expression = not true statement 2 executes
therefore statement 1 must provide eventually an exit or false condition in the while expression so that the program can proceed
b) while (expression)
{
statement 1;
statement n:
}
statemen n+1;
expression = true statements 1 to n executes
expression = not true statement n+1 executes
1) expression checks a counter
expression is increased or decreased until exit point is reached
2) expression checks for a special value called a sentinel
data is read until this special value is reached
3) expression checks for a bool variable
a flag is utilized as a continue or exit the loop condition
4) expression checks for an end of file (eof) marker
when reading data from an input stream the input stream variable will return a logical value of true or false depending upon data still in queue
e.g. while (cin) //will continue the while loop until all input data has been read.
when reading data from an output file the following function is use as an eof
when reading data from a file the function ifstream variable.eof() is used to determine eof
e.g. ifstream indata;
indata.open(test_file);
while(!indata.eof())
2. For Loop
Used to simplify while loops when counting is involved
for (initializing statement: evaluating loop condition; update statement)
Loop statement
Important: order of execution
a) initializing statement
b) evaluate loop condition
c) true
1) execute the loop statement
2) execute the update statement
d) repeat c until the loop condition evaluates to false
e) continue to execute the following statement
Note the loop statement can consist of more than one statement through use of the parenthesis
{
statement 1
statement n
}
3. do while loop
This structure differs from the while loop in that the statement executes first and then the expression or condition is evaluated
do
statement;
while (expression);
or for compound statements
do
{
statement;
statement n;
while (expression);
4. Choice of loops
a) Termination by number (beginning and end known should be a for loop
b) Does not involve a fixed range or known number of times and termination condition is at the beginning of loop then a use a while loop
c) Does not involve a fixed range or known number of times and termination condition is right after the loop then use a do while loop
5. Additional termination instructions.
a) break;
Exits early from a loop
b) continue
When encountered it skips the remaining statements and proceeds to the next iteration
of the loop
Exercises
A.
What
is the output of the following program segment?
int x = 14;
int y = 60;
while(((y - x) % 3) !=
0)
{
cout<<y<<"
";
y
= y - 5;
}
We can check the program using Visual
C++s debugger
Build Menu
Step Into (F11)
Start Debugger
Step Over (F10)
Continue single stepping(F10)and
observing variables change (variable window)
Exit debugger (shift F5)
B. A program to
check the speed of your computer. How
many loops does this program execute?
C.
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
int a,b,n;
b=0;
n=80000;
cout<<"the time is
"<< time(0)<<endl;
while(b<n)
{
a=0;
while(a<n)
{
a=a+1;
}
b=b+1;
}
cout<<"the time is
"<< time(0)<<endl;
return 0;
}
D. Give the output of the program segment if the input is 4 7 8 5 2
int sum = 0;
int num;
int j;
for(j = 1; j <=
5; j++)
{
cin>>num;
if(num < 0)
break;
sum = sum + num;
}
cout<<sum<<endl;
E. What is the
output of the following C++ code?
int x;
for (x=0;
x<3;x++)
cout<<x =
<<x<<endl;
x=x++;
cout<<x =
<<x<<endl;
try also with
debugger
F.
Write a program
that produces the following output
aaaaa
aaaa
aaa
aa
a
G.
What is the
output of the following C++ code?
Inputs
are 1 2
2 3 ^Z(EOF)
What
is the output of the following C++ code?
Inputs
are
1
2
-2
3
int num;
int
sum = 0;
cout<<"enter
the new number = ";
cin>>
num;
while(cin)
{
if
(num < 0)
{
cout<<"Negative number
found in the data"<<endl;
cout<<"enter the new
number = ";
cin>> num;
continue;
}
sum=sum+num;
cout<<"new number = " <<num;
cout<<" new sum = " <<sum
<<endl;
cout<<"enter the new number = ";
cin>>num;
}
H.
What is the output of the following C++ code?
int x = 7;
bool found = false;
do
{
cout<<x<<"
";
if(x <= 2)
found = true;
else
x = x-5;
}
while(x > 0
&& !found);
A. Trace
the following program given the data file: data.txt
containing:
3 4 5 6 0 17 18
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
bool correct
= true;
int num1;
ifstream infile;
infile.open("c:new folder/data.txt");
infile>>num1;
while(correct && num1 < 100)
{
cout<<num1<<endl;
cout<<"continuing...";
if (!num1) correct = false;
infile>>num1;
}
cout<<num1<<\n;
return
0;
}
answer for F.
for (i=5; i>=1; i--)
{
for (j=i; j>=1; j--)
cout<<'a';
cout<<endl;
}
Answer for G
enter the new number = 1 2 -2 3 ^Z
new number = 1 new sum = 1
enter the new number = new number = 2 new sum = 3
enter the new number = Negative number found in the data
enter the new number = new number = 3 new sum = 6
enter the new number = Press any key to continue . . .
Answer for H
7 2 Press any key to continue . . .
Answer for I
3
continuing...4
continuing...5
continuing...6
continuing...0
continuing...17
Press any key to continue . . .