Monday 25 January 2010

Difference Between “FOR” and “WHILE”

In case of For,

General form:

for(initialization; condition; iteration) {

    // body

}

Example:

for( n=10; n>0; n--) {

//body

    System.out.println(n);

}

In case of While,

General form:

initialization;

while(condition) {

    // body

}

Example:

n=12;

while(n>0) {

//body

    System.out.println(n);

n--;

}

No comments:

Post a Comment