| Loops
How are you doing so far? Hang in there!
do-while
Let’s get a little complex now. We will talk about loops
here. Loops, in another word, are iterations. There are three
types of loops that we will focus on. First, let’s take a look
at the do-while statements.
do-while statements ask the program to do certain tasks
repeatedly while a condition is met. When the condition is
false, the program will run the tasks once and does not
repeat.
For example, have the program calculate addition between two
numbers. Ask the user if he wants to calculate another problem. Do
the addition repeated while the user answers y.
#include <iostream.h>
int main()
{
char YorN;
do{
int FirstInteger,
SecondInteger;
cout << "Enter an integer: ";
cin >>
FirstInteger;
cout << "Enter another integer: ";
cin >>
SecondInteger;
cout << "The sum is " << FirstInteger +
SecondInteger << endl;
cout << "Do you want to calculate another addition
problem?"
cin >>
YorN;
}while(YorN = = ‘Y’);
return(0);
}
while
Another type of loop that is used is the while statement.
How is it different from the do-while statement? Well, in
the do-while statement, the tasks within that statement are
performed at least once. In the while statement, the condition
MUST be met, before the tasks can be done. If after performing the
tasks, the result still meets the condition of the while
statement, the tasks are repeated using the result.
Let’s have the program ask the user to enter a number less
than or equal to 100. While the number is greater than 100,
display "The number is too large. Try again." Then, have
the user enter the number again. The loop will repeat as long as
the user keeps entering a number greater than 100. If the user
enters a number less than or equal to 100, the loop will
terminate. The while statement will be skipped if the user enters
a number less than or equal than 100 in the first place. Here’s
the code for the program:
#include <iostream.h>
int main()
{
int Number;
cout << "Enter a number less than or equal to 100:
";
cin >> Number;
while(Number > 100){
cout << "The number is too large. Try again.";
cin >> Number;
}
return(0);
}
for
Now you already have a taste of what loops are like. We will
introduce you to a new type of loop called the for loop. In
a for loop, you can specify the number of times you want the loop
to iterate.
Here’s the format to set up the for loop:
for(start; condition; increment/decrement)
assignment statements;
Let’s create our own for loop that displays all the numbers
from 5 to 16.
for(int x = 5; x <=16; x++)
cout << x << endl;
Above we initiated the variable x with 5. The loop will repeat
as long as x is less than or equal to 16. x increments by one each
time. Thus, the numbers 5 to 16 are displayed on the screen.
Break Statements
Do you recall the break statement from the switch statements?
If you want to put an end to a certain section of the code in your
program, or if you want to terminate a loop, you can add in a
break statement.
All you need to do is type break;
within the section of the code that you want to end.
For example, you suddenly want to stop the program. It requires
you to press q to quit. When you press q, the programmer could
probably have put a break statement in the code so that you can
terminate the program. Let’s take a look at the code:
char Key;
cout << "Press any key to calculate, or q to quit
the program.";
cin >> Key;
while(Key != ‘q’){
cout << "Press any key to start calculating or q to
quit.";
cin >> Key;
if(Key = = ‘q’)
break;
int Number, Number2;
cout << "Enter a number: ";
cin >> Number;
cout << "Enter another number: ";
cin >> Number2;
cout << "The sum of the two numbers is "
<< Number + Number2
<< endl;
}
Algorithms
We all want to make life easier, even programmers do. Before
touching upon anything complex, it is always good to have a plan
at hand. In C++, such a plan is called an algorithm. An algorithm
consists of the procedures to write the code. It is like a rough
draft of the code for the program. If you list out your procedures
with words and C++ code, that type of algorithm is a pseudocode.
NEXT |