Chapter 5 The if Statement Conditional control structure

  • Slides: 14
Download presentation
Chapter 5 The if Statement § Conditional control structure, also called a decision structure

Chapter 5 The if Statement § Conditional control structure, also called a decision structure § Executes a set of statements when a condition is true § The condition is a Boolean expression § For example, the statement if (x == 5) { y = 20; } assigns the value 20 to y only if x is equal to 5. Slide 1 © 2007 Lawrenceville Press

Chapter 5 Relational Operators Operator == < <= > >= != Slide 2 Meaning

Chapter 5 Relational Operators Operator == < <= > >= != Slide 2 Meaning equal less than or equal greater than or equal not equal © 2007 Lawrenceville Press

Chapter 5 The if-else Statement Contains an else clause that is executed when the

Chapter 5 The if-else Statement Contains an else clause that is executed when the if condition evaluates to false. For example, the statement if (x == 5) { y = 20; } else { y = 10; } assigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5. Slide 3 © 2007 Lawrenceville Press

Chapter 5 Nested if-else Statements § Should be indented to make the logic clear.

Chapter 5 Nested if-else Statements § Should be indented to make the logic clear. § Nested statement executed only when the branch it is in is executed. For example, the statement if (x == 5) { y = 20; } else { if (x > 5) { y = 10; } else { y = 0; } } evaluates the nested if-else only when x is not equal to 5. Slide 4 © 2007 Lawrenceville Press

Chapter 5 The if-else if Statement § Used to decide among three or more

Chapter 5 The if-else if Statement § Used to decide among three or more actions. § Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement if (x < 5) { y = 20; } else if (x < 10) { y = 40; } else if (x < 15) { y = 80; } would give very different results if the conditions were ordered differently. Slide 5 © 2007 Lawrenceville Press

Chapter 5 The switch Statement § Used to decide among three or more actions.

Chapter 5 The switch Statement § Used to decide among three or more actions. § Uses an expression that evaluates to an integer. § The break statement moves program execution to the next statement after the switch. § The default code is optional and is executed when none of the previous cases are met: switch (num. Legs) { case 2: System. out. println("human"); break; case 4: System. out. println("beast"); break; case 8: System. out. println("insect"); break; default: System. out. println("? ? ? "); break; } Slide 6 © 2007 Lawrenceville Press

Chapter 5 The Math Class § Part of the java. lang package § The

Chapter 5 The Math Class § Part of the java. lang package § The random() methods generates a double between 0 and 1. 0. For example, double r. Num; r. Num = Math. random(); § A random integer in a range is generated by using the expression: (high. Num – low. Num + 1) * Math. random() + low. Num Slide 7 © 2007 Lawrenceville Press

Chapter 5 Compound Boolean Expressions § More than one Boolean expression in a single

Chapter 5 Compound Boolean Expressions § More than one Boolean expression in a single condition. § Formed using the logical And (&&), logical Or (||), or logical Not (!) operators. Slide 8 © 2007 Lawrenceville Press

Chapter 5 And Truth Table And Exp 1 Exp 2 Result True False True

Chapter 5 And Truth Table And Exp 1 Exp 2 Result True False True False Slide 9 © 2007 Lawrenceville Press

Chapter 5 Or Truth Table Or Exp 1 Exp 2 Result True True False

Chapter 5 Or Truth Table Or Exp 1 Exp 2 Result True True False False Slide 10 © 2007 Lawrenceville Press

Chapter 5 Not Truth Table Not Slide 11 Exp Result True False True ©

Chapter 5 Not Truth Table Not Slide 11 Exp Result True False True © 2007 Lawrenceville Press

Chapter 5 The Math Class § Part of the java. lang package § Methods

Chapter 5 The Math Class § Part of the java. lang package § Methods include: abs(num) returns the absolute value of num pow(num 1, num 2) returns num 1 raised to the num 2 power sqrt(num) returns the square root of num, where num is a positive number Slide 12 © 2007 Lawrenceville Press

Chapter 5 Flowchart Symbols decision Slide 13 © 2007 Lawrenceville Press

Chapter 5 Flowchart Symbols decision Slide 13 © 2007 Lawrenceville Press

Chapter 5 The RPS Flowchart Slide 14 © 2007 Lawrenceville Press

Chapter 5 The RPS Flowchart Slide 14 © 2007 Lawrenceville Press