Lecture_7

# Lecture_7 - Chapter3:DecisionStructures...

This preview shows pages 1–11. Sign up to view the full content.

Chapter 3:Decision Structures

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 3:Decision Structures 3.1 The if Statement 3.2 The if-else Statement 3.3 The if-else-if Statement   3.4 Nested if Statements    3.5 Logical Operators  3.6 Comparing String Objects  3.7 More About Variable Declaration and Scope 3.9 The switch Statement 3.10 Creating Objects with the DecimalFormat Class 3.12 Common Errors to Avoid
The if Statement The code in methods executes sequentially. The  if  statement allows the programmer to make  decisions on whether a section of code executes  or not. The if statement uses a boolean expression as an  argument to decide if the next statement or block  of statements executes. if(boolean expression is true) execute next statement.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Flowcharts If statements can be modeled as a flow  chart. Wear a coat. Yes Is it cold outside? if (coldOutside) wearCoat(); No
Flowcharts A block if statement may be modeled as: 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 .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Relational Operators In most cases, the boolean expression,  used by the if statement, uses  relational  operators. Relational Operator Meaning > 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
Boolean Expressions boolean expression  is any variable or  calculation that results in a  true  or  false   condition. Expression Meaning 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?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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.”); }
AverageScore.java double score1; double score2; double score3; double average; String input; input = JOptionPane.showInputDialog(“Enter ” + “ score #1:”); score1 = Double.parseDouble(input);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
AverageScore.java input = JOptionPane.showInputDialog(  "Enter score #2:"); score2 = Double.parseDouble(input);
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 32

Lecture_7 - Chapter3:DecisionStructures...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online