CS 111 - 9/18/12 Looping - Repeat the same sequence of operations multiple times. Also called "iteration" Types: while, for, do while, "enhanced" for For now, we will only use the while loops Syntax: while ( ) { ; } : evaluates to either TRUE or FALSE while the is TRUE we execute the in the BODY of the loop. The simplest use Relational Operators The Relational Operators include: == Equality != Inequality > Greater Than >= Greater Than or Equal < Less Than <= Less Than or Equal int var1; var1 = 0; var1 == 10 --------------------------- Print out the values from 0 to 10 int count; count = 0; /// initialization line while ( count <= 10 ) { System.out.println (count); count = count + 1; // increment Line } When dealing with loops beware of: 1. Infinite Loop - We forgot the increment line 2. A loop that never executes - We forgot the initialization line 3. A misplaced semicolon // ERROR !!!!!!! // Don't do this count = 0; /// initialization line while ( count <= 10 ) ; // ERROR with semicolon { System.out.println (count); count = count + 1; // increment Line } An example of a "Count down loop" int count; count = 10; /// initialization line while ( count >= 1 ) { System.out.println (count); count = count - 1; // increment Line } Increment Operator ++ Decrement operator -- count = count + 1; count ++; ++ count; int count; count = 0; /// initialization line while ( count < 10 ) { System.out.println (count); count ++; // increment Line } // BEWARE OF THE FOLLOWING count = 5; count = count++; System.out.println (count); count = 7; count = ++count;