While Loop and For Loop Sahar Mosleh California

  • Slides: 21
Download presentation
While Loop and For Loop Sahar Mosleh California State University San Marcos Page 1

While Loop and For Loop Sahar Mosleh California State University San Marcos Page 1

While - Programs are very good at performing similar tasks over and over -

While - Programs are very good at performing similar tasks over and over - In a payroll program, printing thousands of cheques involves a similar activity being repeated thousands of times - Repetition in programming often called looping - Repetition in C++ can be handled by the “while” looping construct Sahar Mosleh California State University San Marcos Page 2

While Loops Is logical expression true while (logical expression) Statement; Next-Statement; Yes No Execute

While Loops Is logical expression true while (logical expression) Statement; Next-Statement; Yes No Execute statement Execute next statement Sahar Mosleh California State University San Marcos Page 3

While Loops - Often used a compound statement in a while loop while (logical

While Loops - Often used a compound statement in a while loop while (logical expression) { stmt 1; stmt 2; ……. As long as the logical expression stmt n; is true, statements within the } block will execute repeatedly (checks before each iteration) Sahar Mosleh California State University San Marcos Page 4

Count Controlled While As long as I have less than 10 parking tickets, I

Count Controlled While As long as I have less than 10 parking tickets, I will continue to park illegally - This is a count controlled loop - An activity will continue to take place until a specific count is reached - Could be pseudo-coded as follows: Sahar Mosleh California State University San Marcos Page 5

Count Controlled While int ticket_ count = 0; while (ticket_ count < 10) {

Count Controlled While int ticket_ count = 0; while (ticket_ count < 10) { keep parking illegally if (I get a ticket) { ticket_count++; } } Sahar Mosleh Loop controlled by counter that must be given an initial value Counter is checked at “top” of loop. If condition true, body of loop is executed Can “nest” constructs within other constructs California State University San Marcos Page 6

Count Controlled While int count = 1; // Print out “Hello World!” 10 times

Count Controlled While int count = 1; // Print out “Hello World!” 10 times while (count <= 10) { cout << “Hello World!” << endl; count++; } - What would happen if count was set to 0 initially? < See: Example 1> Sahar Mosleh California State University San Marcos Page 7

Event Controlled While As long as I am not in LA, I will continue

Event Controlled While As long as I am not in LA, I will continue to drive - This is an event controlled loop An activity will continue to take place until a specific event occurs - Could be pseudo- coded as follows: Sahar Mosleh California State University San Marcos Page 8

Event Controlled While bool LA = false; while (! LA) { keep driving if

Event Controlled While bool LA = false; while (! LA) { keep driving if (I am in LA) { LA = true; } } Loop controlled by Boolean that must be given an initial value Boolean is checked at “top” of loop. If condition is true, body of loop is executed Sahar Mosleh California State University San Marcos Page 9

Event Controlled While There can be many events that control a while loop –

Event Controlled While There can be many events that control a while loop – One of the most common is the “end of file” (EOF) event Sahar Mosleh California State University San Marcos Page 10

End of File - Input file streams generally have a special EOF character at

End of File - Input file streams generally have a special EOF character at the very end - When a program encounters this character, the EOF condition is raised - This is often an event that stops the loop - When reading an input file stream like cin or fin, the name of the file stream can be used as if it were a Boolean - If the last input operation was successful it will be true -- false otherwise Sahar Mosleh California State University San Marcos Page 11

End of File #include <iostream> #include <fstream> using namespace std; int main() { ifstream

End of File #include <iostream> #include <fstream> using namespace std; int main() { ifstream infile; int value; infile. open(“myfile. txt”); infile >> value; while (infile) { cout << value << endl; infile >> value; } This input statement checks to see if the file is empty If there is nothing more to read (EOF), then infile will effectively be false and the loop will soon terminate } < See: Example 2> Sahar Mosleh California State University San Marcos Page 12

End of File #include <iostream> #include <fstream> using namespace std; int main() { Immediately

End of File #include <iostream> #include <fstream> using namespace std; int main() { Immediately after the open: ifstream infile; infile is true if open successful int value; infile is false if open unsuccessful infile. open(“myfile. txt”); if (! infile) {cout << “Oh no: (can’t open file!)” << endl; return; } infile >> value ; while (infile) { cout << value << endl; infile >> value; } } < See: Example 3> Sahar Mosleh California State University San Marcos Page 13

End of File - This program can also be written as follows: ifstream infile;

End of File - This program can also be written as follows: ifstream infile; int value; infile. open(“myfile”); if ( ! infile){ cout << “Oh no: (can’t open file!)”; return; } infile >> value; while ( ! infile. eof() ) { cout << value; infile >> value; } < See: Example 4> Sahar Mosleh California State University San Marcos Page 14

While - Whiles are often used as “undetermined” loops - Not sure how long

While - Whiles are often used as “undetermined” loops - Not sure how long a while loop will last - When will EOF be found? - Very often, it is known exactly how long a while loop last - Perhaps an idea to use a more deterministic looping construct Sahar Mosleh California State University San Marcos Page 15

While Recall the while construct: stmt 1; while (expr 1) { stmt 1; stmt

While Recall the while construct: stmt 1; while (expr 1) { stmt 1; stmt 2; stmt 3; stmtn; }. . . Sahar Mosleh initialization continuation Anything to change the Status of expre 1 in the while (ex: increment) California State University San Marcos Page 16

For The for repetition construct: initialization increment continuation for (expr 0; expr 1; expr

For The for repetition construct: initialization increment continuation for (expr 0; expr 1; expr 2) { stmt 1; stmt 2; The for loop is controlled by three statements / expressions stmt 3; …; . . ; stmtn; } Sahar Mosleh California State University San Marcos Page 17

For - A compact counted loop: initialization continuation increment for (count= 1; count <=

For - A compact counted loop: initialization continuation increment for (count= 1; count <= 10; count++) { stmt 1; stmt 2; stmt 3; How many times will this loop execute? …. ; stmtn; } Sahar Mosleh California State University San Marcos Page 18

For When it is known “ahead of time” how many times to loop, consider

For When it is known “ahead of time” how many times to loop, consider a for loop. For example, - Adding marks of 100 students - Getting exactly 10 numbers from the user - Reading the first 5 numbers from an input file Sahar Mosleh California State University San Marcos Page 19

For (example) Write a for loops to print stars as follows: * ********** *

For (example) Write a for loops to print stars as follows: * ********** * Sahar Mosleh California State University San Marcos Page 20

For int i, stars; // printing the first 5 lines for (stars= 1; stars<=

For int i, stars; // printing the first 5 lines for (stars= 1; stars<= 9; stars= stars+ 2) { for (i= 1; i<= stars; i++) cout << ‘* ’; cout << endl; } // printing the last 4 lines for (stars= 7; stars>= 1; stars= stars- 2) { for (i= 1; i<= stars; i++) cout << ‘* ’; cout << endl ; } Sahar Mosleh California State University San Marcos Page 21