For Loop in Java - Java For Loop Examples & Syntax

The for loop is the type of looping construct. It also works as while loop
construct but it provide the initialization, condition and the increment
is same written in the for construct. All the statements which has to be
executed written in the for block. We can use the multiple for
loop in a program or a for loop also. When we write a loop under another loop
then the second loop is called nested loop.
The Syntax for the For Loop:
for( initialization; termination; increment)
{
statements;
}
The given example illustrates that how to use the for
loop for developing a application or a program. In this program we will see that
the initialization of the variable, condition and increment used for the for
loop. Initialization, Condition and Incrementation, all are optional but two semicolons
are compulsory which separates Initialization, Condition and Incrementation.
Initialization: It allows the variable to be
initialize. Such as:
int i = 1;
int j = 1;
Termination (or
condition): It allows to check the certain condition.
If condition is true then all statements and processes written in the for block
will be executed
otherwise ignored. Condition such as:
i <= 5;
j <= i;
Increment: It allows the how much increase the given
variable. Such as:
i++;
j++;
Code of the program :
public class ForLoop{
public static void main(String[] args){
for(int i = 1;i <= 5;i++){
for(int j = 1;j <= i;j++){
System.out.print(i);
}
System.out.println();
}
}
}
|
Output of the program :
1
22
333
4444
55555
Download For Loop Example

|
Current Comments
6 comments so far (post your own) View All Comments Latest 10 Comments:Hey Deepak,
Thank you very much for responding to my request. You have been very helpful.
hope to keep in touch with you again.
Thanks again.
Norman
Posted by Norman on Friday, 09.26.08 @ 08:44am | #80711
_____1
____232
___34543
__4567654
_567898765
Posted by Lester on Saturday, 09.20.08 @ 17:24pm | #80570
can somebody please help me how to make a java program that will give the following results?
***
***
***
*****
*****
*****
i will really, really appreciate it...
thanks a lot in advance...
Norman
Posted by Norman on Monday, 09.8.08 @ 13:29pm | #77967
Can anyone help me with creating a Java output like this please:
1 2 3
4 5 6
Posted by harvey13 on Wednesday, 08.13.08 @ 22:35pm | #72697
i want to know how to exit from for loop if a condition is true.
reply
Posted by sandeep on Wednesday, 06.11.08 @ 10:18am | #62997
send me java loop examples
Posted by Maximus on Sunday, 02.10.08 @ 19:38pm | #47788