Site icon TestingDocs.com

Java program using for loop

Program Description

In this post, we will write a simple java program using for loop. for loop is used for iteration to do repetitive tasks in programs or applications. We may use for loop if we know the number of iteration beforehand. The syntax for the loop is shown below:

Syntax

for(initialization ; condition ; increment/decrement)

{

    statement(s) //block of statements

}

 

Flow Diagram

 

 

The flowchart tool used is Raptor. For more information about the flowchart tool:

https://www.testingdocs.com/raptor-a-flowchart-tool/

 

Java Program

 

public class ForLoopDemo {
  public static void main(String args[])
  {
    int i;
    for(i = 1; i <= 10 ; i++)
    {
      System.out.println("The value of i is:"+i);
    }
  }
}

 

Program Output

The value of i is:1
The value of i is:2
The value of i is:3
The value of i is:4
The value of i is:5
The value of i is:6
The value of i is:7
The value of i is:8
The value of i is:9
The value of i is:10

 

Screenshot

 

Loop using multiple variables

public class ForLoopDemo { 
  public static void main(String args[]) {
  int i,j; 
  for(i = 1, j = 1; i <= 10 && j <= 10; i++, j--) {
     System.out.println("The value of i is:"+i); 
     System.out.println("The value of j is:"+j); 
   } 
 }
}

 

 

 

 

Exit mobile version