Using loops in Java

You can use loops to re-run a particular code segment until it satisfy a certain condition.

Basically there are 3 basic types of loops in Java.

  • While loop
  • For loop
  • Do-While loop

While Loop

Syntax :

while(){

 

}

For example :

while

x++ => increment the value of x by one

The printing statement will run 11 times until the condition becomes false.

For Loop

Syntax :

for(;;){

 

}

Capture2

The code will execute until the condition becomes False.

Do-While Loop

Syntax:

do{

 

}while();

This loop can be used to run a loop at least one times.

do

Likewise you can use the loops as you need to run over the same set of lines again and again.

Enhanced for loop will be discussed later.

Thank you.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s