Loops

Say you were a programmer for a major university who needed to create a program to input 3000 grades. You could sit there for hours copying and pasting the code you needed, or you could set it up so the program will do the code over and over again until it reaches a condition. This repetition of code is known as a loop. There are three types of loops in C++: Do..while, while, and for loops. Each one has its own special purposes.

A do...while loop will loop once no matter what, and then checks the condition at the bottom of the loop. The syntax for a do..loop is do at the top of the loop, the code in the middle and while at the bottom with the condition in parenthases (). The code to loop would go between the do and while while. Like the if statement, if there is more than one line to loop then you'd need to use curly braces {}, but as good practice you should use them regardless of how many lines there are.

string name;
char grade;
do
{
cout<<"\nEnter the student's last name (quit to end) ";
cin>>name;
cout<<"\n Enter the student's letter grade ";
cin>>grade;
}while(name!= "quit");



It is very important for you not to include a semicolon after the do or before the while. Also if you enter quit you still need to enter a grade to make it get to the end of the loop. To avoid this we can use an if statement after the inputting the name to check if name is equal to quit. If it is we simply use a break statement to break out of the loop (this works for all loops). We can improve it more by adding the program we made in the last tutorial, with some minor tweaking so it runs more efficiently.

string name;
char grade;
do
{
cout<<"\nEnter the student's last name (quit to end) ";
cin>>name;
if (name == "quit")
{
break;
}
cout<<"\nEnter the student's grade ";
cin>>grade;
switch(grade)
{
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
case 'd':
case 'D':
case 'f':
case 'F': cout<<"They got a "<<grade;
break;
default: cout<<"Error";
}
}while(name!= "quit");
system("PAUSE");


Of course you don't need all those case statements, but I did it to show you the different ways to write a program. Now let's look at a loop that tests at the top of the loop and if that condition is false in the beginning won't loop even once.

To do this we need a while loop. It is the same with a while loop as it is with a do loop, only with just the word while and curly braces. The following example is the same as above, only using a while loop instead of a do while loop.

string name;
char grade;
while(name!= "quit")
{
cout<<"\nEnter the student's last name (quit to end) ";
cin>>name;
if (name == "quit")
{
break;
}
cout<<"\nEnter the student's grade ";
cin>>grade;
switch(grade)
{
case 'a':
case 'A': cout<<"They got an A";
break;
case 'b':
case 'B': cout<<"They got a B";
break;
case 'c':
case 'C': cout<<"They got an C";
break;
case 'd':
case 'D': cout<<"They got an D";
break;
case 'f':
case 'F': cout<<"They got an F";
break;
default: cout<<"Error";
}
}
system("PAUSE");


The last loop in the C++ language is the for loop. A for loop is the loop you want to use when you want a count controlled loop. What I mean is say you want the loop to go through 100 times, you would use a for loop, althought you could put the counter in a a do while or a while loop, the for loop is specifically designed for this. This loop actually has three parts, the initialize statement, seperated with a semicolon the loop condition, seperated by a semicolon and the update statement, all in parenthases. Again the code to loop should all go in curly braces. The following code will loop 7 times and print the number 0-6. It stops at 6 because when it checks to see if x is less then 7, it is equal to 7 and therefore the loop stops.

for(int x=0; x<7; x++)
{
cout<<x;
}


The int x=0 is initializing x to 0 and won't be touched again, the x<7 is the condition that if it becomes false it will stop the loop, and x++ is the update statement which says everything it loops then increase x by 1.

The update statement, or counter as it is commonly called is common in not just for loops, but all loops. Say you had to average grades, you'd need a counter to count how many students you had, and then divide that number by the total of all your numbers. A total in programming would be a total in math, you simply take all the numbers and add them together. The easiest way to do this in a loop is to take a variable set at 0 and keep adding to it everything the loop goes through. In the next lesson we'll learn how we can save the student and grade they received to a file which can be opened for reading later on.

Next Section: Files
Twitter








Link Directory | link directory | GeekySpeaky: Submit Your Site! | Blog Matter

Copyright © 2004-2017 Sean Noble, All rights reserved
Read our Disclosure Policy