1 / 24

Flow of Control

Flow of Control. INFSY 535 Spring 2003 Lecture 6. Control Structures. Sequence : one after the other Selection : selects a path based upon action Iteration : repetition. Iteration. Loop: a portion of a program that repeats itself a number of times

deangelis
Download Presentation

Flow of Control

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. Flow of Control INFSY 535 Spring 2003 Lecture 6

  2. Control Structures • Sequence: one after the other • Selection: selects a path based upon action • Iteration: repetition

  3. Iteration • Loop: a portion of a program that repeats itself a number of times • Counter-Controlled Loop: loop that is managed by counting the number of iterations • Conditional Loop: loop which continues while a logical condition is true

  4. while-statement • #include <iostream> • using namespace std; • void main( ) • { • int count_down; • cout << “How many greetings do you want? “; • cin >> count_down; • while (count_down > 0)// logical expression • { • cout << “Hello “; // body • count_down = count_down - 1; • } • cout << endl; • cout << “That’s all!\n”; • return; • }

  5. do-while-statement • similar to a while-statement • loop body is always executed at least once

  6. do-while-statement • char ans; • do • { • cout << “Hello\n”; • cout << “Do you want another greeting?\n” • << “Press y for yes, n for no, \n” • << “and then press return: ”; • cin >> ans; • } while (ans == ‘y’ || ans == ‘Y’);

  7. while(expression) . . - logical expression is checked before loop execution do . while (expression); - logical expression is checked after loop execution Iteration

  8. Iteration Pitfall • Infinite Loop - loop runs forever • x = 2; • while (x != 12) • { • cout << x << endl; • x + = 2; • } • Output =2, 4, 6, 8, 10, loop stops

  9. Iteration Pitfall • x = 1; • while (x != 12) • { • cout << x << endl; • x + = 2; • } • Output = 1, 3, 5, 7, 9, 11, 13, 15, 17, ... loop

  10. Increment/Decrement Operators • A shortcut to increase or decrease a value stored in a variable • The increment/decrement takes place before/after the actual operation on a variable depending upon placement of the operator • ++operator is the incrementoperator • --operator is the decrement operator • Used with variable of typeint

  11. Increment/Decrement Operators • postincrement: when the operator is physically after the variable to be incremented • int m = 3; • k = m++; • 3 assigned to k; m incremented to 4 • preincrement: when the operator is physically before the variable to be incremented • int m = 3; • k = ++m; • m incremented to 4; 4 assigned to k

  12. Increment/Decrement Operators • postdecrement: when the operator is physically after the variable to be decremented • int m = 3; • k = m--; • 3 assigned to k; m decremented to 2 • predecrement: when the operator is physically before the variable to be decremented • int m = 3; • k = --m; • m decremented to 2; 2 assigned to k

  13. Increment/Decrement Operators • Practice with Self-Test exercises, page 419-420, 25 thru 28.

  14. for-Statement

  15. for-Statement

  16. for-Statement • Initializing expression is executed once • Logical expression is evaluated for true/false; terminates if false • Otherwise statements are executed • Increment/decrement is executed

  17. for-Statement Examples • int n; • for (n = 1; n <= 10; n+ = 2) • cout << “n is now equal to “ << n << endl; • for (int n = 0; n < 5; n++) • balance = balance - 2;

  18. for-Statement Pitfalls • Make sure all variables used in the loop are initialized • Be careful to avoid infinite loops • Do not place a semicolon after the parentheses at the beginning of a for-loop

  19. What kind of loop to use? • If a loop involves a numeric calculation using one variable that will be changed by an equal amount each time through the loop, use a for-loop. • If the body of a loop must be executed at least one time, use ado-while-loop. • If there are circumstances for which the loop body should not be executed, use a while-loop.

  20. LAB • Self Test, page 440, 40 - 41 • Write a for-loop for 40. • Write a while-loop and a do-while-loop for 41.

  21. UML Objects

  22. Classes Connect • Classes are the vocabulary of an area of knowledge. • Design a system, model the terms as classes in the UML.

  23. Class Relationships • Association between 2 classes • Multiplicity

  24. Class Relationships

More Related