Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in C++ Lecture Notes 3 Loops (Repetition) Andreas Savva.

Similar presentations


Presentation on theme: "Programming in C++ Lecture Notes 3 Loops (Repetition) Andreas Savva."— Presentation transcript:

1 Programming in C++ Lecture Notes 3 Loops (Repetition) Andreas Savva

2 2 Structures Sequential Sequential Branching Branching Repeating Repeating

3 3 Loops Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary. Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary.

4 4 The teacher of physical education said: Run around the football-field until I tell you to stop. Run around the football-field until I tell you to stop. Run around the football-field five times. Run around the football-field five times.

5 5

6 6 Loops while() do – while() for( ; ; )

7 7 The while() Loop while ( ) while ( ) ; ; Condition LoopBody true false

8 8 The while() Loop while ( ) ; ; while ( ) { ; ;...}

9 9 #include using namespace std; void main() { cout << ” * \n”; cout << ” *** \n”; cout << ”*****\n”; cout << ” * \n”; cout << endl; } * *** ***** * *** ***** * *** ***** * *** ***** * *** ***** *

10 10 i Variable #include using namespace std; void main() { int i = 1; while (i <= 4) { cout << ” * \n”; cout << ” *** \n”; cout << ”*****\n”; cout << ” * \n”; cout << endl; i++; } * *** ***** * * * *** ***** * * * *** ***** * * * *** ***** * * true false Screen

11 11 i Variable #include using namespace std; void main() { int i = 1; while (i < 11) { i += 3; cout << i << endl; } Screen

