Programming Language Control Structures: Repetition (while) Eng. Anis Nazer Second Semester 2017-2018
Repetition statements Control statements change the order which statements are executed Selection : some statements are executed, others are not (based on a condition) Repetition: statements are executed repeatedly Why do we need repetition: You want to find the average of 5 grades? You want to find the average of 10 grades? You want to find the average of 100 grades?...?
Repetion statements Write a program to calculate the average of 5 grades We can repeatedly write the statements: int a,b,c,d,e; double average; cout << "Enter five grades:"; cin >> a ; cin >> b ; cin >> c ; cin >> d ; cin >> e ; average = static_cast<double>(a+b+c+d+e)/5; cout << "Average = " << average; write a similar program for 100 grades?
Repetition writing statements repeatedly is not practical, why? size, effort, programmer time, debugging, maintenance In C++, we have 3 repetition statements while for do/while
while Operation flow chart of a while loop : Condition true Statement(s) false
form 1 while statement syntax while ( condition ) Statement
form 2 while statement syntax while ( condition ) Statements... while body
while Operation The statements inside the while statement are called "while body" Operation: The condition is checked first if the condition is true, the while body is executed after the body is executed, the condition is checked again if the condition is true, the while body is executed again... the process is repeated in other words, while the condition is true, the computer keeps repeating the statements
while statement Example: what is the output int c=0; while ( c <= 8 ) cout << c << " "; c++; cout << "bye";
while statement variable c is called Loop Control Variable (LCV) Notes: if the condition does not become false, we have an infinite loop... logical error we have to make sure that the condition at some point becomes false if c++ is removed, we have an infinite loop :)
General structure: while statement //... initialize loop variable while ( condition ).. //... update loop variable..
while statement Example: Write a loop that will display your name 99 times on the screen? int i=0; // initialize LCV while ( i < 99 ) cout << "Anis"; i++; // update LCV
while statement What is the output? int c = 0; while ( c <= 8 ) c++; cout << c << " "; cout << "bye";
while statement What is the output? int c = 0; while ( c <= 8 ) cout << c << " "; cout << "bye"; infinite loop
Examples Example: what is the output int c = 1, s = 0; while (c <= 5) s = s + c; c++; cout << s << endl << c << endl;
Examples Example: what is the output int c = 1, s = 0, g; while (c <= 5) cin >> g; s = s + g; c++; cout << s << endl << c << endl;
while example Write a program that reads 10 grades and displays their average. Questions: Variables? grade, sum, average... others? steps? read grade 1 then add to sum read grade 2 then add to sum read grade 10 then add to sum display average = sum /10
Example: grade average Start sum = 0 count = 0 count < 10 true Input grade add grade to sum increment counter false calculate average display average End
Example: Grade average int grade, sum = 0, count = 0; double average; cout << "Enter 10 grades:"; while ( count < 10 ) cin >> grade; sum = sum + grade; count++; average = static_cast<double>(sum) / 10; cout << "Average = " << average;
Example: Grade average V2 modify the program to exclude invalid grades int grade, sum = 0, count = 0; double average; cout << "Enter 10 grades:"; while ( count < 10 ) cin >> grade; if ( grade >= 0 && grade <= 100 ) sum = sum + grade; count++; else cout << "Error, invalid grade"; average = static_cast<double>(sum) / 10; cout << "Average = " << average;
loop examples Write a program that finds the average of a any number of grades. The user enters the number of grades first, then he/she enters the grades Questions: Variables: grade, sum, counter, number of grades Steps: read a number (N) read N grades and process them display average
loop example int grade, sum = 0, count = 0, N = 0; double average; cout << "Enter the number of grades:"; cin >> N; cout << "Enter " << N << " grades:\n"; while ( count < N ) cin >> grade; if ( grade >= 0 && grade <= 100 ) sum = sum + grade; count++; else cout << "Error, invalid grade"; average = static_cast<double>(sum) / N; cout << "Average = " << average;
loop example Previous program has some logical errors: What if the user enters a negative number of grades? What if the user enters a 0 for the number of grades? what are your suggestions? we can use a condition of course
Examples Example: what is the output int count = 1; int y = 10; while (count < 7) y = y 1; count++; cout << y << endl << count << endl;
int x = 1; int y = 0; while ( y <= 10 ) if ( x % 3!= 0 ) y = y + 3; else y = y 2; x++; cout << y; Examples Example: what is the output
More Notes: while statement loop control variable must be initialized before the loop, if not logical error int c; while ( c <= 20 ) cout << c << " "; c++; cout << "bye";
More Notes: while statement putting a semicolon after condition is a logical error int c = 0; while ( c <= 20 ) ; cout << c << " "; c++; cout << "bye";
loop types Loops can be classified into some types: Counter controlled : loop repeats for a certain number of times Sentinel controlled : loop repeats until the user enters a value to end the loop Flag controlled : loop repeats until a certain condition is met flag : is a boolean variable that is used to check for a certain case
loop types counter controlled: loop repeats for a certain number of times Example: Write a loop that will display your name 99 times on the screen? int i=0; // initialize LCV while ( i < 99 ) cout << "Anis"; i++; // update LCV
loop types sentinel controlled: keep repeating until user ends the loop example: keep repeating the game until the user quits...but you should never quit... :) char quit = 'n'; while ( quit!= 'y' ) // game commands cout <<"Do you want to quit " <<" (y for yes, n for no)? "; cin >> quit;
loop types flag controlled: loop repeats until a certain condition is met (flagged) example: keep guessing until the password is found bool found = false; while (! found ) // try to guess the password... if ( guess == password ) found = true;
Questions hit me...! with your questions ocourse