Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 14: Control Flow. The break, continue, goto statements.

Similar presentations


Presentation on theme: "Lecture 14: Control Flow. The break, continue, goto statements."— Presentation transcript:

1 Lecture 14: Control Flow. The break, continue, goto statements

2 2 Lecture Contents: t Structured Programming Basics t The break control statement t The continue control statement t The goto control statement t Nested loops t Bit wise operators t Shift left ( >) operators t Demo programs and Exercises

3 3 Structured Programming Basics Control structureC++ source statement Sequencefunction call statements, assignment statement = ; Selectionif, if … else, switch Repetitionfor, while, do … while

4 4 break, continue, goto stmts  The break control statement  Syntax: break; t Usage: –Element of case clause in switch statement –Unconditional loop exit

5 5 break, continue, goto stmts  The continue control statement  Syntax: continue; t Usage: –Skip the rest of the loop body without exiting the loop

6 6 break, continue example for (int i=1; i<=10; i++) { cout << "\nIteration Nr “ << i << “ – 1st part loop "; cout << “\nIteration Nr “ << i << “ – 2nd part loop \n"; }

7 7 break, continue example for (int i=1; i<=10; i++) { cout << "\nIteration Nr “ << i << “ – 1st part loop "; cout << “\nIteration Nr “ << i << “ – 2nd part loop \n"; if ( i==5 ) break; }

8 8 break, continue example for (int i=1; i<=10; i++) { cout << "\nIteration Nr “ << i << “ – 1st part loop"; if ( i==3 ) continue; cout << “\nIteration Nr “ << i << “ – 2nd part loop\n"; if ( i==5 ) break; }

9 9 break, continue, goto stmts  The goto control statement t Remark: Forget the goto statement. Structured programming is “go to-less” programming. t Syntax:goto ; t Usage: –Unconditional transfer of control to statement labeled

10 10 5.8 Nested Loops t As with if statements, loop statements can be nested t Each time outer loop is repeated, any inner loop is restarted - loop control components are reevaluated and all required iterations are performed

11 11 5.8 Nested Loops t Possible to nest loops –Loops inside other loops –Start with outer loop jump to inner loop –Inner loop continues until complete –Back to outer loop and start again t NestLoop.cpp examples

12 12 Nested Logic – variant 1 outer = 1; while(outer <= 3) { inner = 1; // Resets for each iteration of the outer loop while(inner <= 3) { cout << outer << " " << inner << endl; inner = inner + 1; } // End of the nested loop outer = outer + 1; // Increment the outer loop counter } // The end of the outer loop

13 13 Nested Logic – variant 1 Relation btw outer and inner counters Outer |1|2|3| |||| Inner | 1,2,3| 1,2,3| 1,2,3| Total number of iterations for the inner loop body is =3*3=3 + 3 + 3 = 9

14 14 Nested Logic – variant 2 outer = 1; while(outer <= 3) { inner = 1; // Resets for each iteration of the outer loop while(inner <= outer) { cout << outer << " " << inner << endl; inner = inner + 1; } // End of the nested loop outer = outer + 1; // Increment the outer loop counter } // The end of the outer loop

15 15 Nested Logic – variant 2 Relation btw outer and inner counters Outer |1|2|3| |||| Inner | 1| 1,2| 1,2,3| Total number of iterations for the inner loop body is = 1 + 2 + 3 = 6

16 16 Nested Logic – variant 3 outer = 1; while(outer <= 3) { inner = 1; // Resets for each iteration of the outer loop while(inner < outer) { cout << outer << " " << inner << endl; inner = inner + 1; } // End of the nested loop outer = outer + 1; // Increment the outer loop counter } // The end of the outer loop

17 17 Nested Logic – variant 3 Relation btw outer and inner counters Outer |1|2|3| |||| Inner | ?| ?| ?| Total number of iterations for the inner loop body is = …

18 18 Listing 5.13 Nested for loop program

19 19 Listing 5.13 Nested for loop program (continued)

20 20 Listing 5.14 Displaying the multiplication table

21 21 Listing 5.14 Displaying the multiplication table (continued)

22 22 Nested loops (reminder) Example: for (i=1; i<=10; i++) for(j=1; j<=5; j++) cout << setw(8) << i*j;

23 23 Nested loops (reminder) Example: for (i=1; i<=10; i++) { for(j=1; j<=i; j++) cout << setw(8) << i*j; cout << endl; }

24 24 Bit wise operators int a, b; // logical And operator a = 22 && 22;cout << ‘\n’ << a; // bit wise And operator b = 22 & 22;cout << ‘\n’ << b;

25 25 Bit wise operators int a, b; // logical Or operator a = 22 || 22;cout << ‘\n’ << a; // bit wise Or operator b = 22 | 22;cout << ‘\n’ << b;

26 26 Bit wise operators int a, b; // NO logical EOr operator // bit wise EOr operator b = 22 ^ 22;cout << endl << b; a = 12 ^ 23;cout << endl << a;

27 27 Shift left ( >) operators int a=32, b=1024; // Shift left (<<) operator cout << ‘\n’ << a << “ “ << a << 2; cout << ‘\n’ << a << “ “ << (a << 2); // Shift right (>>) operator cout > 3);

28 28 Exercise 14.1 Build programs based on linear, branch and loop algorithms:  Write double powm(double x, int n); function to return x n (x raised to power of n); Create driver program to test powm(3.0, 0); powm(4.0, 2); powm(2.0, -2); Expected return value: 1.16.0.25

29 29 Exercise 14.2 Build programs based on linear, branch and loop algorithms: t Character counting program: in C++ (cin.get()) style in C (getchar()) style; Hint: See back page on handout for details

30 30 Exercise 14.3 Build programs based on linear, branch and loop algorithms: To display Amount saved entering Principal, Rate and Duration using the well known formulae Amount = Principal * (1 + Rate/100) Duration using run time function double pow(double x,double y) /#include or #include / or the user function double powm(double x, int n);

31 31 Exercise 14.4 Build programs based on linear, branch and loop algorithms: To generate a table of multiples of a given number. The user should enter the number and the program should display a table of ten columns and five lines

32 32 Before lecture end Lecture: Control Flow. The break, continue, goto statements More to read: Friedman/Koffman, Chapter 05

33 33 Thank You For Your Attention!


Download ppt "Lecture 14: Control Flow. The break, continue, goto statements."

Similar presentations


Ads by Google