12 12 Examples void main() { int i = 1; while (i<=5) { cout << i << ’ ’; i++; } void main() { int i = 1; while (i<=5) { cout << ’i’ << ’ ’; ++i; } i i i i i

13 13 More Examples void main() { int i = 1; while (i<=5) cout << i << ’ ’; i++; } void main() { int i = 1; while (i<=5) cout << i++ << ’ ’; } …

14 14 More Examples void main() { int x; while (cin >> x, x!=-999) cout << x << ’ ’; } void main() { int x; while (cin >> x, x!=-999); cout << x << ’ ’; }

15 15 More Examples void main() { int i = 1; while (i++ < 5) cout << i << ’ ’; } void main() { int i = 1; while (++i < 5) cout << i << ’ ’; }

16 16 Exercises (a)int i = -3; while (i != 3) { cout << i << ” ”; cout << i << ” ”; i = i + 1; i = i + 1;} (b)int i = 0, sum = 0; while (i <= 10) { sum += i; sum += i; i++; i++;} cout << ”Sum = ” << sum; (c)int i = 1; while (i++ <= 5) cout << i << ” ”; cout << i << ” ”; (d)int i = 10; while (i > 3) { cout << i << endl; cout << i << endl; i = i - 2; i = i - 2;} (e)int i = 6; while (i-- > 1) cout << i << ’\n’; cout << i << ’\n’; (f)int i = 0; while (++i < 8) cout << i; cout << i; (g)int i = -3; while (++i <= 3); cout << ’i’; cout << ’i’; 1. What is the output of the following segments?

17 17 Exercises 2. Write a program to display the numbers from 1 to 100 inclusive. 3. Write a program to display all the letters of the Latin alphabet. 4. Write a program to calculate the average of the integer numbers between 15 and 25, inclusive. 5. Write a program to display the odd numbers from 1 to 101, and also to display their sum. 6. Write a program to read the radian of a circle, check if it is bigger than zero, and if it is to calculate and display the perimeter of the circle using the formula P=2*3.14*R, where P is the perimeter and R is the radius of the circle. Otherwise it should prompt for the radius again until it is bigger than zero. 7. The powers of 2 are: 1, 2, 4, 8, 16, 32, …. Write a program to display the first power of 2, which is bigger than Write a program to calculate the sum of: … + Ν 2.

18 18 Nested Loops Example 1 #include using namespace std; void main() { int i, j, n = 5; i = 1; while (i <= n) { j = 1; while (j <= n) { cout << j; j++; } cout << endl; i++; } 12345

19 19 Nested Loops Example #include using namespace std; void main() { int i, j, n = 5; i = 1; while (i <= n) { j = 1; while (j <= n) { cout << i; j++; } cout << endl; i++; }

20 20 Nested Loops Example 3 ABCDEF #include using namespace std; void main() { char i, j, n = ’F’; i = ’A’; while (i <= n) { j = ’A’; while (j <= n) { cout << j; j++; } cout << endl; i++; }

21 21 Nested Loops Example 4 ABCDEF BCDEF CDEF DEF EF F #include using namespace std; void main() { char i, j, n = ’F’; i = ’A’; while (i <= n) { j = i; while (j <= n) { cout << j; j++; } cout << endl; i++; }

22 22 Nested Loops Example 5 A AB ABC ABCD ABCDE ABCDEF #include using namespace std; void main() { char i, j, n = ’F’; i = ’A’; while (i <= n) { j = ’A’; while (j <= i) { cout << j; j++; } cout << endl; i++; }

23 23 Nested Loops Example 6 * * * * * * * void main() { int i, j, n = 7; i = 1; while (i <= n) { j = 1; while (j <= n) { if (i==j || i+j==n+1) cout << ’*’; else cout << ’ ’; j++; } cout << endl; i++; }

24 24 Exercises #include #include using namespace std; void main() { int i=1, j, n; int i=1, j, n; cout << ”Enter n: ”; cout << ”Enter n: ”; cin >> n; cin >> n; n = 2 * n + 1; n = 2 * n + 1; while (i <= n) { while (i <= n) { j = 1; j = 1; while (j <= n) { while (j <= n) { if ((2*i == n+1) || (2*j == n+1)) if ((2*i == n+1) || (2*j == n+1)) cout << ’*’; cout << ’*’; else else cout << ’ ’; cout << ’ ’; j++; j++; } cout << endl; cout << endl; i++; i++; }} 1. What is the output of the following program if the input for n is: (a) 0(b) 1(c) 2(d) 4(e) 7

25 25 The do–while() Loop do ; ; while ( ); Condition LoopBody true false

26 26 The do – while() Loop do ; ; while ( ) ; do { ; ;... } while ( ) ;

27 27 i Variable #include using namespace std; void main() { int i = 1; do { cout << ” * \n”; cout << ” *** \n”; cout << ”*****\n”; cout << ” * \n”; cout << endl; i++; } while(i <= 4); } * *** ***** * * * *** ***** * * * *** ***** * * * *** ***** * * Screen

28 28 i Variable #include using namespace std; void main() { int i = 1; do { i += 3; cout << i << endl; } while(i < 11); } Screen

29 29 The for( ; ; ) Loop for ( ; ; ) ; ; init is usually an assignment to give a loop counter an initial value. Executed ONLY when entering the loop. Can also declare variables. condition is any statement returning an integral value and for as long as it is true, the statement will be executed. Executed at every pass. change is a statement normally to modify the loop counter, so that eventually it will make condition false and the loop will terminate. Executed at every pass after the execution of the loop body.

30 30 Example void main() { int i; for(i=1; i<7; i++) cout << i << ” ”; }

31 31 Example void main() { int i; for(i=1; i<7; ++i) cout << i << ” ”; }

32 32 Example void main() { int i; for(i=1; i++<7; ) cout << i << ” ”; }

33 33 Example void main() { int i; for(i=1; ++i<7; ) cout << i << ” ”; }

34 34 Example void main() { int i; for(i=9; i>5; --i) cout << i << ” ”; }

35 35 while equivalent of for e1; while(e2) { s; e3; } for(e1; e2; e3) s; same as for (i=0; i<5; i++) cout << i; i=0; while (i<5) { cout << i; i++; } same asExample

36 36 Nested For-Loops Example int main() { int i, j, n = 7; for(i=1; i<=n; i++){ for(j=1; j<=i; j++) cout << j; cout << endl; } return 0; }

37 37 Nested For-Loops Example 2 * * * * * * * int main() { int n = 7; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) if (i==j) cout << ’*’; else if (i>j) cout << i; else cout << j-i; cout << endl; } return 0; }

38 38 The power of C++ #include using namespace std; void main() { int x; for(cout > x, x!=-999 ; ) ; } #include using namespace std; void main() { int x; for( ; cout > x, x!=-999 ; ) ; } Enter x: Enter x: 6 Enter x: 3 Enter x: 7 Enter x: -999

39 39 Endless Loop #include using namespace std; void main() { for( ; ; ) ; }

40 40 Exercise M T W T F S S #include void main() { printf(’ M T W T F S S’); printf(’ ’); for (int day=1; day<=31; day++) if (day % 7 == 0) cout << setw(3) << day << endl; else cout << setw(3) << day; }

41 41 The break and continue statements A break statement is used to “break” out of a loop or a switch statement. When it is executed, it causes the flow of control to immediately exit the innermost switch statement or loop. A break statement is used to “break” out of a loop or a switch statement. When it is executed, it causes the flow of control to immediately exit the innermost switch statement or loop. A continue statement can only be used inside loops and it causes the execution to skip to the end of the loop, ready to start a new insertion. A continue statement can only be used inside loops and it causes the execution to skip to the end of the loop, ready to start a new insertion.

42 42 Break & Continue statements void main() { for(int i=1; i<8; i++) { if (i==4) break; cout << i << ” ”; } void main() { for(int i=1; i<8; i++) { if (i==4) continue; cout << i << ” ”; }

43 Memory Scope C++ is a block language. { … } C++ is a block language. { … } Variables declared in a block are called local variables. Variables declared in a block are called local variables. Variables declared outside the block, but not within another inner block, are called global variables. Variables declared outside the block, but not within another inner block, are called global variables. 43 { int x; const double pi = 3.14; x = 1; { int p = 4; x = p; cout << x << p; } int y = 12; cout << x << y << pi; } x pi p y

44 Scope Example 44 #include using namespace std; int main() { int x = 5, y = 3; cout << x + y << endl; { int y = 12; cout << x + y << endl; } cout << x + y << endl; return 0; }

45 Local and Global Variables 45 #include using namespace std; float max; int main() { int x, y;... { int y, z;... } { char ch;... { float x, p;... } return 0; } LocalGlobal max x, ymax y, zx, max chx, y, max x, pch, y, max

46 Scope 46 #include using namespace std; int main() { int i; for(i=0; i<10; i++) { cout << i; } cout << i; return 0; } #include using namespace std; int main() { for(int i=0; i<10; i++) { cout << i; } cout << i; return 0; } Correct Wrong undeclared identifier for(int i=0; i<10; i++) cout << i; Wrong i i

47 Be Careful 47 #include using namespace std; int main() { int i; while(i=0, i<10) { cout << i; i = i + 1; } return 0; }

48 48 Random Number Generation Library: stdlib.h Library: stdlib.h Generates unsigned integer between 0 and RAND_MAX (usually 32767). Generates unsigned integer between 0 and RAND_MAX (usually 32767). Used in games. Used in games.Examples: i = rand() % 6; // generates a number between 0 and 5 i = 1 + rand() % 6; // generates a number between 1 and 6 num = rand();

49 49 Random Numbers #include using namespace std; #include void main(){ int i; srand(time(0)); for(i=0; i<10; i++) cout << 1+rand()%6 << ’ ’; } Returns the current time in seconds


Download ppt "Programming in C++ Lecture Notes 3 Loops (Repetition) Andreas Savva."

Similar presentations


Ads by Google