Loops in Java with Examples

0

Loops in java are also called iteration statements. Basically, there are three types of iterations statement in java. These are

  1. while statement
  2. do-while statement
  3. For statement

Iteration statement is used when need to execute a block of statement repeatedly.

All three iteration statements differ in the order in which they execute the loop.

while and for loop check the Boolean condition first then only it executes the block of statements while do-while, execute the body of a loop first then checks the condition.

So if programmers want to execute a body of a loop at least once it is recommended to use do-while.

Java has two flavors of For, one is basic For loop and other is enhanced For loop, will discuss in detail later.

For each iteration statement, there should be a termination condition so that loop execution stop, else it will go into an infinite loop.

Loop termination condition must be a Boolean condition, in general, programmers do mistake as given below

Java Loops Example:-

suppose user want to execute some block of statement until counter value is not equal to 10

int counter = 0
while (counter = 10 ) 
{
< statement 1>
}

As the condition in while is counter = 10; this is assignment statement so this will give true, means loop will execute infinite time.

Correct code would be while(counter == 10)

while Statement in Java:-

Syntax:-

while(<condition which must give Boolean result>)

<body for while Loop>

 

First check the condition, if condition gives true then it will execute code for while loop else it will come out from while Loop.

While loop Java Example:-

int counter = 10;
while(counter &gt;= 0) 
{
System.out.println(“Value of counter is “ + counter);
counter--;
}

While loop will run 10 times and in 11th iteration it will come out from the loop.

If there is only one statement for while you can skip the curly braces else you have put curly braces as we done in above example

A while loop without any statement is called empty while

While(<some condition>);                           // Empty Condition

 

do-while statement in Java:-

In a do-while statement, the first statement will execute and then the condition will check.

Syntax:-

do {

// statements

} while(<condition>)

do-while loop java Example:-

int counter = 10;
do {
System.out.println(“Value of counter is “ + counter);
counter--;
}
while(counter &gt; 11)

In above example though the condition is not satisfied as 10 < 11, but the body of the loop will execute once.

for statement in Java:-

Syntax of basic for loop is:

for(<initialization>; <condition>; <incremental/decremental expression>) {

// statements

}

For loop java Example:-

int [] value = {1,2,3,4,5,6,7,8,9,10};
int sum = 0;
for (int counter = 0; counter &lt; 10; counter++)
{
sum += value[counter];
}
System.out.println(“Sum is “ + sum);

In above example for loop will execute 10 times. As the value of counter reached 10 loops will terminate. And print the sum.

Case 1:

We can use multiple initialization in for loop as given below

for (int I = 0, j = 2; j < 12; j++)

In above example there are two variables are getting initialize.

Case 2:

If you are using multiple initialization than it should be same data type, else it will through compile time error

for (int value = 0, String str = “hi”; value < = 5; value++)  // compile time error

Case 3:

for (; ; ;)

System.out.println(“Hello”);

It is an infinite loop.

Syntax for enhanced for loop:-

for( element : expression )

Enhanced For Loop Java Example:-

int [] value = {1,2,3,4,5,6,7,8,9,10};
int sum = 0;
for (int I : value)
sum += I;

The above example will take all the values from an array one by one and then at the end sum them.

5/5 - (1 vote)

royal52
royal52

I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

We will be happy to hear your thoughts

Leave a reply