Lecture 7 - The if and if-else Statements General form: If...

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

View Full Document Right Arrow Icon
The if and if-else Statements General form: If ( expression ) statement; else statement; The expression must evaluate to true or false values o Can use any of the relational and logical operators to help form complex expression The else clause is optional Use braces for more than one statement: If ( expression ) { statement1; statement2; ... } else { statement1; statement2; ... } If –else-if ladders are common: if ( condition1 ) statement1; else if ( condition2) statement2; else if (condition3) statement3; . . . else statementN; Eg: o If (a == b) System.out.println(“Equal”); else if (a < b) System.out.println(“a less than b”); else //(a > b) would also work System.out.println(“a greater than b”);
Background image of page 1

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

View Full DocumentRight Arrow Icon
If statements can be nested o Eg: if (x>3) { if (y == 0) System.out.println(“in range”); else System.out.println(“out of range”); } Ternary Operator Also called the conditional operator General form: exp1 ? exp2 : exp3 ; If
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/04/2010 for the course CPSC 233 taught by Professor Li during the Fall '09 term at University of Calgary.

Page1 / 4

Lecture 7 - The if and if-else Statements General form: If...

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

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