Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Notes 3 Loops (Repetition)

Similar presentations


Presentation on theme: "Lecture Notes 3 Loops (Repetition)"— Presentation transcript:

1 Lecture Notes 3 Loops (Repetition)
Andreas Savva Programming in C++ 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

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

7 The while() Loop while (<Condition>) <Loop body> ;
true false while (<Condition>) <Loop body> ;

8 The while() Loop while (<Condition>) <Statement> ;
{ <Statement 1> ; <Statement 2> ; . . . }

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

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

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

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 More Examples void main() { int i = 1; while (i<=5) 1 1 1 1 1 1 …
cout << i << ’ ’; i++; } void main() { int i = 1; while (i<=5) cout << i++ << ’ ’; }

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

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

16 Exercises What is the output of the following segments?
(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) (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’;

17 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. 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 1000. Write a program to calculate the sum of: … + Ν2.

18 Nested Loops Example 1 12345 #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++; 12345

19 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++; 11111 22222 33333 44444 55555

20 Nested Loops Example 3 ABCDEF #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++; ABCDEF

21 Nested Loops Example 4 ABCDEF BCDEF CDEF DEF EF F
#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++; ABCDEF BCDEF CDEF DEF EF F

22 Nested Loops Example 5 A AB ABC ABCD ABCDE ABCDEF
#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++; A AB ABC ABCD ABCDE ABCDEF

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

24 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++;

25 The do–while() Loop do <Loop body> ; while (<Condition>);
true false do <Loop body> ; while (<Condition>);

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

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

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

29 The for( ; ; ) 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 condition false and the loop will terminate. Executed at every pass after the execution of the loop body.

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

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

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

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

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

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

36 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; 1 12 123 1234 12345 123456

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; *123456 2*12345 33*1234 444*123 5555*12 66666*1 777777*

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

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

40 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; } M T W T F S S

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 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 Break & Continue statements
void main() { for(int i=1; i<8; i++) if (i==4) break; cout << i << ” ”; } 1 2 3 void main() { for(int i=1; i<8; i++) if (i==4) continue; cout << i << ” ”; }

43 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

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

45 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 2 3 4 5 1 2 3 4 5 Local Global max x, y max y, z x, max ch x, y, max x, p ch, y, max

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

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

48 Random Number Generation
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 num = rand();

49 Returns the current time in seconds
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


Download ppt "Lecture Notes 3 Loops (Repetition)"

Similar presentations


Ads by Google