package forExamples; /* * Created on Apr 25, 2005 * * DoWhileEx1.java * @author: hornick */ /** * @author hornick * * illustrates some for() loop constructs */ public class ForLoopEx1 { public static void main(String[] args) { String s = "Hello SE1011"; int i; // we need to declare i outside the for() loop so that we can use it outside the loop after the end for( i=1; // initialization happens only once, when the for-loop is first started i<=10; // this check happens at the beginning of the loop; the loop is executed only if it's true i=i+1 ) { // this action is executed each time at the bottom of the loop // i=1 is effectively executed here ONLY ONCE // the check for i<10 is done here each time the loop is executed double x = Math.pow(i, 0.5); // compute the square root of i System.out.printf("Value of square root of i in the loop is %8.3f \n", x ); // i=i+1 effectively is executed here } System.out.println("The value of i after the loop is " + i ); } } /* The equivalent while() loop would be: int i=1; while( i<=10 ) { double x = Math.pow(i, 0.5); // compute the square root of i System.out.printf("Value of square root of i in the loop is %8.3f \n", x ); i=i+1; } System.out.println("The value of i after the loop is " + i ); } */