JAVA programs

java program using for loop

In this program we demonstrate use of for loop. For loop is a flow control statement that allows statement block to be executed repeatedly, when we know exact number of times we want to repeat code then for loop is used.

For loop is widely used in many programming language.

Syntax of for loop :

 

for (initialization; condition; increment/decrement) {

  // code lines (block) to be executed

}

 

Initialization: initialize value of variable, executed before the execution of the code block (only one time for loop).

condition: the condition for executing the code block.

Increment / decrement: increment or decrement value of identifier, executed after the code block has been executed (every time after code).

Example:

for (int x = 0; x < 5; x++) {

  System.out.println(x);

}

Program

public class ForLoopDemo

{

    public static void main(String[] args)

    {

        for(int i=1;i<=5;i++)

        {

            for(int j=0;j<i;j++)

        {

 

            System.out.println(j+1);

          }

            System.out.println(" ");

            }

    }

}

When Run The Program Following Output:

See Following window

Description

Releted Programs

© 2017 codewmAll rights reserved.