Statements

• Statements

• Statement:

• Statement = a command issued to the computer to do something

• In Java:

 A statement must be contained inside a method

• Types of statements:

 Assignment statement Selection statement Loop statement Method invocation statement (I don't count this as a statement; but a feature....)

• Assignment statement

• Syntax:

 ``` variable = expression ; ```

Example:

 ``` x = 4; // Store the value 4 in variable x x = x + 1; // Read the value in variable x, add 1 to it, // then store result in variable x ```

Note:

 Variables on the Right hand side of the = operator are read (= accessed) The variable on the Left hand side of the = operator is written (= updated)

• Selection statement

• Selection statements

 If statement If-else statement Switch statement

• If statement:

 ``` if ( condition ) one-statement More common form: if ( condition ) { // Block statement1 // Counts as 1 statement statement2 ... } ```

Example:

 ``` // Code to compute the absolute value of x if ( x < 0 ) x = -x; // Negate x if x is negative ```

• If-else statement:

 ``` if ( condition ) one-statement-1 else one-statement-2 More common form: if ( condition ) { // Block statement1-1 // Counts as 1 statement statement1-2 ... } else { // Block statement2-1 // Counts as 1 statement statement2-2 ... } ```

Example:

 ``` // Code to compute the max of x and y if ( x > y ) max = x; // x is the maximum else max = y; // y is the maximun ```

• Switch statement:

 ``` Switch ( integer-expression ) { case intVal1: statement1-1 statement1-2 ... break; case intVal2: statement2-1 statement2-2 ... break; ... default: statementD-1 // Optional clause !!! statementD-2 ... break; } ```

• Loop statements

• Loop statements:

 While statement For statement Do-while statement

• While statement:

 ``` while ( condition ) one-statement More common form: while ( condition ) { // Block statement1 // Counts as 1 statement statement2 ... } ```

Example: find the first pair of numbers (i, i+1) such that i2 + (i+1)2 > 100

 ``` i = 0; while ( i*i + (i+1)*(i+1) <= 100 ) i++; ```

• For statement:

 ``` for ( initialization ; termination-cond ; increment ) one-statement More common form: for ( initialization ; termination-cond ; increment ) { // Block statement1 // Counts as 1 statement statement2 ... } ```

Example: compute 12 + 22 + ... n2

 ``` sum = 0; for ( i = 0; i <= n; i++ ) sum += i*i; ```

• Do-while statement

 ``` do one-statement while ( condition ) More common form: do { // Block statement1 // Counts as 1 statement statement2 ... } while ( condition ) ```

Comment:

 It's hard to find a good example that make use of the while statement....

• Which loop statement should you use ?

• If you know the number of repetitions that some statements will be executed:

 Use a for loop

• If you need to keep perform an operation until something "happens":

 Use a while loop That something than need to "happen" is the termination condition of the while loop !!!

• Pretest question

• What is the output of the following code fragment:

 ``` int sum = 1; for ( int i = 0; i <= 5; sum = sum + i++ ); System.out.print(sum); ```

Solution: (use variables !!!)

 ``` initialization: sum = 1 for statement: iteration 1: i = 0 (0 <= 5) sum = 1 + 0 = 1 iteration 2: i = 1 (1 <= 5) sum = 1 + 1 = 2 iteration 3: i = 2 (2 <= 5) sum = 2 + 2 = 4 iteration 4: i = 3 (3 <= 5) sum = 4 + 3 = 7 iteration 5: i = 4 (4 <= 5) sum = 7 + 4 = 11 iteration 6: i = 5 (5 <= 5) sum = 11 + 5 = 16 iteration 6: i = 6 (6 > 5) Stop: sum = 16 System.out.print(sum) will print: 16 ```