1 / 49

Programming in C++

Andreas Savva. Programming in C++. Lecture Notes 3 Loops (Repetition). Structures. Sequential Branching Repeating. Loops. Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary. The teacher of physical education said:.

shilah
Download Presentation

Programming in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Andreas Savva Programming inC++ Lecture Notes 3 Loops (Repetition)

  2. Structures • Sequential • Branching • Repeating

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

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

  5. Loops while() do – while() for( ; ; )

  6. Condition false true Loop Body Thewhile()Loop while (<Condition>) <Loop body> ;

  7. The while() Loop while (<Condition>) <Statement> ; while (<Condition>) { <Statement 1> ; <Statement2> ; . . . }

  8. * *** ***** * * * *** ***** * * * *** ***** * * * *** ***** * * #include <iostream> using namespace std; void main() { cout << ” * \n”; cout << ” *** \n”; cout << ”*****\n”; cout << ” * \n”; cout << ” * \n”; cout << endl; } * *** ***** * *

  9. Variable i false true Screen #include <iostream> using namespace std; void main() { int i = 1; while (i <= 4) { cout << ” * \n”; cout << ” *** \n”; cout << ”*****\n”; cout << ” * \n”; cout << ” * \n”; cout << endl; i++; } } * *** ***** * * * *** ***** * * * *** ***** * * * *** ***** * * 1 2 3 4 5

  10. Variable i #include <iostream> using namespace std; void main() { int i = 1; while (i < 11) { i += 3; cout << i << endl; } } 1 10 13 4 7 Screen 4 7 10 13

  11. 1 2 3 4 5 i i i i i Examples void main() { int i = 1; while (i<=5) { cout << i << ’ ’; i++; } } void main() { int i = 1; while (i<=5) { cout << ’i’ << ’ ’; ++i; } }

  12. 1 1 1 1 1 1 … 1 2 3 4 5 More Examples void main() { int i = 1; while (i<=5) cout << i << ’ ’; i++; } void main() { int i = 1; while (i<=5) cout << i++ << ’ ’; }

  13. 2 3 -6 -999 2 3 -6 2 3 -6 -999 -999 More Examples void main() { int x; while (cin >> x, x!=-999) cout << x << ’ ’; } void main() { int x; while (cin >> x, x!=-999); cout << x << ’ ’; }

  14. 2 3 4 5 2 3 4 More Examples void main() { int i = 1; while (i++ < 5) cout << i << ’ ’; } void main() { int i = 1; while (++i < 5) cout << i << ’ ’; }

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

  16. Exercises Write a program to display the numbers from 1 to 100 inclusive. Write a program to display all the letters of the Latin alphabet. Write a program to calculate the average of the integer numbers between 15 and 25, inclusive. Write a program to display the odd numbers from 1 to 101, and also to display their sum. 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. Thepowersof 2 are: 1, 2, 4, 8, 16, 32, … . Write a program to display the first power of 2, which is bigger than 1000. Write a program to calculate the sum of: 12 + 22 + 32 + … + Ν2.

  17. 12345 12345 12345 12345 12345 Nested Loops Example 1 #include <iostream> 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++; } }

  18. 11111 22222 33333 44444 55555 Nested Loops Example 2 #include <iostream> 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++; } }

  19. ABCDEF ABCDEF ABCDEF ABCDEF ABCDEF ABCDEF Nested Loops Example 3 #include <iostream> 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++; } }

  20. ABCDEF BCDEF CDEF DEF EF F Nested Loops Example 4 #include <iostream> 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++; } }

  21. A AB ABC ABCD ABCDE ABCDEF Nested Loops Example 5 #include <iostream> 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++; } }

  22. * * * * * * * * * * * * * 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++; } }

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

  24. Loop Body Condition true false Thedo–while()Loop do <Loop body> ; while (<Condition>);

  25. The do – while() Loop do <Statement> ; while (<Condition>) ; do { <Statement 1> ; <Statement2> ; . . . } while(<Condition>) ;

  26. Variable i Screen #include <iostream> using namespace std; void main() { int i = 1; do { cout << ” * \n”; cout << ” *** \n”; cout << ”*****\n”; cout << ” * \n”; cout << ” * \n”; cout << endl; i++; } while(i <= 4); } * *** ***** * * * *** ***** * * * *** ***** * * * *** ***** * * 1 2 3 4 5

  27. Variable i #include <iostream> using namespace std; void main() { int i = 1; do { i += 3; cout << i << endl; } while(i < 11); } 1 10 13 4 7 Screen 4 7 10 13

  28. Thefor( ; ; )Loop for (<init> ;<condition>;<change>) <Loop body> ; 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 conditionfalse and the loop will terminate. Executed at every pass after the execution of the loop body.

  29. 1 2 3 4 5 6 Example 1 void main() { int i; for(i=1; i<7; i++) cout << i << ” ”; }

  30. 1 2 3 4 5 6 Example 2 void main() { int i; for(i=1; i<7; ++i) cout << i << ” ”; }

  31. 2 3 4 5 6 7 Example 3 void main() { int i; for(i=1; i++<7; ) cout << i << ” ”; }

  32. 2 3 4 5 6 Example 4 void main() { int i; for(i=1; ++i<7; ) cout << i << ” ”; }

  33. 9 8 7 6 Example 5 void main() { int i; for(i=9; i>5; --i) cout << i << ” ”; }

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

  35. 1 12 123 1234 12345 123456 1234567 Nested For-Loops Example 1 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; }

  36. *123456 2*12345 33*1234 444*123 5555*12 66666*1 777777* 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 << ’*’; elseif (i>j) cout << i; else cout << j-i; cout << endl; } return 0; }

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

  38. Endless Loop #include <iostream> using namespace std; void main() { for( ;; ) ; }

  39. M T W T F S S -------------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Exercise #include <iostream> #include <iomanip> 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; }

  40. The breakand continuestatements • 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.

  41. 1 2 3 1 2 3 5 6 7 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 << ” ”; } }

  42. Memory Scope • C++ is a block language. { … } • Variables declared in a block are called local variables. • Variables declared outside the block, but not within another inner block, are called global variables. { 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

  43. Scope Example #include <iostream> 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; } 8 17 8

  44. Local and Global Variables #include <iostream> using namespace std; float max; int main() { int x, y; ... { int y, z; ... } { char ch; ... { float x, p; ... } } return 0; } 1 Local Global max 1 2 x, y max 2 y, z x, max 3 3 ch x, y, max 4 x, p ch, y, max 5 4 5

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

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

  47. Random Number Generation num = rand(); • Library: stdlib.h • Generates unsigned integer between 0 and RAND_MAX (usually 32767). • 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

  48. 2 3 4 5 6 6 2 5 1 2 Random Numbers #include <iostream> using namespace std; #include <stdlib.h> #include <time.h> void main(){ int i; srand(time(0)); for(i=0; i<10; i++) cout << 1+rand()%6 << ’ ’; } Returns the current time in seconds

More Related