Presentation is loading. Please wait.

Presentation is loading. Please wait.

Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

Similar presentations


Presentation on theme: "Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved."— Presentation transcript:

1 Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

2 Why do we care about relational and logical operations? New structures – Decision We may want to do two different things. We can carry out division as long as the denominator is not 0. We may want to validate input What if a user enters a character instead of a number? Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #2

3 Flowcharts If statements can be modeled as a flow chart. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #3 Wear a coat. Yes Is it cold outside? if (coldOutside) wearCoat();

4 Flowcharts A block if statement may be modeled as: Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #4 Wear a coat. Yes Is it cold outside? Wear a hat. Wear gloves. if (coldOutside) { wearCoat(); wearHat(); wearGloves(); } Note the use of curly braces to block several statements together.

5 Relational Operators In most cases, the boolean expression, used by the if statement, uses relational operators. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #5 Relational OperatorMeaning > is greater than < is less than >= is greater than or equal to <= is less than or equal to == is equal to != is not equal to

6 Boolean Expressions A boolean expression is any variable or calculation that results in a true or false condition. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #6 ExpressionMeaning x > y Is x greater than y? x < y Is x less than y? x >= y Is x greater than or equal to y? x <= y Is x less than or equal to y. x == y Is x equal to y? x != y Is x not equal to y?

7 If Statements and Boolean Expressions if (x > y) System.out.println(“X is greater than Y”); if(x == y) System.out.println(“X is equal to Y”); if(x != y) { System.out.println(“X is not equal to Y”); x = y; System.out.println(“However, now it is.”); } Example: AverageScore.javaAverageScore.java Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #7

8 Programming Style and if Statements If statements can span more than one line; however, they are still one statement. if(average > 95) System.out.println(“That’s a great score!”); is functionally equivalent to if(average > 95) System.out.println(“That’s a great score!”); Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #8

9 Programming Style and if Statements Rules of thumb: The conditionally executed statement should be on the line after the if condition. The conditionally executed statement should be indented one level from the if condition. If an if statement does not have the block curly braces, it is ended by the first semicolon encountered after the if condition. if(expression) statement; Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #9 No semicolon here. Semicolon ends statement here.

10 Block if Statements Conditionally executed statements can be grouped into a block by using curly braces {} to enclose them. If curly braces are used to group conditionally executed statements, the if statement is ended by the closing curly brace. if(expression) { statement1; statement2; } Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #10 Curly brace ends the statement.

11 Block if Statements Remember that if the curly braces are not used, then only the next statement after the if condition will be executed conditionally. if(expression) statement1; statement2; statement3; Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #11 Only this statement is conditionally executed.

12 Comparing Characters Characters can be tested using the relational operators. Characters are stored in the computer using the Unicode character format. Unicode is stored as a sixteen (16) bit number. Characters are ordinal, meaning they have an order in the Unicode character set. Since characters are ordinal, they can be compared to each other. char c = ‘A’; if(c < ‘Z’) System.out.println(“A is less than Z); Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #12

13 if-else Statements The if-else statement adds the ability to conditionally execute code based if the expression of the if statement is false. if(expression) statementOrBlockIfTrue; else statementOrBlockIfFalse; Example: Division.javaDivision.java Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #13

14 if-else Statement Flowcharts Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #14 Wear a coat. Yes Is it cold outside? Wear shorts. No

15 Scanner has methods Has methods let us ask the question Does this Scanner have a ????? hasNextInt() hasNextDouble() hasNext() Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #15


Download ppt "Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved."

Similar presentations


Ads by Google