Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Different kinds of loops are there in Java

Different kinds of loops are there in Java

1. For Loops
2. While Loops
3. Do While Loops

1. For Loops : There are two types of for loops. Incresing and Decreasing for loop

for( <initialization>; <continue condition>; <increment> ) {
    <statements>
}

#Incrementing
for (int i=0; i<=10; i++) {
System.out.println("This is a for loop" +i);
}

#Decrementing
for (int i=10; i>=0; i--) {
System.out.println("This is a for loop" +i);
}

2. While Loops : Repeatedly execute some code while a certain condition evaluates to True.

while( <continue condition> ) {
    <statements>
}

#Increment
int count = 1;
while ( count < 11 ) {
System.out.println("Count is : "+ count);
count++;
}
#Decrement
int count = 10;
while ( count > 0 ) {
System.out.println("Count is : "+ count);
count--;
}
Main Difference between for loop and while loop is # 
Generally the for loop is for a specific number of iterations and 
the while is as long as a condition is true.

Do While :
The difference between do-while and while is that do-while evaluates its expression 
at the bottom of the loop instead of the top. 

int count = 1;
do {
System.out.println("Count is :" + count);
count++;
} while ( count < 11);

Post a Comment

0 Comments