Java Reference
In-Depth Information
3. You should automatically have the basis for the class body shown here:
public class DoWhileLoop {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
If not, you can type it yourself. You do not need the comments, which are denoted with /** or
// . In Eclipse, they will appear as blue or green text. Comments are useful for explaining what
the code is doing, but are never compiled or executed by Java.
public class DoWhileLoop {
public static void main(String[] args){
}
}
4. Recall that the main method provides the starting point and ordering for the execution of
your program. A do while loop requires a variable to be initialized before the loop so that it
can be evaluated as part of the conditional expression. Initialize an integer with the following
statement:
int i = 1;
It should be placed after (String[] args){ and before the next } .
5. Now add the following do while loop immediately following the int declaration.
do {
} while (i <= 10);
6. Now insert the statements that will be executed during each loop. On each iteration, you multiply
the value of the current array entry by 2 (doubling it) and then print a string containing the result-
ing value to the console. Use the following statements to do so:
int doubled = i * 2;
System.out.println(i + " times two equals " + doubled);
Place these statements between the { } of the do while loop.
7. Remember, you must alter the value of the variable used in the conditional expression during the
loop to avoid infinite looping. You may add it anywhere within the loop, but keep in mind that the
statements are executed from top to bottom, so if you change the value of i before doubling and
printing, you will double and print the new value. Add the following line after the System.out.
println() line, but before the next } .
i++;
Search WWH ::




Custom Search