CP1 - Unit 11 - Loops
CP1 - Unit 11 - Loops
Introduction
Learning Objectives:
Course Materials:
11.1 while loop - Repeats a statement or group of statements while a given condition is
true. It tests the condition before executing the loop body.
Syntax:
while (condition) {
// statements/codes to be executed
}
do …while loop - Like a ‘while’ statement, except that it tests the condition at the end
of the loop body.
Syntax:
do {
// statements/codes to be executed
}
while (condition);
89
while and do…while
Let us first convert our examples in the for loop lesson to while and do…while statement.
90
while and do…while
91
while and do…while
Note: the counter or variable i in the for loop needs to be incremented inside the while
and do while loop to reach the maximum needed value. You can use another variable name for
the counter. There is also NO semicolon at the end of the while loop but, there is one at the end
of do… while loop.
We can also use the while loop in validating our data entry. For example, the only valid
entries are the numbers 1 – 5. Instead of just issuing cin >> number; we can add while or
do…while loop so the program will keep on asking the user to enter another number if a
number outside 1 – 5 was entered.
Choice [1-5]: 9
Invalid Input. Try again!
Choice[1-5]: 7
Invalid Input. Try again!
Choice[1-5]: 3
OK
int choice=0;
92
while and do…while
}
cout << "OK";
// if choices are letters from A to E. Small letters a to e are considered invalid in the code
below:
// to include small letters ( a to e ) as valid entries add the function toupper()in your
condition. This will convert temporarily the entry to capital letter.
// If the choice or values to be entered are not in sequence. Example, gender ‘M’ for
male and ‘F’ for female
93
while and do…while
while (condition) {
while (condition) {
// statement(s) of inside loop
}
// statement of outer loop
}
do {
do {
// statement of inside loop
}while(condition);
// statement of outer loop
} while(condition);
Note: there can be any type of loop nested inside any type and to any level
do{
while(condition) {
for (statement1; statement2; statement3) {
//statement inside for loop
}
// statement of inside while loop
}
// statement of outer do…while loop
} while(condition);
Example: Assuming you don’t know how many students to process. Enter their grades for each
quarter compute and display their average and then count and display how many students
passed and failed in the class.
# include<iostream>
using namespace std;
int main(){
94
while and do…while
do {
i++;
total = 0.0;
cout << "\n Enter quarterly grades of student #" <<i <<endl;
average = total/4;
cout << "\n\n Average grade: " << average;
if (average >= 75) {
pass++; //pass =pass + 1;
cout << "\n PASSED\n";
}
else {
fail++; // fail =fail + 1;
cout << "\n FAILED\n";
}
cout << "\n\n No. of students who passed: " << pass;
cout << "\n No. of students who failed: " << fail;
return 0;
}
95
while and do…while
Activities
Examine the following code. What will the code display in the monitor?
1. int count = 0;
while ( count <= 6 )
{
cout << count;
count += 2;
}
2. int count = 7;
while ( count >= 4 )
{
cout << count;
count -= 2;
}
3. int count = 1;
while ( count < 5 )
{
cout << count;
}
sum = 0;
sum = sum + I;
5. Using while statement ask the user to input a character, accept only characters ‘Y’ or ‘N’,
if other characters were inputted ask the user to enter another character again. Sample
Screen below:
96
while and do…while
Programming Exercises
Write a program that will incorporate while or do…while statements to do the following:
97
while and do…while
Online References
https://www.tutorialspoint.com/cplusplus/cpp_while_loop.htm/
https://www.cprogramming.com/tutorial/lesson3.html
http://www.cplusplus.com/doc/tutorial/control/
98