Java For Loop (With Examples)
Java For Loop (With Examples)
Try (https://programiz.pro/learn/master-java?
PRO
utm_source=right-floating-
Search tutorials and examples
for (/)
FREE block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
(https://srv.carbonads.net/ads/click/x/GTND42JNCV
ADS VIA CARBON
segment=placement:wwwprogramizcom;)
(HTTP://CARBONADS.NET/?
M_SOURCE=WWWPROGRAMIZCOM&UTM_MEDIUM=AD_VIA_LINK&UTM_CAMPAIGN=IN_UNIT&UTM_TERM=CARBON)
for loop
Here,
Try (https://programiz.pro/learn/master-java?
PRO
utm_source=right-floating-
Search tutorials and examples
for (/)
FREE block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
class Main {
int n = 5;
// for loop
System.out.println("Java is fun");
}
}
Output
Try (https://programiz.pro/learn/master-java?
PRO
utm_source=right-floating-
Search tutorials and examples
for (/)
FREE block&utm_campaign=programiz&utm_medium=referral)
Java is fun
www.domain-name.com
Java is fun
Java is fun
Java is fun
Java is fun
Condition: i
Iteration Variable Action
<= n
Java is fun is
i = 1
n = 5
i is increased to 2.
Java is fun is
i = 2
n = 5
i is increased to 3.
Java is fun is
i = 3
n = 5
i is increased to 4.
Java is fun is
i = 4
n = 5
i is increased to 5.
Java is fun is
i = 5
n = 5
i is increased to 6.
i = 6
The loop is
6th false
n = 5 terminated.
Try (https://programiz.pro/learn/master-java?
PRO
utm_source=right-floating-
Search tutorials and examples
for
class Main {
(/) block&utm_campaign=programiz&utm_medium=referral)
FREE
public static void main(String[] args) {
www.domain-name.com
int n = 5;
// for loop
System.out.println(i);
}
}
Output
5
Hereour
Thank you for printing is how the
content atprogram works.
www.domain-name.com. Please check back soon for new contents.
Try (https://programiz.pro/learn/master-java?
PRO
Iteration
for
tutorials Condition:
utm_source=right-floating-
Search
Variable
i
and examples Action
(/) block&utm_campaign=programiz&utm_medium=referral)
<= n
FREE
www.domain-name.com
i = 1
1 is printed.
1st true
n = 5 i is increased to 2.
i = 2
2 is printed.
2nd true
n = 5 i is increased to 3.
i = 3
3 is printed.
3rd true
n = 5 i is increased to 4.
i = 4
4 is printed.
4th true
n = 5 i is increased to 5.
i = 5
5 is printed.
5th true
n = 5 i is increased to 6.
i = 6
The loop is
6th false
n = 5 terminated.
(/) block&utm_campaign=programiz&utm_medium=referral)
FREE
public static void main(String[] args) {
www.domain-name.com
int sum = 0;
int n = 1000;
// for loop
for (int i = 1; i <= n; ++i) {
Output:
Sum = 500500
(/) block&utm_campaign=programiz&utm_medium=referral)
FREE
public static void main(String[] args) {
www.domain-name.com
int sum = 0;
int n = 1000;
// for loop
for (int i = n; i >= 1; --i) {
Try (https://programiz.pro/learn/master-java?
PRO
utm_source=right-floating-
Search tutorials and examples
for
class Main {
(/) block&utm_campaign=programiz&utm_medium=referral)
FREE
public static void main(String[] args) {
www.domain-name.com
// create an array
System.out.println(number);
}
}
Output
-5
class Infinite {
int sum = 0;
System.out.println("Hello");
}
}
Next Tutorial:
Java for-each (/java-programming/enhanced-
for-loop)
Loop
Previous Tutorial:
(/java-programming/switch-
Java switch
statement)
Statement
Share on:
(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/inte
u=https://www.programiz.com/java- text=Check%20this%20
programming/for-loop) programming/for-loop)
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
Try (https://programiz.pro/learn/master-java?
PRO
utm_source=right-floating-
Search tutorials and examples
forDid you find this article helpful?
(/) block&utm_campaign=programiz&utm_medium=referral)
FREE
www.domain-name.com
Related Tutorials
Java Tutorial
(/java-programming/do-while-loop)
Java Tutorial
(/java-programming/enhanced-for-loop)
Java Tutorial
(/java-programming/continue-statement)