Statements

Statements - Statement Blocks A Compound statement(or block is a set of statements enclosed in braces cfw int x = 5 double y = readDouble"y

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

View Full Document Right Arrow Icon
1 Statement Blocks • A Compound statement (or block) is a set of statements enclosed in braces { int x = 5; double y = readDouble("y: "); println("y = " + y); } • Variable’s scope is block in which it is declared Scope : lifetime of variable • When the variable is available to be used if statement General form: if ( condition ) { statements } if ((num % 2) == 0) // can omit braces println("num is even"); // if one statement if ((num % 2) == 0) { println("num is even"); println("Oh, and Beat Cal!"); } • Use braces with if with more than one statement • Good idea to use braces (block) even if there is only one statement in the if Any boolean condition/variable if-else statement General form: if ( condition ) { statements } else { statements } if ((num % 2) == 0) { println("num is even"); } else { println("num is odd"); println("and so are you"); } Cascading if if (score >= 90) { println("A"); } else if (score >= 80) { println("B"); } else if (score >= 70) { println("C"); } else { println("Bad Times"); } switch statement int day = readInt("Day of week as int: "); switch (day) { case 0: println("Sunday"); break; case 6: println("Saturday"); break; default:
Background image of page 1

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

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

This note was uploaded on 10/25/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 3

Statements - Statement Blocks A Compound statement(or block is a set of statements enclosed in braces cfw int x = 5 double y = readDouble"y

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

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