Home Java Loops in java

Loops in java

7 min read
0
0
289

Loop are use to print the block of code data again by again.In Java Consist of 3 Loops.

1.for loop

2.while loop

3.do while loop

for loop

Public class Loop1{
   public static void main(String[] args) 
    {
        for(int x=1; x<10; x++)
        {
            System.out.println(x);
        }
    }
}

Output

1
2
3
4
5
6
7
8
9

While loop

Public class Loop1{ 
public static void main(String[] args) 
{
   int x=0;
   while(x<10)
    {
      x++;
      System.out.println(x);
    } 
 }

Output

1
2
3
4
5
6
7
8
9
10

Do while Loop

Public class Loop3
{
   public static void main(String[] args) 
{
      int x=0;
      do{
          System.out.println(x);
           ++x;
        }
      while(x<10);
 }
}

Output

0
1
2
3
4
5
6
7
8
9

Exercises

Public class Loop4
{
   public static void main(String[] args) 
{
     int a;
     for(a=1; a<10; a++)
     {
         System.out.println(a*a);
     }
}
}

Output

1
4
9
16
25
36
49
64
81

Public class Loop5
{
   public static void main(String[] args) 
{
        int x;
        for(x=1; x<10; x++)
        {
            System.out.println((x*x) + 10 + (x*x) );
        }

}
}

Output

12
18
28
42
60
82
108
138
172

Public class Loop6
{
   public static void main(String[] args) 
{
        int x,y;
        for(x=1,y=1; x<10; x++,y++)
        {
            System.out.println(x+y);
        }
}
}

Output

2
4
6
8
10
12
14
16
18

Public class Loop7
{
   public static void main(String[] args) 
{
       int x,y,z;
       
       for(x=1,y=1,z=1; x<10; x++,y++,z++)
       {
           System.out.println(x*y*z);
       }

}
}

Output

1
8
27
64
125
216
343
512
729

Public class Loop8
{
   public static void main(String[] args) 
{
       int x,y,z;
       
       for(x=1,y=1,z=1; x<10; x++,y--,z--)
       {
           System.out.println(x*y*z);
       }


}
}

Output

1
0
3
16
45
96
175
288

Public class Loop9
{
   public static void main(String[] args) 
{
      int x=0;
      int y=10;
       
       while(x<10)
       {
           x++;
           System.out.println(x*y);
       }
}
}

Output

10
20
30
40
50
60
70
80
90
100

Public class Loop9
{
   public static void main(String[] args) 
{
     int x=0;
     int y=10;
       
       while(x>-10)
       {
           x--;
           System.out.println(x*y);
       }
}
}

Output

-10
-20
-30
-40
-50
-60
-70
-80
-90
-100

Public class Loop9
{
   public static void main(String[] args) 
{
        int x=1;
        int y=2;
        int z=3;
        while((z=x++)<10)
       {
           y++;
           System.out.println("******");
           System.out.println(x*y);
       }
}
}

Output

******
6
******
12
******
20
******
30
******
42
******
56
******
72
******
90
******
110

Public class Loop9
{
   public static void main(String[] args) 
{
       int x=5;
       int y=3;
       int z=2;
       
       while(x>1)
       {
           x--; y--; z--;

           System.out.println("x= " +x);
           System.out.println("y= " +y);
           System.out.println("z= " +z);    
       }
}
}

Output

x= 4
y= 2
z= 1
x= 3
y= 1
z= 0
x= 2
y= 0
z= -1
x= 1
y= -1
z= -2

Public class Loop10
{
   public static void main(String[] args) 
{
        int x=1;
        int y =2;
        
        do{ 
           System.out.println((x++)+(y++));
          }
           while(x<10);
          }
}
}

Output

3
5
7
9
11
13
15
17
19

Public class Loop10
{
   public static void main(String[] args) 
{
        int x=1;
        int y =2;
        
        do{ 
           System.out.println((x++)+(y++));
          }
           while(x<10);
          }
}
}

break Keyword

Public class Loop11
{
   public static void main(String[] args) 
{
       for(int x=1; x<10; x++)
        {
            System.out.println(x);
            if(x==5)
            {
                break;
            }
        }
}

Output

1
2
3
4
5

Public class Loop12
{
   public static void main(String[] args) 
{
      int x=1;
      int y=2;
      
      do{
          if(x==3)
           break;
          System.out.println((x++)+(y++));
      }while(x<10);
    }
}

Output

3
5

Public class Loop13
{
   public static void main(String[] args) 
{
       for(int x=1; x<10; x--)
        {
            System.out.println(x);
            if(x==-10)
                break;
        }   
    }
}

Output

1
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10

Continue Keyword

public class Loop14
{
    public static void main(String[] args) {
        for(int x=1; x<10; x++)
        {
            if(x==3)
           
                continue;
                System.out.println(x);
        }
    } 
}

Output

1
2
4
5
6
7
8
9

public class Loop15
{
    public static void main(String[] args) {
        for(int x=5; x<10; x++)
        {
            if(x==7)
           
                continue;
                System.out.println(x);
        }
    } 
}

Output

5
6
8
9

 

Example 17

class Example{
public static void main(String args[]){
for(int i = 0 ; i < 3 ; i++)
{
  System.out.println("* * * *");
}
}
}

Output

* * * *
* * * *
* * * *

Example 18

class Example{
public static void main(String args[]){
 for(int i = 0 ; i < 5 ; i++)
 {
 for(int j = 0 ; j <= i ; j++){
  System.out.print("* ");
 }
System.out.println();
}
}
}

Output

*
* *
* * *
* * * *
* * * * *

Find the Maximum Number

import java.util.*;
class Example{
 public static void main(String args[]){
  Scanner input = new Scanner(System.in);
   int number = 0;
   int max = 0;
   for(int i = 0 ; i < 5 ; i++){
   System.out.print("Enter Number " + (i + 1) + " : ");
   number = input.nextInt();
 if(max < number){
   max = number;
 }
 }
  System.out.println();
  System.out.println("Maximum Number : " + max);
}
}

Output

Enter Number 1 : 4
Enter Number 2 : 5
Enter Number 3 : 6
Enter Number 4 : 7
Enter Number 5 : 8

Maximum Number : 8

 

 

Load More Related Articles
Load More By admin
Load More In Java

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

Build a Full-Stack Node.js MongoDB CRUD App with JWT Authentication

In this tutorial, we will teach the process of building a full-stack application using Nod…