The for statement is the third and perhaps
the most commonly used looping statement. This statement includes an expression
that specifies an initial value for an index, another expression that
determines whether the loop is continued or not and a third expression that
allows the index to be modified at the end of each pass. Syntax –
for(initial expression ; test expression ;
update expression)
{
Code/s
to be executed ;
}
The
initial expression is initialized only once at the beginning of the for loop.
Then the test expression is checked by the program. If the expression is false,
for loop is terminated. But, if test expression is true then the codes are
executed and update expression is updated, again the test expression is
checked. If it is false, loop is terminated and if it is true the same process
repeats until test expression is false.
Example –
public class
Loop2 {
public static void
main(String[] args) {
int i,a=10;
for(i=1;i<=a;i++)
{
System.out.print(i+" ");
}
}
}
Output –
1 2 3 4 5 6 7 8 9 10
No comments:
Post a Comment