Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 The if Statement

Similar presentations


Presentation on theme: "Chapter 5 The if Statement"— Presentation transcript:

1 Chapter 5 The if Statement
5/13/2018 9:32 PM 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. Refer to page 105 in the text. © 2007 Lawrenceville Press

2 Chapter 5 Relational Operators
5/13/2018 9:32 PM Chapter 5 Relational Operators Operator Meaning == equal < less than <= less than or equal > greater than >= greater than or equal != not equal Refer to page 105 in the text. Relational operators are used to form Boolean expressions. Boolean expressions evaluate to true or false. © 2007 Lawrenceville Press

3 Chapter 5 The if-else Statement
5/13/2018 9:32 PM 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. Refer to pages 106 and 107 in the text. © 2007 Lawrenceville Press

4 Chapter 5 Nested if-else Statements
5/13/2018 9:32 PM 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. Refer to page 107 in the text. © 2007 Lawrenceville Press

5 Chapter 5 The if-else if Statement
5/13/2018 9:32 PM 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. Refer to page 108 in the text. © 2007 Lawrenceville Press

6 Chapter 5 The switch Statement
5/13/2018 9:32 PM 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 (numLegs) { 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; } Refer to page 109 in the text. © 2007 Lawrenceville Press

7 Part of the java.lang package
5/13/2018 9:32 PM 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 rNum; rNum = Math.random(); A random integer in a range is generated by using the expression: (highNum – lowNum + 1) * Math.random() + lowNum Refer to pages 110 and 111 in the text. The Random class actually generates pseudorandom (like random) numbers. Java implements the Linear Congruential Method to ensure that generated numbers can be considered random for most applications. © 2007 Lawrenceville Press

8 Chapter 5 Compound Boolean Expressions
5/13/2018 9:32 PM 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. Refer to page 112 in the text. Compound Boolean expressions use more than one Boolean expression to determine if a condition is true or false. © 2007 Lawrenceville Press

9 Chapter 5 And Truth Table
5/13/2018 9:32 PM Chapter 5 And Truth Table And Exp1 Exp2 Result True False Refer to page 112 in the text. © 2007 Lawrenceville Press

10 Or Exp1 Exp2 Result True False Chapter 5 Or Truth Table
5/13/2018 9:32 PM Chapter 5 Or Truth Table Or Exp1 Exp2 Result True False Refer to page 112 in the text. © 2007 Lawrenceville Press

11 Chapter 5 Not Truth Table
5/13/2018 9:32 PM Chapter 5 Not Truth Table Not Exp Result True False Refer to page 112 in the text. © 2007 Lawrenceville Press

12 Part of the java.lang package Methods include:
5/13/2018 9:32 PM Chapter 5 The Math Class Part of the java.lang package Methods include: abs(num) returns the absolute value of num pow(num1, num2) returns num1 raised to the num2 power sqrt(num) returns the square root of num, where num is a positive number Refer to page 113 in the text. © 2007 Lawrenceville Press

13 Chapter 5 Flowchart Symbols
5/13/2018 9:32 PM Chapter 5 Flowchart Symbols decision Refer to page 115 in the text. The diamond flowchart symbol indicates a decision. © 2007 Lawrenceville Press

14 Chapter 5 The RPS Flowchart
5/13/2018 9:32 PM Chapter 5 The RPS Flowchart Refer to page 115 in the text. The RPS flowchart illustrates the application solution. Solution steps include: 1. prompting the user for a number between 1 and 3 2. generating a random number between 1 and 3 3. comparing the generated number to the number typed by the user 4. determining a winner and displaying an appropriate message © 2007 Lawrenceville Press


Download ppt "Chapter 5 The if Statement"

Similar presentations


Ads by Google