CS 111 - 10/9/14 Project 2 - Making 3D images for loops - special form of the while loop The for loop tries to improve on the while loop by keeping the information for the - initialization - condition and - increment together on the same line of code. syntax of the for loop for ( ; ; ) { ; } int counter; for ( counter = 1 ; counter <= 10 ; counter = counter + 1 ) { System.out.println ("Current value: " + counter); } The above for loop can be translated into the following while loop: int counter; counter = 1; while ( counter <= 10 ) { System.out.println ("Current value: " + counter); counter = counter + 1; } New Arthimetic operators: ++ -- ++ increments a value by 1 -- decrements a value by 1 int counter; for ( counter = 1 ; counter <= 10 ; counter++ ) { System.out.println ("Current value: " + counter); } Beware of the ++ and -- operators, they can give strange results We can use these operators in both a prefix and a postfix format. These different formats do slightly different things. If we never using the ++ or -- operators with the = operator, the prefix and postfix difference don't matter. It is always best to use ++ and -- as stand-alone statements. counter++; // postfix notation ++counter; // prefix notation Consider the following code that uses ++ with = int val1 = 5; int val2 = 5; int val3 = val1++; int val4 = ++val2; System.out.println ("Val1: " + val1); System.out.println ("Val2: " + val2); System.out.println ("Val3: " + val3); System.out.println ("Val4: " + val4); Watch the bizarre results of the following int val5 = 5; val5 = val5++; // Error: NEVER DO THIS!!!!! PLEASE!!! System.out.println ("Val5: " + val5); Bottom line: USE ++ AS A STANDALONE OPERATION