Sahar Mosleh California State University San MarcosPage 1 While Loop and For Loop.
-
date post
21-Dec-2015 -
Category
Documents
-
view
225 -
download
1
Transcript of Sahar Mosleh California State University San MarcosPage 1 While Loop and For Loop.
Sahar Mosleh California State University San Marcos Page 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
Sahar Mosleh California State University San Marcos Page 3
While Loops
while (logical expression)Statement;
Next-Statement;
Is logical expression true
Execute statement
Execute next statement
NoYes
Sahar Mosleh California State University San Marcos Page 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)
Sahar Mosleh California State University San Marcos Page 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:
Sahar Mosleh California State University San Marcos Page 6
Count Controlled While
int ticket_ count = 0;
while (ticket_ count < 10) {
keep parking illegallyif (I get a ticket) {
ticket_count++;}
}
Loop controlled by counterthat must be given an initialvalue
Counter is checked at“top” of loop. If conditiontrue, body of loop isexecuted
Can “nest” constructswithin other constructs
Sahar Mosleh California State University San Marcos Page 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?
< See: Example 1>
Sahar Mosleh California State University San Marcos Page 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:
Sahar Mosleh California State University San Marcos Page 9
Event Controlled While
bool LA = false;
while (! LA) {keep drivingif (I am in LA) {
LA = true;}
}
Loop controlled by Booleanthat must be given an initialvalue
Boolean is checked at“top” of loop. If conditionis true, body of loop isexecuted
Sahar Mosleh California State University San Marcos Page 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
Sahar Mosleh California State University San Marcos Page 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
Sahar Mosleh California State University San Marcos Page 12
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;
}}< See: Example 2>
This input statement checks to see if the file is empty
If there is nothing more toread (EOF), then infile willeffectively be false and theloop will soon terminate
Sahar Mosleh California State University San Marcos Page 13
End of File#include <iostream>#include <fstream>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;
}}
< See: Example 3>
Immediately after the open:infile is true if open successfulinfile is false if open unsuccessful
Sahar Mosleh California State University San Marcos Page 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;
}
< See: Example 4>
Sahar Mosleh California State University San Marcos Page 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
Sahar Mosleh California State University San Marcos Page 16
While
Recall the while construct:
stmt1;while (expr1) {
stmt1;stmt2;stmt3;stmtn;
}. . .
initialization
Anything to change the Statusof expre1 in the while (ex: increment)
continuation
Sahar Mosleh California State University San Marcos Page 17
For
The for repetition construct:
for (expr0; expr1; expr2) {stmt1;stmt2;stmt3;… ;.. ;stmtn;
}
The for loop is controlled by threestatements / expressions
initialization increment continuation
Sahar Mosleh California State University San Marcos Page 18
For
- A compact counted loop:
for (count= 1; count <= 10; count++) {stmt1;stmt2;stmt3;…. ;…. ;stmtn;
}
initializationincrementcontinuation
How many times willthis loop execute?
Sahar Mosleh California State University San Marcos Page 19
For
When it is known “ahead of time” how manytimes 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 20
For (example)
Write a for loops to print stars as follows:
* ****************************************
Sahar Mosleh California State University San Marcos Page 21
For
int i, stars;
// printing the first 5 linesfor (stars= 1; stars<= 9; stars= stars+ 2){ for (i= 1; i<= stars; i++)
cout << ‘* ’;cout << endl;
}
// printing the last 4 linesfor (stars= 7; stars>= 1; stars= stars- 2) {
for (i= 1; i<= stars; i++)cout << ‘* ’;
cout << endl ; }