03-conditionals

03-conditionals - Conditional Statements CSE 114: Computer...

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

View Full Document Right Arrow Icon
Conditional Statements CSE 114: Computer Science I 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Flow of Control 2
Background image of page 2
The if Statement General form: if ( condition ) statement (or block of statements) to be executed if condition is true Ex. if (length < 2) System.out.print(“Too short!\n”); 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Examples if (age < 16) System.out.print(“Too young to drive!\n”); if (password.equals(“foo”)) accessGranted = true; 4
Background image of page 4
Empty Statements A semicolon by itself is a valid (but non- functional) statement Common mistake: putting a semicolon immediately after an if statement: if (x > 5) ; System.out.print(“x greater than 5!”); 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
Compound Statements if and else only execute a single following statement We can get around this by enclosing multiple statements in braces The resulting block is called a compound statement Style suggestion: always use braces around the body of an if or else clause 6
Background image of page 6
Compound Statements if (hours > 40) { hours = hours - 40; overtimePay = hours * 12.0; totalPay = (40 * 8.0) + overtimePay; } 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 23

03-conditionals - Conditional Statements CSE 114: Computer...

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

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