knowledgediarybd.com

Your Virtual Knowledge Campus

For loop while loop and Do while loop in java

loop

Java has very flexible three looping mechanisms like C/C++ or other languages.

For Loop,While Loop and  Do while Loop are the most important statement all of them in java programming.They are used in java programming for doing a program very fast & effective.

You can use one of the following three loops in java:

  1. while Loop
  2. do…while Loop
  3. for Loop

1. while loop:

The  while loop is a control structure that allows you to repeat a task a certain number of times.

The while loop you can use it  when you don’t know in advance when the loop will terminate or when you may not want to execute the loop body even once.

Syntax of while loop:

while(Condition)

{

//Statements

}

More details see the figure:

while loop

When executing, if the Condition result is true then the actions inside the loop will be executed. This will continue as long as the expression result is true.

Here key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example with code:

public class Kabir{

public static void main(String args[]) {

int x = 1;

while( x < 5) {

System.out.print("value of x : " + x );

x++;

System.out.print("\n");

}

}

}
if you want to run it on Net Beans You have to save it as Kabir.java because here  public class  is Kabir then you have to press Run file button from the menu.

This would produce following result:

value of x : 1

value of x : 2

value of x : 3

value of x : 4

2.do…while Loop:

A do…while loop is similar to a while loop, except that a do…while loop is guaranteed to execute at least one time that means you can use do loop when you’re sure you want to execute the loop body at least once.

Syntax:

do

{

//Statements

}while(Condition);

More detail syntax of do..while loop:

do while loop

Notice that the Condition appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.

If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process repeats until the Boolean expression is false.

Example with java code:

public class Kabir {

public static void main(String args[]){
int x = 1;

do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 7 );
}
}

if you want to run it on Net Beans You have to save it as Kabir.java because here  public class  is Kabir then you have to press Run file button from the menu.

This would produce following result:

value of x : 1
value of x : 2
value of x : 3
value of x : 4
value of x : 5
value of x : 6

3.for Loop:

A for loop is a repetition of control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax:

for(initialization; Condition; update)

{

//Statements

}

for loop

Here is the flow of control in a for loop:

  1. The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
  2. Next, the Condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop.
  3. After the body of the for loop executes, the flow of control jumps back up to the update statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the Boolean expression.
  4. The Condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then update step,then Boolean expression). After the Boolean expression is false, the for loop terminates.

Example:

public class Kabir {

public static void main(String args[]) {

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

System.out.print("value of x : " + x );

System.out.print("\n");

}

}

}

This would produce following result:

value of x : 1

value of x : 2

value of x : 3

value of x : 4

Enhanced for loop in Java:

As of java 5 the enhanced for loop was introduced. This is mainly used for Arrays.

Syntax:

for(declaration : expression)

{

//Statements

}

  • Declaration . The newly declared block variable, which is of a type compatible with the elements of the array you are accessing. The variable will be available within the for block and its value would be the same as the current array element.
  • Expression . This evaluate to the array you need to loop through. The expression can be an array variable or method call that returns an array.

Example:

public class Kabir {

public static void main(String args[]){

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

for(int x : numbers ){

System.out.print( x );

System.out.print(",");

}

System.out.print("\n");

String [] names ={"Melon", "kabir", "Eisty", "sagor"};

for( String name : names ) {

System.out.print( name );

System.out.print(",");

}

}

}

This would produce following result:

10,20,30,40,50,
Melon,kabir,Eisty,sagor

Note:More interested things are waiting to publish..Stay connected to us..

——————-:Thank you:—————–

Related Post or you may Like:

knowledgediarybd.com © 2017
shares