Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.

Similar presentations


Presentation on theme: "Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming."— Presentation transcript:

1 Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming

2 SE15: Branching Statements7–27–2 Today’s Learning Objectives Understand how you can alter the flow of a program Learn about the Java branching statements Learn about Boolean expressions

3 SE15: Branching Statements7–37–3 Lecture Outline Flow of Control Branching Statements The if statement The if-else statement Boolean expressions Multi branch if-else statements The switch statement

4 SE15: Branching Statements7–47–4 Flow of Control The order in which a program performs actions Java uses two kinds of statements to regulate flow of control: Branching or Conditional statement Chooses one action from a list Loop statement Repeats an action until some stopping condition is met Savtich p130

5 SE15: Branching Statements7–57–5 The if statement An example if statement if (total > 30) System.out.println(“Count exceeded”); The condition in the statement is total > 30. This expression must evaluate to a boolean result (true or false). If it is true the println statement is executed, if not the println statement is skipped. The condition is enclosed in parentheses.

6 SE15: Branching Statements7–67–6 if-else Chooses between two possible alternative actions Banking example If you are in credit, interest may be added to your account If you are in debt, you will be charged a penalty Example 1 if (balance >= 0) balance = balance + (INTEREST_RATE * balance); else balance = balance - OVERDRAWN_PENALTY ; Note: expression inside parentheses

7 SE15: Branching Statements7–77–7 Block or Compound Statements You may want to do more than one thing as the result of evaluating a boolean condition. In java we can replace the single statement with a block statement. A block statement is a collection of statements enclosed in parentheses.

8 SE15: Branching Statements7–87–8 Example 2 (Compound statements) if (balance >= 0) { System.out.println(“Wow, you are in credit”); balance = balance + (INTEREST_RATE * balance); } else { System.out.println(“Call us to discuss a loan”); balance = balance - OVERDRAWN_PENALTY; }

9 SE15: Branching Statements7–97–9 Boolean Expressions An expression which is either true or false time < time_limit balance <= 0 You can form more complicated expressions from simpler ones by joining them with the Java version of “and”, && (logical operator) if ((pressure > min ) && (pressure < max)) System.out.println(“Pressure is OK.”); else System.out.println(“Warning!!”); Note: you cannot use min < pressure < max

10 SE15: Branching Statements7–10 Java Comparison Operators Math Notation NameJava Notation Examples =Equal to ==balance == 0 ≠Not equal to !=answer != ‘y’ >Greater than >cost > balance ≥Greater than or equal to >=marks >= 70 <Less than <balance < 0 ≤Less than or equal to <=cost <= balance

11 SE15: Branching Statements7–11 Logical Operators You can also use “or” to combine simpler expressions, || if ((salary > phoneBill) || (savings > phoneBill)) System.out.println(“You can pay the bill”); else System.out.println(“Get rid of the phone”); You can negate an expression using ! if (!(number >= min)) System.out.println(“Too small”); else System.out.println(“OK”); Note: avoid ! to improve readability

12 SE15: Branching Statements7–12 Nested Statements You can use one if-else statement within another if (balance >= 0) if (INTEREST_RATE >= 0) balance = balance + (INTEREST_RATE * balance); else System.out.println(“Error: negative interest”); else balance = balance - OVERDRAWN_PENALTY ;

13 SE15: Branching Statements7–13 Multibranch if-else Statements If you have the ability to branch two ways, then you have the ability to branch four or more ways. Just branch two ways and then have each of these outcomes branch two ways…… A example of a three way branch if (balance > 0) System.out.println(“Positive balance”); else if (balance < 0) System.out.println(“Negative balance”); else if (balance == 0) System.out.println(“Zero balance”); Savtich p145

14 SE15: Branching Statements7–14 The switch statement A multi-way branch that makes its decision based on the value of an integer or character expression by matching that value with one of several possible cases. The switch statement begins with the keyword switch followed by a controlling expression in parentheses switch (seatLocationCode) This is followed by a list of cases, consisting of the keyword case and a case label followed by the actions for each case case 1: System.println(“Orchestra”); Savtich p149

15 SE15: Branching Statements7–15 Example of switch switch (seatLocationCode) { case 1: System.out.println(“Orchestra”); price = 40.0; break; case 2: System.out.println(“Upper Circle”); price = 50.0; break; default: System.out.println(“Unknown Ticket Code”); break; }

16 SE15: Branching Statements7–16 Summary Looked at the using branching to control the flow of a program. Met if if-else Boolean Expressions Multibranch if-else switch statement

17 SE15: Branching Statements7–17 Follow-up Work Savitch chapter 3 How do you test if two objects are equal, such as two strings? Which methods do you require to determine the alphabetical order of two strings? What does the following conditional operator do? int max, n1, n2; max = (n1 > n2) ? n1 : n2;


Download ppt "Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming."

Similar presentations


Ads by Google