# Week4 - Weeks 4 and 5 (notes for chapters 4 and 5) if...

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

Weeks 4 and 5 (notes for chapters 4 and 5)

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

View Full Document
if Statements if (boolean condition) statement 1; else statement 2; if (boolean condition) {multiple statements} else {multiple statements} if (boolean condition 1) if (boolean condition 2) if (boolean condition 3) statement 1; else statement 2; else statement 3; else statement 4; Relational Operators: = = ! = > < > = < = Logical Operators: && | | Write code fragment to assign into biggest the largest value of a, b, c.
How to Assign the largest value of a, b, and c into the variable biggest using the conditional expression operator biggest = (a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c);

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

View Full Document
while Statement while (condition) { body of the loop } // Demonstrate the while loop. class While { public static void main( String args[ ]) { int n = 10; while (n > 0) { System.out.println(“tick ” + n); n--; } System.out.println(“Lift Off!”); } } Output: tick 10 tick 9 tick 8 tick 7 tick 6 tick 5 tick 4 tick 3 tick 2 tick 1 Lift off!
// The target of a loop can be empty. class NoBody { public static void main(String args[ ]) { int i = 100, j = 200; // find midpoint between i and j while (++i < --j) ; // no body in this loop System.out.println(“Midpoint is ” + i); Output: Midpoint is 150

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

View Full Document
do-while Statement do { // body of loop } while ( condition ); // Demonstrate the do-while loop. Class DoWhile {
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/12/2009 for the course CS 120 taught by Professor Robertdependahl during the Fall '09 term at Santa Barbara City.

### Page1 / 15

Week4 - Weeks 4 and 5 (notes for chapters 4 and 5) if...

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

View Full Document
Ask a homework question - tutors are online