N. Achyut java For Loop in Java | Basic Java Tutorial

For Loop in Java | Basic Java Tutorial

For Loop in Java | Basic Java Tutorial

In this page you are going to learn about for loop in java, When you know how many times you want to loop through a block of code then use for loop.

A loop statement in java allows us to execute a block of code or statements multiple times.

Basically there are Three different types of loops
1. While Loop
2. Do While Loop and
3. For loop

In this session we are going to learn about For loop structure with it’s example

Syntax : FOR LOOP

for (statement 1; statement 2; statement 3) {
  // code block to be executed
 }

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

Example : Program to execute hello world five times and sum of number from 1- 10

package practice_07;

public class ForLoop 
{
    public static void main(String[] args) 
    {
        for(int i= 1; i<=5; i++)
        {
            System.out.println("Hello");
        }
        
        int sum =0;
        for(int i=1; i<=10; i++)
        {
            sum=sum+i;
        }
        System.out.println("Sum=" + sum);
    }
    
}

Related Post