05-java-control-structures

05-java-control-structures - Java Control/Data Structures...

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

View Full Document Right Arrow Icon
Java Control/Data Structures CSE 110: Introduction to Computer Science SUNY at Stony Brook
Background image of page 1

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

View Full DocumentRight Arrow Icon
Control and Data Structures Conditional Statements Loops Arrays and ArrayLists
Background image of page 2
Conditionals and Decision Statements CSE 110: Intro to Computer Science SUNY at Stony Brook
Background image of page 3

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

View Full DocumentRight Arrow Icon
Making Decisions Many programs must behave differently when presented with different situations e.g., choosing an action from a menu, determining what sales tax rate to charge, etc. Java offers two similar ways to handle these situations: branching (if-else) statements selection (switch) statements
Background image of page 4
True and False Values A boolean expression is one whose value is either true or false (but not both!) Ex. a > b or var1 == var2 Boolean (logical) equality operator: == Common programming error: using ‘=’ (assignment) instead of “==” (equality) Ex. if (x = 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
Boolean Operations Decisions are made in Java programs using boolean logic (i.e., values of true and false) and operations Relational operators are used to compare two values Logical operators combine smaller boolean expressions into a single, larger expression
Background image of page 6
Relational Operators Operator Meaning Example < Less than age < 30 > Greater than height > 6.2 <= Less than or equal to taxable <= 20000 >= Greater than/equal to temp >= 98.6 == Equal to grade == 100 != Not equal to number != 250
Background image of page 7

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

View Full DocumentRight Arrow Icon
Logical Operators && (logical AND) true only if BOTH operands are true || (logical OR) true if AT LEAST ONE operand is true (inclusive OR) ! (logical NOT) true if operand is false and vice versa
Background image of page 8
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”);
Background image of page 9

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;
Background image of page 10
The if-else Statement Select one of two possible execution paths, based on the result of a comparison General format: if ( expression ) statement block 1 else statement block 2
Background image of page 11

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

View Full DocumentRight Arrow Icon
More on if Statements An if statement executes its body when (and only when) the condition is true If the condition is false, the body is skipped, and execution picks up at the Frst statement after the if By default, the body of an if statement is restricted to the Frst statement that follows the “if (condition)” line Indentation doesn’t matter This can lead to trouble. ..
Background image of page 12
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!”); With the semicolon, the print statement will execute regardless of the value of x
Background image of page 13

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
Background image of page 14
Compound Statements if (hours > 40) { hours = hours - 40; overtimePay = hours * 12.0; totalPay = (40 * 8.0) + overtimePay; }
Background image of page 15

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

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

This note was uploaded on 03/29/2012 for the course CSE 110 taught by Professor Shaunakpawagi during the Spring '08 term at SUNY Stony Brook.

Page1 / 87

05-java-control-structures - Java Control/Data Structures...

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

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