Lecture_7 - Chapter3:DecisionStructures...

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

View Full Document Right Arrow Icon
    Chapter 3:Decision Structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Flowcharts If statements can be modeled as a flow  chart. Wear a coat. Yes Is it cold outside? if (coldOutside) wearCoat(); No
Background image of page 4
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 .
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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?
Background image of page 7

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

View Full DocumentRight Arrow Icon
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.”); }
Background image of page 8
AverageScore.java double score1; double score2; double score3; double average; String input; input = JOptionPane.showInputDialog(“Enter ” + “ score #1:”); score1 = Double.parseDouble(input);
Background image of page 9

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

View Full DocumentRight Arrow Icon
AverageScore.java input = JOptionPane.showInputDialog(  "Enter score #2:"); score2 = Double.parseDouble(input);
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

Lecture_7 - Chapter3:DecisionStructures...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online