Presentation is loading. Please wait.

Presentation is loading. Please wait.

Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.

Similar presentations


Presentation on theme: "Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible."— Presentation transcript:

1 Branch Statements (Decision)

2 Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible actions.  A loop statement (to be discussed later) repeats an action again and again until some stopping condition is met.

3 The if-else Statement  Example: if (balance >= 0) balance = balance +(INTEREST_RATE * balance)/12; else balance = balance – OVERDRAWN_PENALTY;  If the condition (balance >= 0) is true, the if part of the statement is executed. If it is false the else part is executed.

4 The if-else Statement (cont’d)  If the else part is omitted nothing happens if the condition is false.  Example: if (weight > ideal) calorieAllotment = calorieAllotment -500;

5 Boolean Expressions  The condition in an if-else statement is a boolean expression.  A boolean expression is simply an expression that is either true or false.  The simplest boolean expressions are comparisons of two expressions, like time < limit, or balance <= 0

6 Boolean Expressions (cont’d)  Far more complicated boolean expressions are possible.  Examples: if ((pressure > min) && (pressure < max))... if ((a + b < c) || (a + c < b) || (b + c < a))...

7 Java Comparison Operators Math NotationNameJava NotationJava Examples = Equal to == balance == 0 ≠ Not equal to != income != tax > Greater than > cost > value ≥ Greater than or equal to >= points >= 60 < Less than < pressure < max ≤ Less than or equal to <= cost <= value

8 Java Boolean Operators Logical Notation NameJava Notation Java Examples ^and&& rich && famous vor|| win || lose ¬not! !married

9 Nested if-else Statements  if-else statements can be nested within other if-else statements  Example: if (balance >= 0) if (INT_RATE >= 0) balance = balance +(INT_RATE * balance)/12; else System.out.println(“Negative int error”); else balance = balance – OVERDRAWN_PENALTY;

10 Compound Statements  If the if or the else part of the branching statement can not be expressed in a single statement, the multiple statements needed can be grouped together by putting braces { } around them. They are then treated as single compound statement. { system.out.println(“Good for you.”); balance = balance +(INT_RATE * balance)/12; }

11 Multibranch if-else Statements Example: 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”);

12 The Switch Statement  The switch statement is a multiway branch that makes its decision as to which way to branch based on the value of an integer or character expression.  The switch statement begins with the keyword switch followed by an expression in parentheses.  Below this is a list of cases enclosed in braces, each case consisting of the keyword case followed by a constant, then a colon, and then a list of statements which are the actions for that case.

13 Switch Example switch (numberOfBabies) { case 1: case 2: System.out.println(“Congratulations.”); break; case 2: System.out.println(“Wow. Twins.”); break; case 3: System.out.println(“Wow. Triplets.”); break; default: System.out.println(“Unbelievable.”); break; }


Download ppt "Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible."

Similar presentations


Ads by Google