Loops in Java

 

Loops

         It helps to repeat a statement a certain number of times until the condition is satisfied.

Three types of Loops

         for loop

         while loop

         do…while loop

for loop

         When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

Syntax

for (statement 1; statement 2; statement 3)

 {
  // code block to be executed
}

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

Statement 2 defines the condition for executing the code block.

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

Example

The example below will print the numbers 0 to 4:

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

{
  System.out.println(i);
}

Output:

0

1

2

3

4


package Looping_Stat;



import java.util.Scanner;

public class for_loop {

public static void main(String args[])

{

System.out.println("Enter The Limit : ");

Scanner in =new Scanner(System.in);

int n=in.nextInt();

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

{

System.out.println(i);

}

}

}



package Looping_Stat;

public class nested_for {

public static void main(String args[]) {

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

{

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

{

System.out.print("*");

}

System.out.println("");

}

}

}



while loop

The while loop loops through a block of code as long as a specified condition is true:

Syntax:

Initialization;

while (condition)

 {
  // code block to be executed

Increment/decrement;
}

Example

In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:

int i = 0;
while (i < 5)

{
  System.out.println(i);
  i++;
}


Output: 0 1 2 3 4


package Looping_Stat;

import java.util.Scanner;

public class while_loop {

public static void main(String args[])

{

System.out.println("Enter The Limit : ");

Scanner in =new Scanner(System.in);

int n=in.nextInt();

int i=1;

while(i<=n)

{

System.out.println(i);

i++;

}

}

}



do…while loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax

Initialization;

do 

{
  // code block to be executed

Increment/decrement;
}
while (condition);

Example:

int i = 0;
do {
  System.out.println(i);

   i++;
}
while (i < 5);

Output:

0

1

 

2

3

4


package Looping_Stat;

import java.util.Scanner;

public class do_while {

public static void main(String args[])

{

System.out.println("Enter The Limit : ");

Scanner in =new Scanner(System.in);

int n=in.nextInt();

int i=2;

do {

System.out.println(i);

i=i+2;

}while (i<=n);

}

}



Enhanced for loop:

The enhanced style for can only cycle through an array sequentially,

 * from start to finish. It is also known as the enhanced for loop.

 * The enhanced for loop was introduced in Java 5 as a simpler way to

 * iterate through all the elements of a Collection.

 * It can also be used for arrays,

 * as in the above example, but this is not the original purpose.*/

 public class Enhanced_for {

    public static void main(String args[])

    {

        int numbers[]={10,20,30,40,50,60,70};

        for(int n : numbers)

        {

            System.out.println(n);

        }

    }

}

 

Unconditional Statements:

break – it temporarily terminates the program.

continue – skips the value  


package Looping_Stat;


public class break_continue {

public static void main(String args[]) {

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

if(i==5)

continue;

System.out.println(i);

if(i==8)

break;

}

}

}

Post a Comment

0 Comments