Looping Statements in Java
Looping Statements in Java
Syntax:-
for(initialization;condition;increment/decrement)
{
//statement or code to be executed
}
Working of - for loop
Example of - for loop
for(int i=1;i<=10;i++)
System.out.println(“Hello”);
Examples of - for loop
Example 1:- To display natural numbers from 1 to 10
for(int i=1;i<=10;i++)
System.out.println(i);
Syntax:-
initialization;
while(condition)
{
//code to be executed
//updation
}
Working of - while loop
Initialization;
while(condition)
{
//code to be executed
//updation
}
do..while loop in java
Syntax:-
initialization;
do
{
//code to be executed
//updation
}
while(cond);
Working of – do...while loop
initialization;
do
{
//code to be executed
//updation
while(cond);
Entry controlled loop Vs Exit controlled loop
Entry controlled loop:- for loop and while loop are called
entry controlled loop as the test condition is checked at the
beginning of the loop.
Loop body is not executed if the condition is false.