Types of loop in java

Asked By 10 points N/A Posted on -
qa-featured

How many types of loop are there in Java and how they differ in their function?

Can you give an example of each loop?

SHARE
Best Answer by sd123123
Best Answer
Best Answer
Answered By 10 points N/A #103634

Types of loop in java

qa-featured

Thanks Sheldon Ron for your answer. But I think

Java has very flexible three looping mechanisms. You can use one of the following three loops:

  • While Loop
  • Do…while Loop
  • For Loop
  • The while Loop:
A while loop is a control structure that allows you to repeat a task a certain number of times.
 
Syntax:
 
The syntax of a while loop is:
 
while(Boolean_expression)
{
   //Statements
}
The do…while Loop:
A do…while loop is similar to a while loop, except that a do…while loop is guaranteed to execute at least one time.
 
Syntax:
 
The syntax of a do…while loop is:
 
do { //Statements }while(Boolean_expression);
 
The for Loop:
 
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
 
A for loop is useful when you know how many times a task is to be repeated.
 
Syntax:
 
The syntax of a for loop is:
 
for(initialization; Boolean_expression; update)
{
   //Statements
}
Answered By 0 points N/A #103633

Types of loop in java

qa-featured

Java has four types of loops,

For loop
While Loop
Do While LoopJava has four types of loops,
For loop
While Loop
Do While Loop
For each loop
Syntax:
for ( initializations or statements  separated by comma ; condition evaluates to true or false ; statements separated by comma){
statement or a group of statements;
}
Syntax: 
initializations or statements ;
While (condition evaluates to true or false ){
statement or a group of statements;
}
Syntax: 
initializations or statements ;
Do{
statement or a group of statements;
}While (condition evaluates to true or false )
 
An example of for each loop
int summation(int[] c) {
    int res = 0;
    for (int j : c)
        res += j;
    return res;
}
It prints all the elements of the array ‘c’.
Loops are such control structures that are available in almost all the high-level programming languages.
 
Without this control structure, you may not be able to do almost all tasks. for, while & do While are common to many programming languages. try to develop the logic of loops clearly.
 
Thanks.

Related Questions