Back    home    next           view contents    in a pop up window

 

Go to:  if - if else - switch - continue                                                                                     

 

 The while  repetition structures.                

  So far all the programs that we wrote would perform the set of instructions and then end or they would encounter an error and cancel out. This is not practical. We want to have as much interaction as possible with a program. Here is where repetition structures come into play.

 

The while repetition structure.

      This command tells a program to execute while a certain condition is true. An example in pseusocode:  While outside temperature is above 80, print “Good time for swimming.” In this small pseusocode program, the program will print         “ Good time to go for swimming” only if the condition is met, which is while (temp>80).  You may ask "well I can do that by using the if command". The if command will not allow your program to loop. It will check to see if a certain condition is met or not, execute accordingly and then terminate. The While command will keep looping the program until a certain condition is met. Here is one simple example.

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
Please enter grade for class 1
80
Please enter grade for class 2
95
Please enter grade for class 3
65
Please enter grade for class 4
75
The average from all of you classes is: 78.75
Press any key to continue

 

The program asks us from how many courses we want to calculate the average. The variable courses is our sentinel value (line 8). Notice also in line 4 the variable loop1=0. In line 8 we tell the program that while loop1 remains less than the number of  courses, it should run the block of code between line 10 and 14. Each time though the block is executed, a value of one is added to the variable loop1 (which was set as zero in line 4). Who is responsible for this? Look at line 12. In other words each time the block of code is executed the ++ operators next to the loop variable in line 12, add the value of one to that variable. When the loop variable reaches the value of the number of courses we entered (line 7), the program will end its loop. 

 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   repetition structures.   

 

    

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.         

  While/for temparture >80

Print “ I think I will go swimming"

 

Do command.

Print “ I think I wll go swimming”

While/for tamparture > 80.

   In the first example the computer will print “ I think I will go swimming” only if the temp is higher than 80. In the second example the computer will print “ I think I will go swimming” at least once (no matter what the temp is), THEN check to see if the temp is larger than 80. If temp is less than 80 it will not print “ I think I will go swimming” again.

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

1
Hosted by www.Geocities.ws

1