Loop

Loops in Java programming language

Loops in Java programming language

In this tutorial, we will discuss Loops in Java programming language

In some cases, if you want to execute a block of the statement multiple times according to a condition, such case loops are used to perform this action.

Looping structure in programming languages is a feature which helps to execute a block of instruction(continuous instruction or function or task) repeatedly while some conditions and evaluate until becomes true.

 

Loops in Java programming language

Java language contains some loops which are functioning similarly. it uses to helps to perform some continuous task until the given condition is fulfilled.

We will discuss in this part all Loops in Java programming language

 

1) While loop

A “while loop” is a Loop statement. That allows the code( flow of control ) to be executed repeatedly to perform based on the given Boolean expression for a certain number of times.

Syntax

initilaization;
while(boolean_Expression)
{
//loop body
looping statements

increment/decrement statements
}

 

Flowchart

The flow of control in while loop

First the “while loop” starts with the initialization of statement only once, then it checks the Boolean-expression. If it is evaluated as true then the loop body of statements are executed. If it is evaluated as false, the flow of control exits from the loop body and moves to the end of the loop.

When the loop is evaluated true. the body of statements are executed and the flow of control goes to updating statements and then, test Boolean-expression is evaluated again to executed the body of the statement.

This process is followed until the Boolean-expression returns false.

Example

//Java program to illustrate while loop
class Demo_While{
public static void main(String args[]){
int i=0;//declare variable "i" and initialize  as zero
while(i<=5){
System.out.println("Value of i: "+i);
//this statement execute until this condition(become greater than 5) performed
i++;
//increment the value of i for next ikteration
}
}

}

When the above code is executed, it produces the following result

value of i=0
value of i=1
value of i=2
value of i=3
value of i=4
value of i=5

Know more about this

while loop in Java language

Nested while loop in Java language

 

2) for loop

The for loop is a looping structure. It allows their code (flow of control) to be executed repeatedly to perform based given boolean expression to a certain number of times., as same as while loop but with some differences.

Although for loop and while loop both have same flow of control, but the for loop structure distinguished with the while loop.

In the for loop, initialization statements, boolean expression, updating statements are defined as the same line

Syntax

for(initialization_statements; boolean_Expression; increment/decrement){
 //loop body looping statements 
}


Flowchart

the flow of control for loop in Java

First, the “for loop” starts with the initialization statement only once. and then checks the boolean-expression. If it is evaluated as true then the loop body of statements are executed or If it evaluated as the false flow of control exits from the loop body and goes to the end of the loop.

When the loop is evaluated true. the body of statements is executed, then the flow of control goes for updating statements and test boolean expression is evaluated again to executed the body of the statement.

This process follows until the boolean expression returns false.

Example

//Java program to illustrate for loop
class Demo_For1{
public static void main(String args[]){
int i;//declare variable "i";
for(i=0; i<=5; i++){
System.out.println("Value of i: "+i);
//this statement execute until this condition(become greater than 5) performed
//increment the value of i for next ikteration
}
}

}

When the above code is executed, it produces the following result

value of i=0
value of i=1
value of i=2
value of i=3
value of i=4
value of i=5

 

Know more about this

for loop in Java language

Nested for loop in Java language

 

3) do-while loop

The do while loop is similar to while loop with the only difference that it starts with the execution of statements before the test condition is checked. If the test expression returns false as the first time, the statements execute only once and loop exit from the execution

do-while is a looping structure. It allows their flow of control access repeatedly to perform based given boolean expression as same as while loop but with some differences. The do-while loop structure distinguishes with the while loop structure.

In the do -while loop, initially, statements are executed only once then flow of control moves for updating-statements. Then it starts to evaluate test condition and it(flow of control )begins to work as a normal while loop.

Syntax

for(initialization_statements; boolean_Expression; increment/decrement){
 //loop body looping statements 
}

Example

class DoWhileLoopEx{
  public static void main (String args[]){
     int i=1;
     do{
        System.out.println(i);
       i++;
      }while(i<=10);
   }

}

When the above code is executed, it produces the following result

1
2
3
4
5
6
7
8
9
10

 

Flowchart

Java programming Loop control do-while loop

Know more about this

do-while loop in Java language

Nested do-while loop in Java language

 

 

4) Enhanced for loop

Java language is introduced to another type of for loop in Java 5. It is called as Enhanced for loop(for each loop) to iterate through the elements of a collection of an array.

you can see in this blog how to use enhanced for loop to iterate the one-dimensional array, two-dimensional array and three-dimensional array elements in Java.

Enhanced for loop is used only to iterates through the elements in a sequential manner without knowing currently processing elements.

 

Syntax

for(elements:Collection obj/array)
{
statements
}

 

Example

class Java_for_each{
public static void main(String args[]){
int marks[]={56,78,98,54,67};  // array declaration and initialization

for(int i:marks){
System.out.println(i);
                       }
        }
    }

When the above code is executed, it produces the following result

56
78
98
54
67

 

Know more about this

Enhanced for loop in Java language

 

Suggested for you

Single dimension array in Java language

Two dimension array in Java language

three dimension array in Java language

 

Nested for loop in Cpp programming language
Switch case statement in Java language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

Recent Posts

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

5 hours ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

3 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago