Java : Looping Statements


Programming language loops are control flow statements used when need to execute a set of instructions repeatedly when some conditions become true. There are three types of loops in java.

for loop

The for loop is used to iterates a part of the programs multiple times. It’s most preferred to use when the number of iteration is fixed.

For Detail Information follow: Java: for loop

Syntax


for(initialization;condition;increment/decrement){  
// code to be executed 
}

Example

//for loop
for(int i=1; i<=15; i++){
System.out.println(i);
}

The syntax for infinitive loop

for(;;){
//code to be executed
}

while loop

The while loop is used to executes a part of the programs repeatedly on the basis of given boolean condition. It’s most preferred when the number of the iteration is not fixed.

For Detail Information follow: Java: while loop

Syntax


while(condition){  
//code to be executed 
}

Example

//while loop
int i=1;
while(i<=10){
System.out.println(i);
i++;
}

The syntax for infinitive loop

while(true){
//code to be executed
}

do-while loop

The do-while loop is used to executes a part of the programs at least once and the further execution depends upon the given boolean condition. It’s preferred when the number of iteration is not fixed and you must have to execute the loop at least once.

For Detail Information follow: Java: do-while loop

Syntax

     
do{  
//code to be executed  
}while(condition); 

Example

//do-while loop
int i=1;
do{
System.out.println(i);
i++;
}while(i<=15);

The syntax for infinitive loop

do{
//code to be executed
}while(true);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s