Download presentation
Presentation is loading. Please wait.
1
Sahar Mosleh California State University San MarcosPage 1 While Loop and For Loop
2
Sahar Mosleh California State University San MarcosPage 2 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
3
Sahar Mosleh California State University San MarcosPage 3 While Loops while (logical expression) Statement; Next-Statement; Is logical expression true Execute statement Execute next statement No Yes
4
Sahar Mosleh California State University San MarcosPage 4 While Loops - Often used a compound statement in a while loop while (logical expression) { stmt 1; stmt 2; ……. stmt n; } As long as the logical expression is true, statements within the block will execute repeatedly (checks before each iteration)
5
Sahar Mosleh California State University San MarcosPage 5 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:
6
Sahar Mosleh California State University San MarcosPage 6 Count Controlled While int ticket_ count = 0; while (ticket_ count < 10) { keep parking illegally if (I get a ticket) { ticket_count++; } 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
7
Sahar Mosleh California State University San MarcosPage 7 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?
8
Sahar Mosleh California State University San MarcosPage 8 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:
9
Sahar Mosleh California State University San MarcosPage 9 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
10
Sahar Mosleh California State University San MarcosPage 10 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
11
Sahar Mosleh California State University San MarcosPage 11 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
12
Sahar Mosleh California State University San MarcosPage 12 End of File #include 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
13
Sahar Mosleh California State University San MarcosPage 13 End of File #include using namespace std; int main() { ifstream infile; int value; infile.open(“myfile.txt”); if (! infile) {cout << “Oh no: (can’t open file!)” << endl; return;} infile >> value ; while (infile) { cout << value << endl; infile >> value; } Immediately after the open: infile is true if open successful infile is false if open unsuccessful
14
Sahar Mosleh California State University San MarcosPage 14 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; }
15
Sahar Mosleh California State University San MarcosPage 15 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
16
Sahar Mosleh California State University San MarcosPage 16 While Recall the while construct: stmt1; while (expr1) { stmt1; stmt2; stmt3; stmtn; }... initialization Anything to change the Status of expre1 in the while (ex: increment) continuation
17
Sahar Mosleh California State University San MarcosPage 17 For The for repetition construct: for (expr0; expr1; expr2) { stmt1; stmt2; stmt3; … ;.. ; stmtn; } The for loop is controlled by three statements / expressions initialization increment continuation
18
Sahar Mosleh California State University San MarcosPage 18 For - A compact counted loop: for (count= 1; count <= 10; count++) { stmt1; stmt2; stmt3; …. ; stmtn; } initialization increment continuation How many times will this loop execute?
19
Sahar Mosleh California State University San MarcosPage 19 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
20
Sahar Mosleh California State University San MarcosPage 20 For (example) Write a for loops to print stars as follows: * *** ***** ******* ********* ******* ***** *** *
21
Sahar Mosleh California State University San MarcosPage 21 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 ; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.