Java For Loop Examples

Real-Life Examples

To demonstrate a practical example of the for loop, let’s create a program that counts to 100 by tens:

Example

for (int i = 0; i <= 100; i += 10) {
  System.out.println(i);
} 

 

In this example, we create a program that only print even values between 0 and 10: Continue reading Java For Loop Examples

Java While Loop Examples

Real-Life Examples

To demonstrate a practical example of the while loop, we have created a simple “countdown” program:

Example

int countdown = 3;

while (countdown > 0) {
  System.out.println(countdown);
  countdown--;
}

System.out.println("Happy New Year!!");

 

To demonstrate a practical example of the while loop combined with an if else statement, let’s say we play a game of Yatzy: Continue reading Java While Loop Examples