Computer Science, asked by jaryan4543, 11 months ago

Explain various looping statements in java?

Answers

Answered by MrThakur14Dec2002
1
ANSWER.....

☆☆•••☆☆ Java Looping Statement

☛Looping statement are the statements execute one or more statement repeatedly several number of times.
☛In while loop first check the condition if condition is true then control goes inside the loop body otherwise goes outside of the body.

HOPE THIS WILL HELP YOU.......


☆☆ @ Mr. Thakur ☆☆

Answered by homosapiens45
4

A loop statement allows us to execute a statement or group of statements multiple times.


There are generally three types of loop statements in java


1) while loop

2) for loop

3) do-while loop



1)while loop :


In while loop,condition is evaluated first and if it returns true then the statements inside while loop execute.

When condition returns false, the control comes out of loop and jumps to the next statement after while loop.



Syntax for while loop


while(condition)

{

Statements(s) ;

}



Eg:


class WhileLoopExample

{

public static void main(String args[] )

{

int i=10;

while(i>1)

{

System.out.println(i);


i--;

}

}

}



Output :


10 9 8 7 6 5 4 3 2




2)do-while loop :


The statements inside loop execute and then the condition gets evaluated.

If the condition returns true then the control gets transferred to the “do” else it jumps to the next statement after do-while.



Syntax for do-while loop


do

{

Statements(s);

} while (condition);



Eg:


class DoWhileLoopExample

{

public static void main(String args[])

{

int i=10;

do

{

System.out.println(i);

i--;

}while(i>1);

}

}



Output :


10 9 8 7 6 5 4 3 2




3)for loop :


The for loop starts with a for statement followed by a set of parameters inside the parenthesis.


The initialization statement describes the starting point of the loop, where the loop variable is initialized with a starting value. A loop variable is simply a variable that controls the flow of the loop.


The test expression is the condition until when the loop is repeated.


Increment/decrement is usually the number by which the loop variable is incremented/decremented.



Syntax for for loop


for (initialization; text expression condition; increment or decrement)

{

statements(s);

}


Eg:

class ForLoopExample

{

public static void main(String args[])

{

for(int i=10; i>1; i--)

{

System.out.println("The value of i is: "+i);

}

}

}



Output :


The value of i is: 10

The value of i is: 9

The value of i is: 8

The value of i is: 7

The value of i is: 6

The value of i is: 5

The value of i is: 4

The value of i is: 3

The value of i is: 2

Similar questions