Java Loop Control Statement

Java Loop Control Statement


Posted in : Core Java Posted on : September 28, 2010 at 12:39 PM Comments : [ 0 ]

Java Loop Control Statement

Java Loop Control Statement

When a block of code executes number of times, we referred it as loop.

The three basic loop control statements supported by java are :

  • while loop

  • do...while loop

  • for loop

  • enhance for loop

while loop control statement

The while loop repeats a statement or block while its controlling expression is true. given below it's syntax :

while(condition) {
//body of loop
}

Here, the condition can be any Boolean expression, whose outcome is either true or false. The loop executes if the condition is true. If not, the next line of code executes.

public class Main {
   public static void main(String args[]){
      int a= 10;
      System.out.println("The Changing value of 'a' during execution :");
      while( a < 20 ){
      System.out.print("a : " + a );
      a++;
      System.out.print("\n");
      }
   }
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac Main.java

C:\Program Files\Java\jdk1.6.0_18\bin>java Main

values after loop execution :
x : 10
x : 11
x : 12
x : 13
x : 14
x : 15
x : 16
x : 17
x : 18
x : 19

do...while loop control statement :

This loop is similar to while loop but it executes at least one time. After executing set of code or block every time, it checks the condition - if condition is true then block of code executes , if not - it will not execute. The syntax of this loop control statement is given below :

do
{
   //body of loop
}while(condition);
Given below the sample code :

public class Main {
   public static void main(String args[]){
      int a= 3;
      System.out.println("The Changing value of 'a' during execution :")
      do{
         System.out.print(" a : " + a );
         a++;
         System.out.print("\n");
      }while( a < 10 );
   }
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac Main.java

C:\Program Files\Java\jdk1.6.0_18\bin>java Main

The Changing value of 'a' during execution :
a :3
a :4
a :5
a :6
a :7
a :8
a :9

for loop control statement :

This loop works as follows : At first go, it initialize the variable. After this, it checks the condition, if true then it will execute further. If not , it will terminate loop. After first go, the sequence of execution is as follows : first evaluate the condition, then execution of body of loop, and then executing the iteration expression with each pass.

The syntax given below :

for(initialization; Condition; iteration)
{
   //body
}

Example :

public class Main {
public static void main(String args[]){

for(int a = 10; a < 20; a = x+1){
System.out.print("value of a : " + a );
System.out.print("\n");
}
}
}

Output :
C:\Program Files\Java\jdk1.6.0_18\bin>javac Main.java

C:\Program Files\Java\jdk1.6.0_18\bin>java Main
x : 10
x : 11
x : 12
x : 13
x : 14
x : 15
x : 16
x : 17
x : 18
x : 19

enhance for loop (for-each)

Enhance for loop was added in java 5, which is mainly used for arrays , It is also referred as 'for-each'.  

Syntax is given as follows :

for(type itr-var: collection)
{
   //body
}

Example :

public class Main {
public static void main(String args[]){
int [] array = {10, 20, 30, 40, 50};

for(int a : array ){
System.out.print( a );
System.out.print(",");
}
System.out.print("\n");
String [] list ={"Ankit", "Kapil", "Samita", "Annu"};
for( String li : list ) {
System.out.print( li );
System.out.print(",");
}
}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac Main.java

C:\Program Files\Java\jdk1.6.0_18\bin>java Main
 
10,20,30,40,50,
Ankit, Kapil, Samita, Annu

The type specifies the type and itr-var specifies the name of the iteration variable that will receive the elements from a collection , one at a time, from beginning to end The collection being cycled through is specified by 'collection'. You can use various type of collection with 'for'. Most basic example is array.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics