- /**
- * LoopExamples
- *
- * @author T K Rogers
- * @version 01-04-06
- */
- public class LoopExamples {
- public static void main ( ) {
-
double output =
0 ;
-
int x ;
-
double array [ ]
= { 20.0, 2.0, 3.0 } ;
-
- // The output for each of the following
loops is the same
- // for loop example
- for ( x = 0; x <
3; x++ ) {
-
output += array [x] ;
-
System.out.print ( array[x] + ", " ) ;
- }
-
System.out.println ( "____for loop: output = " + output +
"\n" ) ;
- output = 0 ;
-
-
// while loop
example
- x = 0 ;
- while ( x < 3 ) {
-
output += array [x] ;
-
System.out.print (array [x] + ", " ) ;
-
x++ ;
- }
-
System.out.println ( "____while loop: output = " + output +
"\n" ) ;
- output = 0 ;
-
-
// do...while
loop example
- x = 0;
- do {
-
output += array[x];
-
System.out.print ( array[x] + ", " ) ;
-
x++;
- } while ( x < 3 ) ;
-
System.out.println ( "____do...while loop: output = " + output +
"\n" ) ;
- output = 0 ;
-
-
// foreach loop
example
- // note that the double i
represents the value of the
- // array at an index of i.
- for ( double i:
array ) {
-
output += i ;
-
System.out.print ( i + ", " ) ;
- }
-
System.out.println ( "____foreach loop: output = " + output +
"\n" ) ;
- output = 0 ;
- }
- }