Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Real-Life Examples

Real-Life Examples

To illustrate a practical example of the for loop, let’s create a program that counts to 100 in increments of ten.

Example

for (int i = 0; i <= 100; i += 10) {

System.out.println(i);

}

In this example, we create a program that prints only even values between 0 and 10.

Example

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

In this example, we create a program that prints the multiplication table for a specified number.

Example

int number = 2;
 
// Print the multiplication table for the number 2
for (int i = 1; i <= 10; i++) {
  System.out.println(number + " x " + i + " = " + (number * i));
}