UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Java For Loop Control Statement

Instruction and program for how for loop is using in java

by Krishna viswambharan


Posted on 25 Jul 2018 Category: Java Views: 18

Edited on 26 Jul 2018


Java 'For' loop Control Statement

In the previous article, we have discussed the  if control statement. So, in this section, we will discuss another versatile control statement: ‘for’ loop control statement.

As we know that, loop statements are an important part of any programming language. Java is no exception. The handiest is the ‘for’ loop. The ‘for’ loop syntax is the same as the other languages. The simplest form or the syntax of the for loop statement is shown here.



for(initialization;condition;iteration)
{
      statement;
}

The initialization portion of the loop sets a loop control variable to an initial value.

The condition is a Boolean expression that tests the loop control variable. If the outcome of that test is true, then the loop continues to iterate. If it is false, the loop terminates.

The iteration expression determines how the loop control variable is changed each time the loop iterates. The increment operator (++) increases its operand by one, while the decrement(--) decreases its operand by one.  

Here is a small program, which explains the ‘for’ loop control statement using the increment operator.

class ForLoopTest 
{
   public static void main(String args[]) 
  {
     int x;
     for(x = 0; x<10; x = x+1)
          System.out.println("Value of x: " + x);
  }
}

In this example, x is the loop control variable. It is initialized to zero in the initialization portion of the for loop. At the start of each iteration, the conditional test (x < 10 ) is performed. If the outcome of this test is true, the println( ) statement is executed, and then the iteration portion of the loop is executed, that is the value of X is incremented using increment(++) operator. This process continues until the conditional test is false.

The keywords used in the above example such as class, static, void, public etc.. are explained in the previous java program article.

OUTPUT



Leave a Comment:


Advertisement