Sponsered Links
Categories
Sponsered Links

Java for Loop

 

 This example illustrates how to use the for loop in java program. 

In this example we create a java program to print the number of 1-15 by using for loop. Here is the some running example related to for loop. The for loop is the type of  looping construct. It also works as while loop construct but it provide the initialization, condition and the increment  is same written in the for construct. All the statements which has to be executed written in the for block. We can use the multiple for loop in a program or a for loop also. When we write a loop under another loop then the second loop is called nested loop.

Source code of javaforloop.java

class javaforloop{

  public static void main(String[] args){

    for(int i=1;i<=15;i++){

      System.out.println(" "+i);

    }

  }

}

output:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

 

In the below source code we are going to print the number in increasing order, Each time, it prints the number from new line with next number.

 

/*Program for print 1

                    2 1

          3 2 1

          4 3 2 1

          5 4 3 2 1

 */

class javaforloop4{

  public static void main(String[] args){

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

      for(int j=i;j>=1;j--){

        System.out.print(j);

      }

        System.out.println(" ");

    }

  }

}

Output:

 

1

21

321

4321

54321

 

In the below source code we are going to print the number from 4-1 in decreasing order, each time number decreases by 1 and print from new line.

 

/*Program for print 4 3 2 1

                    3 2 1

          2 1

          1

*/

class javaforloop5{

  public static void main(String[] args){

    for(int i=4;i>=1;i--){

      for(int j=i;j>=1;j--){

        System.out.print(" "+j);

      }

        System.out.println(" ");

    }

  }

}

Output

 

4321

321

21

1

 

In the below source code we are going to print the number from 1-3 in decreasing order, each time number decreases by 1and print from new line.

 

/*Program for print 1 2 3

                    2 3

          3

*/

class javaforloop6{

  public static void main(String[] args){

    for(int i=1;i<=4;i++){

      for(int j=i;j<4;j++){

        System.out.print(" "+j);

      }

        System.out.println(" ");

    }

  }

}

Output

 

 1 2 3

2 3

3

 

In the below source code we are going to print the number from 1-4 in increasing order, each time number increases by same number and print from new line.

 

/*Program for print 1

                    2 2

          3 3 3

          4 4 4 4

*/

class javaforloop7{

  public static void main(String[] args){

    for(int i=1;i<=4;i++){

      for(int j=1;j<=i;j++){

        System.out.print(" "+i);

      }

        System.out.println(" ");

    }

  }

}

Output

 

 1

2 2

3 3 3

4 4 4 4

 

In the below source code we are going to print the number from 1 in increasing order, each time number increases by same number and print from new line.

 

/*Program for print 1

                    1 1

            1 1 1

          1 1 1 1

*/

class javaforloop8{

  public static void main(String[] args){

    for(int i=1;i<=4;i++){

      for(int j=1;j<=i;j++){

        System.out.print(" "+"1");

      }

        System.out.println(" ");

    }

  }

}

Output

 

 1

1 1

1 1 1

1 1 1 1

 

 

In the below source code we are going to print the number from 1-4 in increasing order, each time number increases by same number and print from new line and every new line the odd number print with + operator and the even number print with - operator. 

 

/*Program for print 1

                   -2 -2

            3  3  3

           -4 -4 -4 -4

*/

class javaforloop9{

  public static void main(String[] args){

    for(int i=1;i<=4;i++){

      for(int j=1;j<=i;j++){

        if(i%2==0)

        {

        System.out.print(-i);

        }

        else

        {

          

          System.out.print(" "+i);

        }

              System.out.print(" ");

      }

       System.out.println();  

    }

  }

}

Output

 

 1

-2 -2

3 3 3

-4 -4 -4 -4

 

 

In the below source code we are going to print the number from 1-5 in increasing order, each time number increases by next number and print from new line. 

 

/*Program for print 1

                    1 2

          1 2 3

          1 2 3 4 

          1 2 3 4 5 

*/

class javaforloop3{

  public static void main(String[] args){

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

      for(int j=1;j<=i;j++){

        System.out.print(j);

      }

        System.out.println(" ");

    }

  }

}

Output

 

1

12

123

1234

12345

 

 

In the below source code we are going to print the even number up to 15. 

 

//program for print 2 4 6 8 10 12 14

class javaforloop2{

  public static void main(String[] args){

    for(int i=1;i<=15;i++){

      if(i%2==0){

            System.out.print(" "+i);

    }

    }

  }

}

Output

 

 2 4 6 8 10 12 14

 

In the below source code we are going to print the odd number up to 15. 

//Program for print 1 3 5 7 9 11 13 15

class javaforloop1{

  public static void main(String[] args){

    for(int i=1;i<=15;i++){

      if(i%2==1){

            System.out.print(" "+i);

    }

    }

  }

}

  

Output

 

 1 3 5 7 9 11 13 15

 

 

 
 
Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved