For Loop Repetitive statement You can repeatedly execute



























- Slides: 27

For Loop (Repetitive statement) You can repeatedly execute a sequence of code by creating a loop. C++ supplies a powerful assortment of loop constructs. The one we will look at here is the for loop. The simplest form of the for loop is shown here: for (initialization; condition; increment) statement; • Initialization: sets a loop control variable to an initial value. • Condition: is an expression that is tested each time the loop repeats. As long as condition is true (nonzero), the loop keeps running. • Increment: is an expression that determines how the loop control variable is incremented each time the loop repeats.


1, 2, 3, 4, 5, 6, 7, 8, 9, End! example #include <iostream> void main () { for (int n=1; n<10; n++) { cout << n << ", "; } cout << “End!n"; }

This program uses a for loop to display the number 1 -10 and their squares. #include <iostream> using namespace std; void main() { int num; cout << "Number Squaredn"; cout << "-------------n"; for (num = 1; num <= 10; num++) cout << num << "tt" << num * num << endl; } Program Output Number Squared ------------1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100


#include<iostream> using namespace std; void main() { for (int i = 0; i <= 11; i++) cout << "*" <<" "; cout << endl; system("pause"); }

#include<iostream> using namespace std; void main() { int i; for (i = 0; i <= 10; i++) { cout << i<< endl; } system("pause"); }

include<iostream> using namespace std; void main() { int i; for (i = 0; i <= 10; i=i+2) cout <<I <<” , ”; } cout << endl; system("pause"); } {

#include<iostream> using namespace std; void main() { int i; int sum = 0; for (i = 0; i <= 10; i=i+2) { sum = sum + i; } cout << sum << endl; system("pause"); }

This program uses a for loop to display the number 1 -10 and their squares. #include <iostream> using namespace std; void main() { int num; cout << "Number Squaredn"; cout << "-------------n"; for (num = 1; num <= 10; num++) cout << num << "tt" << (num * num) << endl; }

#include <iostream> using namespace std; void main() { int num; int fact = 1; cout << "the Factor of Number: "; cin >> num; for (int i = 1; i < num; i++) fact = fact*i; cout << "the Factor of Number of " << num << " is = " << fact << endl; system("pause"); }

#include <iostream> using namespace std; void main() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) cout << "* "; } cout << endl; system("pause"); }

#include <iostream> using namespace std; void main() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { cout << "* "; } cout << endl; } system("pause"); }

#include <iostream> using namespace std; void main() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) cout << i; cout << endl; } system("pause"); }

#include <iostream> using namespace std; void main() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) cout << j; cout << endl; } system("pause"); }

#include <iostream> using namespace std; void main() { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) cout << i*j<<"t"; cout << endl; } system("pause"); }

#include <iostream> using namespace std; void main() { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { cout << "*"<<"t"; } cout << endl; } system("pause"); }

#include <iostream> using namespace std; void main() { for (int i = 4; i >= 1; i--) { for (int j = 1; j <= i; j++) {cout << "*"<<"t"; } cout << endl; } system("pause"); }

1. What does the if statement do? 2. What does the for statement do? 3. What are C++’s relational operators? Answer Key: 1. if is C++’s conditional statement. 2. The for is one of C++’s loop statements. 3. The relational operators are ==, !=, <, >, <=, and >=. 1. How is a block of code created? 2. In C++, statements are terminated by a ______. 3. All C++ statements must start and end on one line. True or false? Answer Key: 1. A block is started by a {. It is ended by a }. 2. semicolon 3. False. Is the following assignment statement valid or invalid? If it is invalid, why? 72 = amount; What will the following program segment display? int a = 0, b = 2, x = 4, y = 0; cout << (a == b) << endl; Why does this program not compile? Show what must be done to fix it?

Jump statements. break statement Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end. For example, we are going to stop the count up before its natural end (maybe because of an engine check failure? ):

1, 2, 3, 4, 5, countdown aborted! example / #include <iostream. h> using namespace std; void main () { int n; for (n=1; n<10; n++) { cout << n << ", "; if (n==6) { cout << "countdown aborted!"; break; } } }

The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration

10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE! example #include <iostream. h> using namespace std; void main () { for (int n=10; n>0; n--) { if (n==5) continue; cout << n << ", "; } cout << "FIRE!n"; }

The goto statement goto allows to make an absolute jump to another point in the program. You should use this feature with caution since its execution causes an unconditional jump ignoring any type of nesting limitations. • A program is a set of instructions a computer follows in order to perform a task. • A programming language is a special language used to write computer programs.

example #include <iostream> Using namespace std; void main () { int n=10; loop: cout << n << ", "; n--; if (n>0) goto loop; cout << "FIRE!n"; } / 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!

• This program uses an if/else if statement to assign a letter grade (A, B, C, D, or F) to a numeric test score. • Number or letter or symbol |3 x+2| const # define Y= (x>6) ||(y<4) ? X++; Y--; This program averages a set of test scores for multiple students. It lets the user specify how many. This program takes daily sales figures for one week 2 // and calculates their total. 3 #include <iostream> 4 #include <iomanip> 5 using namespace std; 67 int main() 8{ 9 const int NUM_DAYS = 7; // Number of days to process 10 int count; 11 double total;

#include<iostream. h> main() { int i; for ( i = 1 ; i < = 10 ; i + + ) cout<< "*"; } The Compilation Process (Diagram)