Back home next view contents in a pop up window
Go to: if - if else - switch - continue
The
while
The
while repetition structure.
program 1.6
#include <iostream.h>
int main ()
{
int select,num1, num2;
if
(select!=-1)
// Line 5
{
cout << " Enter a number.";
cin >> num1;
cout << " Enter a second number";
cin >> num2;
cout << num1 << " " << num2;
cout << "
.Do you want to continue. Press -1 to stop. Press any other key to continue\n";
cin >> select;
}
return 0;
}
output 1.6
| Enter a number.1 Enter a second number2 1 2 .Do you want to continue. Press -1 to stop. Press any other key to continue 2 Press any key to continue |
As you can see here we used the
if
command (line 5) if (select!=-1) . The
program asked us to enter two numbers, then it asked us if we want to continue
by hitting any key besides -1. I entered the number two, but the program went on
and terminated. It did not loop. Lets see the same example, using the the while
command.
Lets demonstrate the use of the while command in an
actual C++ program.
program 1.7
#include <iostream.h>
int main ()
{
int select,num1, num2;
while
(select!=-1)
// Line 5
{
cout << " Enter a number.";
cin >> num1;
cout << " Enter a second number";
cin >> num2;
cout << num1 << " " << num2;
cout << "
.Do you want to continue. Press -1 to stop. Press any other key to continue\n";
cin >> select;
}
return 0;
}
program 1.7
| Enter a number.1 Enter a second number2 1 2. Do you want to continue. Press -1 to stop. Press any other key to continue 2 Enter a number.1 Enter a second number3 1 3 .Do you want to continue. Press -1 to stop. Press any other key to continue -1 Press any key to continue |
Here we see that the program did loop. When asked if I wanted to continue I pressed 2 and the program continued looping. No matter what key I press the program will keep looping, unless I hit the -1 key. In that case the condition while (select!=-1) is met. In other words what this condition is saying to the program is this : "keep looping as long as the variable select is not -1".
Lets view another program using the while and what we call a counter.
program 1.8
#include <iostream.h>
int main ()
{
int counter=0, Max;
//line 4
cout << " Enter up to what number you want the computer to count: ";
cin >> Max;
// line 6
while (counter<Max)
// line 7
{
counter++;
//line 9
cout << counter << endl;
}
return 0;
}
This program asks up to what number we want the computer to
count. Variable Max (line 4) is the repetition controller. It will loop the program
as many times as the number we will enter in cin >> Max; (in line
6). In line 7 we have made the fallowing condition while(counter<Max).
Also notice in line 9 the counter++; That means that values will be added in variable counter in
increments of one but the total of the variable counter will not exceed the
value we have given to variable Max.
output of program 1.8
| Enter up to what number you want the computer to
count: 4 1 2 3 4 Press any key to continue |
Here I assigned the value of four for variable max. On line 7 I told the computer that it should continue looping until the variable counter is less than four. In line 9 the counter is adding values to it self of increments of one until it reaches the value of four in which case it stops looping.
notice: If you I was to put while(counter<=4) the computer would count up to 5. This is because the variable counter already was given a value (line 4) of zero. Zero is a value as well. By saying counter <4 that means that the counter is actually looping 3 times.
Let us see a more to “real life” program.
program 1.9
#include <iostream.h>
int main ()
{
int courses, loop1=0, loop2=1; //
Line 4
float average, grades, total=0;
cout << " Please enter how many courses you took this year.:";
cin >> courses;
while (loop1<courses) //line 8
{
cout << " Please enter grade for class " << loop2 << endl;
cin >> grades;
total=total + grades;
loop1++;
loop2++;
}
average=total/courses;
cout << " The average from all of you classes is: " << average << endl;
return 0;
}
out put from program 1.9
|
Please enter how many courses you took this year.:4 |
The pr
Notice the loop2.It is used to display for what course you are entering the grade :
Please enter grade for class 1
Please enter grade for class 2
Please enter grade for class 3
I assigned the value of value of one in line 4. Otherwise the output would be like this
Please enter grade for class 0
Please enter grade for class 1
Please enter grade for class 2
Thus confusing the user.
The
for
The for command performs a somewhat similar task with the while command. The difference between those two is that the for command is used when we want a block of code to run a specific number of times. The while command is used when we want a block of code to run until a certain condition is met. The for command will loop the program with out testing the loop condition. The while condition will test the loop condition, thus there is always a possibility that when using the while command the loop will run zero times if a condition is not met.
Here is an example of the for command.
program 1.10
#include<iostream.h>
int main () {
int Max;
cout << " How many times you want the loop to run?";
cin >> Max;
for (int loop=0; loop<=Max; ++loop)
// Line 6
{
cout << loop << endl;
}
return 0;
}
output 1.10
| How many times you want the loop to run?8 0 1 2 3 4 5 6 7 8 Press any key to continue |
In line 6 we created within the parentheses a variable of int type (loop). We could do that outside the parentheses as well. Then we initialized the value of the variable loop to zero and we also told the computer that variable loop will not exceed the value of variable Max.. The ++loop will start the actual looping. Notice the semicolons after loop=0 and loop<=Max. This should always be done otherwise the program will not compile.
| Other forms that the FOR command can take. Things your school text book most likely does not teach you! SOON TO BE ADDED!!!! |
Along with the while command we can use a command called do.
The shoot first and ask questions later command.
In my opinion this command is seriously lacking flexibility and I am clueless as why would anyone use it. Maybe it has some advantage which I have yet to see. By using a while or for command a set of instructions will run only if certain conditions are met. By using the do command the set of instructions will run at least once and then the program will check if the conditions are met to run the set of instructions again.
Here
is an example in pseudocode of what I am talking about.
|
While
or for command.
Print “ I think I will go swimming" |
|
Do
command. Print
“ I think I wll go swimming” While/for tamparture > 80. |
Lets see the do command at work.
program 1.11
#include <iostream.h>
int main () {
int temp;
do
{
cout << "What is the temp today?" << endl;
cin >> temp;
cout << " I think I will go for swimming.";
} while (temp>80);
// Line 9
return 0;
}
Output.1.11
| What is the temp today? 75 I think I will go for swimming. Press any key to continue |
As you can see here, in
line 9 we have set a condition while (temp>80);
In the output you see that the computer did not check first to see if the
variable temp is larger than 80 and it just went and printed I think I will go
for swimming. Then the computer went on line 9 and checked the condition and saw
that 75 (the number we assinged to the variable) is smaller than 80 and it
canceled out. If we were to enter a number greater than 80 the program would
simply go on looping.
Just remember, the do command is the "shoot first ask later who deserved it" command.
Go to: if - if else - switch - continue
Back home next view contents in a pop up window