Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 SE15: Branching Statements6–26–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 Statements6–36–3 Lecture Outline Flow of Control Branching Statements The if-else statement Boolean expressions Multi branch if-else statements The switch statement

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

5 SE15: Branching Statements6–56–5 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

6 SE15: Branching Statements6–66–6 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; } Example 3 (Omitting else) if (balance >= 0) balance = balance + (INTEREST_RATE * balance);

7 SE15: Branching Statements6–76–7 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”, && if ((pressure > min ) && (pressure < max)) System.out.println(“Pressure is OK.”); else System.out.println(“Warning!!”); Note: you cannot use min < pressure < max

8 SE15: Branching Statements6–86–8 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

9 SE15: Branching Statements6–96–9 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

10 SE15: Branching Statements6–10 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 ;

11 SE15: Branching Statements6–11 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…… 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

12 SE15: Branching Statements6–12 The switch statement A multi-way branch that makes its decision based on the value of an integer or character expression 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

13 SE15: Branching Statements6–13 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; }

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

15 SE15: Branching Statements6–15 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 6 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming."

Similar presentations


Ads by Google