Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.

Similar presentations


Presentation on theme: "Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012."— Presentation transcript:

1 Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012

2 So Far… Statement 1 Statement 2 Statement n-1 Statement n...... int n, temp, ans; cout<<“Enter number: “; cout<<“The answer is “<<ans; return 0;...... The C++ programs we have been writing so far have been executing sequentially

3 Introduction C++ provides different forms of statements for different purposes – Declaration statements – Assignment-like statements – etc The order in which statements are executed is called flow control

4 Branching statements – specify alternate paths of execution, depending on the outcome of a logical condition – Do one thing if some condition is met or another if not (Decisions) Loop statements – specify computations, which need to be repeated until a certain logical condition is satisfied. – Do a certain action repeatedly based on a condition (Looping)

5 C++ provides us with the if statement and the switch statement for these purposes.

6 If statement The if statement Syntax: if (condition) statement If the condition is true, the statement is executed, otherwise it is skipped. A single statement or a compound statement, to be executed if condition evaluates to true. A test expression that evaluates to a boolean value (either true or false)

7 Example 1 unsigned short age; cout<<“Enter your age: “; cin>>age; if (age < 17) cout<<“You are too young!\n”; cout<<“Thank you.”<<endl; Note: the if statement in this example, executed only a single statement when the expression is true

8 Example 2 In order to execute multiple statements, we can use a block. int val; cout<<“Enter an integer value: “; cin>>val; if (val%2 == 0) { int quotient = val/2; cout<<val<<“ is divisible by 2.”<<endl; cout<<“The quotient is ”<<quotient<<endl; } cout<<“Thank you.”<<endl;

9 The else clause Syntax: if (condition) statement1 else statement2 statement1 and statement2 can be compound statements. 11/13/09 If condition evaluates to true, this statement will be executed If condition evaluates to false, this statement will be executed

10 Branching Mechanisms if-else statements – Choice of two alternate statements based on condition expression – Example: if (hrs > 40) grossPay = rate*40 + 1.5*rate*(hrs-40); else grossPay = rate*hrs;

11 if-else Statement Syntax Formal syntax: if ( ) else Note each alternative is only ONE statement! To have multiple statements execute in either branch  use compound statement

12 Compound/Block Statement Must use compound statement { } for multiples – Also called a "block" statement Each block should have block statement – Even if just one statement – Enhances readability

13 Compound Statement in Action Note indenting in this example: if (myScore > yourScore) { cout << "I win!\n"; wager = wager + 100; } else { cout << "I wish these were golf scores.\n"; wager = 0; }

14 Flowchart equivalents 11/13/09

15 Common Pitfalls Operator "=" vs. operator "==" One means "assignment" (=) Two means "equality" (==) – VERY different in C++! – Example: if (x == 12)  Note operator used! Do_Something else Do_Something_Else

16 The Optional else else clause is optional – If, in the false branch (else), you want "nothing" to happen, leave it out – Example: if (sales >= minimum) salary = salary + bonus; cout << "Salary = %" << salary; – Note: nothing to do for false condition, so there is no else clause! – Execution continues with cout statement

17 Nested Statements if-else statements contain smaller statements – Compound or simple statements (we’ve seen) – Can also contain any statement at all, including another if- else statement! – Example: if (speed > 55) if (speed > 80) cout << "You’re really speeding!"; else cout << "You’re speeding."; Note proper indenting!

18 Multiway if-else: Display Not new, just different indenting Avoids "excessive" indenting – Syntax:

19 Multiway if-else Example: Display

20 The Conditional Operator A short-hand method of expressing if…else statements Syntax: condition ? expression1 : expression2; 11/13/09 An expression that evaluates to a boolean. Expression to execute if condition evaluates to true. Expression to execute if condition evaluates to false.

21 Example (x < y) ? x = 10 : y = 10; //this is the same as if (x < y) x = 10; else y = 10; 11/13/09

22 The switch Statement A new statement for controlling multiple branches Uses controlling expression which returns bool data type (true or false) Syntax: – next slide

23 Syntax: switch (expression) { case constant1: statements break; case constant2: statements break; … case constantN: statements break; default: statements } An integer expression, whose value we want to test Unique, possible values that we are testing for equality with expression Statements to be executed when expression becomes equal to constantN Statements to be executed when expression is not equal to any of the N constants The break statement causes the Program to exit the switch statement

24 switch Statement Syntax

25 The switch Statement in Action

26 The switch: multiple case labels Execution "falls thru" until break – switch provides a "point of entry" – Example: case "A": case "a": cout << "Excellent: you got an "A"!\n"; break; case "B": case "b": cout << "Good: you got a "B"!\n"; break; – Note multiple labels provide same "entry"

27 switch Pitfalls/Tip Forgetting the break; – No compiler error – Execution simply "falls thru" other cases until break; Biggest use: MENUs – Provides clearer "big-picture" view – Shows menu structure effectively – Each branch is one menu choice

28 switch Menu Example Switch statement "perfect" for menus: switch (response) { case "1": // Execute menu option 1 break; case "2": // Execute menu option 2 break; case 3": // Execute menu option 3 break; default: cout << "Please enter valid response."; }

29 Exercise Write a program that displays the following menu: Geometry Calculator 1.Calculate the area of a Circle 2.Calculate the Area of a Rectangle 3.Calculate the Area of a Triangle 4.Quit Enter your choice (1-4) :

30 Exercise continued If the user enters 1, the program should ask for the radius of the circle and display its area. If the user enter 2, the program should ask for the length and width of the rectangle, and then display the rectangle’s area. If the program enter 3, the program should ask for the length of the triangle’s base and its height, and then display its area. If the user enters 4, the program should end.

31 Good Night and Good Luck. That’s all for now folks!!


Download ppt "Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012."

Similar presentations


Ads by Google