- /**
- * Demonstrates that a
for loop can do the job of
- * any other loop.
- *
- * @author TK Rogers
- * @version 1-3-07
- */
- public class
DemoVariousLoops {
-
public static void main ( ) {
-
int x ;
-
- // for-loop simulating a
while loop
-
System.out.println ( "for loop1 " ) ;
- x =
0 ;
// initialize
- for
( ; x < 10 ; ) {
// test increment
-
x += 2 ;
//increment
-
System.out.print ( x + " " ) ;
- }
-
- // for-loop simulating a
do-while loop
-
System.out.println ( "\n\nfor loop2 " ) ;
- x =
0 ;
// initialize;
- for
( ; ; ) {
-
x += 2 ;
// increment
-
System.out.print ( x + " " ) ;
-
if ( x >= 10 ) break ;// test
- }
-
-
System.out.println ( "\n\nwhile loop " ) ;
- x =
0;
// initialize
-
while (x < 10) {
//test
-
x += 2 ;
//increment
-
System.out.print ( x + " " ) ;
- }
-
-
System.out.println ( "\n\ndo while loop " ) ;
- x =
0 ;
// initialize
- do
{
-
x += 2 ;
// increment
-
System.out.print ( x + " " ) ;
- }
while (x < 10) ;
//test
- }
- }