For loop in Java
Java for loop
In Java, for loop is similar to C and C++. It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of code. We use the for loop only when we exactly know the number of times, we want to execute the block of code.
The flow chart for the for-loop is given below.
Consider the following example to understand the proper functioning of the for loop in java.
//print "I love my India" 10 times
Output:
I love my India
I love my India
I love my India
I love my India
I love my India
I love my India
I love my India
I love my India
I love my India
I love my India
//print 1 to 10
Output:
1
2
3
4
5
6
7
8
9
10
//print all even number from 1 to 10
Output:
2
4
6
8
10
//print all even numbers from 1 to 100 which are divisible by 5
Output:
10
20
30
40
50
60
70
80
90
100
Comments
Post a Comment