Java for loop

For Loop in Java Programming :

  1. For Loop is one of the looping statement in java programming.
  2. For Loop is used to execute set of statements repeatedly until the condition is true.
  3. For Loop checks the contrition and executes the set of the statements , It is loop control statement in java.
  4. For Loop contain the following statements such as “Initialization” , “Condition” and “Increment/Decrement” statement.

for-Loop

Syntax For Loop : Java Programming

for (initialization; condition ; increment) {
    Statement1
    Statement2
    .
    .
    .
    StatementN
}

Live Example : For Loop Statement

class ForDemo {
    public static void main(String[] args){
         for(int i=1; i<11; i++){
              System.out.println("Count is : " + i);
         }
    }
}

Output :

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

Explanation :

1. Initialization

  1. Consider above program , in this program we are able to see that for loop statement contain 3 statements first one is “Initialization”.
  2. ‘i’ is loop control variable used to control the complete loop.
  3. Its value is initialized with 1.
  4. Initialization statement gets executed once.

2. Condition

  1. Inside first step value of i is initialized .
  2. After initialization the value of control variable is checked against the condition present inside “Condition”.
  3. i < 11 is true condition so that body part of the loop gets executed and it print the Count.
  4. As soon as it finishes the execution of the body part it will jump to the “Increment/Decrement” statement.

3. Increment / Decrement

  1. It will alter the value of Control variable by incrementing it or by decrementing it.
  2. After alteration it again executes “Condition” of the loop.
  3. Again if the condition evaluates to be true then it will again executes the body part as long as condition remains true.

Keep in mind :

  1. Initialization expression initializes the loop
  2. Initialization expression executed once when the loop begins.
  3. When the Condition expression evaluates to false, the loop terminates.
  4. The Increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

Consider Following Program :

class Sample {
    public static void main(String[] args){
         for(int i=1; i ; i++){
              System.out.println("Count is : " + i);
         }
    }
}

In Java Integer is not an True Expression :

  1. It will throw error since In Java we cannot consider Positive integer as “True”.
  2. We must use comparison operator in order to make expression True.
  3. We can Use boolean Operators instead.
C:Priteshjava>javac Sample.java
Sample.java:3: incompatible types
found   : int
required: boolean
         for(int i=1; i ; i++){
                      ^
1 error

Legal and Illegal Ways of For Loop :

Example 1 : Compile Error

public static void main(String[] args){
         int exp;
         for(int i=1; exp ; i++){
              System.out.println("Count is : " + i);
         }
    }

Example 2 : Error Free

public static void main(String[] args){
         boolean exp;
         for(int i=1; exp ; i++){
              System.out.println("Count is : " + i);
         }
    }