{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec03_StatementsFlow_2per

# Lec03_StatementsFlow_2per - 1 EECS EECS EECS EECS EECS EECS...

This preview shows pages 1–4. Sign up to view the full content.

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 EECS285 Lecture 03 Statements and Flow Van der Linden Ch. 4 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 2 Andrew M. Morgan Expressions • Expression: a sequence of tokens that can be evaluated to a numerical quantity • Expressions result in some value • Example expressions – 5 (value: 5) – 5 + 10 (value: 15) – a < 15 (value: depends on value of a) – (intVar >= 15 && intVar <= 30) (value: depends on value of intVar) – 2 * y - i /2 (value: depends on values of i and y) – x = 17 (value: 17) 2 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 3 Andrew M. Morgan Statements • Statement: a sequence of tokens recognized by the compiler as a complete instruction • A statement does not have a value • Example statements – x = 5; – System.out.println("Hello world!"); – a = 14.8 + fvar; – i++; – if (a == 4) { b = 6; } EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 4 Andrew M. Morgan Blocks • Syntax of many Java constructs allows only one single statement to be used • Blocks allow multiple statements to be combined into what the compiler recognizes as a single statement • Blocks begin with open curly brace and end with closing curly brace x = 5; a = 14.8 + fvar; i++; { x = 5; a = 14.8 + fvar; i++; } 3 Statements 1 Statement (1 Block containing 3 statements) 3 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 5 Andrew M. Morgan If Statement • Each statement can only be one single statement • Could use a block to put multiple statements in the body of an if or else. if (expression) statement else statement int x = 4; if (x == 4) { System.out.println("x was 4!!"); } else { System.out.println("x was not 4!!"); System.out.println("It was: " + x); } Single statement only. (Used block) Single statement only. (Used block) x was 4!! Note: “else” portion is optional • Used for conditional branching • If-else syntax One statement. Entire if-else construct can be used where a single statement is expected. EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 6 Andrew M. Morgan Nested If-Else Statements int x = 4; if (x == 3) { System.out.println("x was 3!!"); } else if (x == 4) { System.out.println("x was 4!!"); } else { System.out.println("x not 3 or 4"); } int x = 4; if (x == 3) { System.out.println("x was 3!!"); } else if (x == 4) { System.out.println("x was 4!!"); } else { System.out.println("x not 3 or 4"); } This is ONE if statement. Any single statement can be used in the body of an if-else construct....
View Full Document

{[ snackBarMessage ]}

### Page1 / 10

Lec03_StatementsFlow_2per - 1 EECS EECS EECS EECS EECS EECS...

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

View Full Document
Ask a homework question - tutors are online