the for repetition structure. syntax for (initial-expression1, conditional-expression2,...
TRANSCRIPT
![Page 1: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/1.jpg)
The For Repetition Structure
![Page 2: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/2.jpg)
Syntaxfor (initial-expression1, conditional-expression2, loop-expression3)
{ statement(s); }
Example:
for (int counter=1; counter <= 10; counter++;)
cout << counter << endl;
![Page 3: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/3.jpg)
What the for Loop Looks Like:
The C++ for loop contains four major parts:
1. The value at which the loop starts.
2. The condition under which the loop is to continue.
3. The changes that are to take place for each loop.
4. The loop instructions.
![Page 4: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/4.jpg)
Components of a typical for loop.
For Control Final value
Keyword Variable of control variable
Name
for (int counter = 1; counter <= 10; counter++)
initial value Loop increment
of control continuation of control
variable condition variable
![Page 5: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/5.jpg)
Flowchart
Counter = 1
Counter <= 10 cout << counter <<endl;
counter++;
False
True
Body of loop Increment the counter variable
![Page 6: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/6.jpg)
Example Program#include <iostream.h>
main(){
int time; // Counter variable.
// The loop starts here.
for(time = 1; time <= 5; time = time + 1)cout << "Value of time = " << time << "\n";
// The loop stops here.
cout << "This is the end of the loop.\n";}
![Page 7: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/7.jpg)
![Page 8: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/8.jpg)
More Than One Statement
You can have more than one statement in a C++ for loop.
By using
• the open braces { to indicate the beginning of the loop
• the close braces } to indicate the end of the loop.
![Page 9: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/9.jpg)
Example Program: This program computes the distance a
body falls in feet per second, for the first 5 seconds of free fall as given by the equation: s = ½ at2
Wheres = the distance in feet.a = acceleration due to gravity t = time in seconds
![Page 10: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/10.jpg)
#include <iostream.h>#define a 32.0
main(){
int time; // Counter variable.int distance; // Distance covered by the falling
// body.
![Page 11: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/11.jpg)
// The loop starts here.
for(time = 1; time <= 5; time = time + 1)
{
distance = 0.5 * a * time * time;
cout << "Distance at the end of "
<< time << " seconds is ";
cout << distance << " feet.\n";
}
// The loop stops here.
![Page 12: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/12.jpg)
cout << "This is the end of the loop.\n";
}
![Page 13: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/13.jpg)
Your output should look like:
![Page 14: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/14.jpg)
The Increment and Decrement Operators
Operator MeaningX++ Increment X after any operation with it.
(called post-incrementing).
++X Increment X before any operation with it.(called pre-incrementing).
X-- Decrement X after any operation with it. (called post-decrementing).
++X decrement X before any operation with it.(called pre-decrementing).
![Page 15: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/15.jpg)
#include <iostream.h>
main(){
int count; // Program counter.int number1 = 0; // Number to post-increment.int number2 = 0; // Number to pre-increment.
for(count = 1; count <= 5; count++){
cout << "Post-incrementing number = " << number1++ << ", ";
cout << "Pre-incrementing number = "<< ++number2 << "\n";}
}
![Page 16: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/16.jpg)
Your program should look like:
![Page 17: The For Repetition Structure. Syntax for (initial-expression1, conditional-expression2, loop-expression3) { statement(s); } Example: for (int counter=1;](https://reader031.vdocuments.us/reader031/viewer/2022020319/56649f345503460f94c50bae/html5/thumbnails/17.jpg)
For Today:
For Loop Worksheet
Homework:
Read pages 87 to 95.
Quiz on Friday regarding For Loops!