You are on page 1of 12

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
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)

Java for Loop


In this tutorial, we will learn how to use for loop in Java
with the help of examples and we will also learn about the
working of Loop in computer programming.

In computer programming, loops are used to repeat a


block of code. For example, if you want to show a
message 100 times, then rather than typing the same
code 100 times, you can use a loop.

In Java, there are three types of loops.

for loop

while loop (https://www.programiz.com/java-


programming/do-while-loop#syntax-while)

do...while loop (/java-programming/do-while-loop#do-


while-loop)
This our
Thank you for printing tutorial focuses
content on the for loop. You
at www.domain-name.com. will check
Please learn back
aboutsoon for new contents.
the
Try other type of loops in the upcoming tutorials.
(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

Java for Loop


Java for loop is used to run a block of code for a certain
number of times. The syntax of for loop is:

for (initialExpression; testExpression; updateExpression)


// body of the loop

Here,

1. The initialExpression initializes and/or declares


variables and executes only once.

2. The condition is evaluated. If the condition is true ,


the body of the for loop is executed.

3. The updateExpression updates the value of


initialExpression.

4. The condition is evaluated again. The process


continues until the condition is false .

To learn more about the conditions, visit Java relational


(/java-programming/operators#equality-relational) and
logical operators (/java-
programming/operators#logical).
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
for (/)
FREE block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com

Flowchart of Java for loop

Example 1: Display a Text Five Times

// Program to print a text 5 times

class Main {

public static void main(String[] args) {

int n = 5;

// for loop

for (int i = 1; i <= n; ++i) {

System.out.println("Java is fun");

}
}

Run Code (/java-programming/online-compiler)


Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.

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

Here is how this program works.

Condition: i
Iteration Variable Action
<= n

Java is fun is
i = 1

1st true printed.

n = 5
i is increased to 2.

Java is fun is
i = 2

2nd true printed.

n = 5
i is increased to 3.

Java is fun is
i = 3

3rd true printed.

n = 5
i is increased to 4.

Java is fun is
i = 4

4th true printed.

n = 5
i is increased to 5.

Java is fun is
i = 5

5th true printed.

n = 5
i is increased to 6.

i = 6
The loop is
6th false
n = 5 terminated.

Example 2: Display numbers from 1 to 5


Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
// Program to print numbers from 1 to 5

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

for (int i = 1; i <= n; ++i) {

System.out.println(i);

}
}

Run Code (/java-programming/online-compiler)

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.

Example 3: Display Sum of n Natural Numbers


Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
// Program to find the sum of natural numbers from 1 to 1
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 sum = 0;

int n = 1000;

// for loop
for (int i = 1; i <= n; ++i) {

// body inside for loop

sum += i; // sum = sum + i

System.out.println("Sum = " + sum);

Run Code (/java-programming/online-compiler)

Output:

Sum = 500500

Here, the value of sum is 0 initially. Then, the for loop is


iterated from i = 1 to 1000 . In each iteration, i is added
to sum and its value is increased by 1.

When i becomes 1001, the test condition is false and


sum will be equal to 0 + 1 + 2 + .... + 1000 .

The above program to add the sum of natural numbers


can also be written as
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
// Program to find the sum of natural numbers from 1 to 1
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 sum = 0;

int n = 1000;

// for loop
for (int i = n; i >= 1; --i) {

// body inside for loop

sum += i; // sum = sum + i

System.out.println("Sum = " + sum);

Run Code (/java-programming/online-compiler)

The output of this program is the same as the Example 3.

Java for-each Loop


The Java for loop has an alternative syntax that makes it
easy to iterate through arrays (/java-
programming/arrays) and collections. For example,
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
// print array elements

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

int[] numbers = {3, 7, 5, -5};

// iterating through the array

for (int number: numbers) {

System.out.println(number);

}
}

Run Code (/java-programming/online-compiler)

Output

-5

Here, we have used the for-each loop to print each


element of the numbers array one by one.

In the first iteration of the loop, number will be 3, number

will be 7 in second iteration and so on.

To learn more, visit Java for-each Loop (/java-


programming/enhanced-for-loop).

Java Infinite for Loop


If weour
Thank you for printing setcontent
the test expression in such aPlease
at www.domain-name.com. way that it back
check neversoon for new contents.
evaluates
Try to false , the for loop will run forever. This is
(https://programiz.pro/learn/master-java?
PRO
called utm_source=right-floating-
infinite for tutorials
Search loop. For example,
and examples
for (/) block&utm_campaign=programiz&utm_medium=referral)
FREE
www.domain-name.com

// Infinite for Loop

class Infinite {

public static void main(String[] args) {

int sum = 0;

for (int i = 1; i <= 10; --i) {

System.out.println("Hello");

}
}

Run Code (/java-programming/online-compiler)

Here, the test expression , i <= 10 , is never false and


Hello is printed repeatedly until the memory runs out.

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 while and do...while Loop

(/java-programming/do-while-loop)

Java Tutorial

Nested Loop in Java


Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
(/java-programming/nested-loop)
Try (https://programiz.pro/learn/master-java?
Java Tutorial
PRO
utm_source=right-floating-
Search tutorials and examples
for (/)
block&utm_campaign=programiz&utm_medium=referral)
Java for-each Loop
FREE
www.domain-name.com

(/java-programming/enhanced-for-loop)

Java Tutorial

Java continue Statement

(/java-programming/continue-statement)

You might also like