Skip to main content
Write a program to print integers from 1 to 6Output:
1
2
3
4
5
6

for Loop

  for (initialization; condition; update statement) {
      // do something
  }

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

Example for Loop

int n = 5;
for (int i = 1, j = n; i <= n && j > 0; i++, j--) {
    System.out.println(i + " - " + j);
}

Output:
1 - 5
2 - 4
3 - 3
4 - 2
5 - 1

Exercise for Loop

Write a program using for loop to print odd integers from 1 to n (inclusive).Sample input:
10
Sample output:
1 3 5 7 9

while Loop

  while (condition) {
      // do something
  }

  int n = 1;
  while (n <= 5) {
      System.out.println(n);
      n++;
  }

Exercise while Loop

Write a program using while loop to print integers from n to 1.Sample input: 10Sample output: 10 9 8 7 6 5 4 3 2 1

do-while Loop

  do {
      // do something
  } while (condition);

  int n = 10;
  do {
      System.out.println(n);
      n--;
  } while (n > 0);

Example do-while Loop

double number;
do {
    System.out.print("Enter a positive number: ");
    number = input.nextDouble();
} while (number <= 0);
System.out.println("You entered " + number);

Exercise do-while Loop

Write a program using do-while loop to display the multiplication table of numbers entered by the user.Sample input:
5
Sample output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50\

Nested Loops

for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        // do something
    }
}

Exercise Nested Loops

Make a program to display m × n rectangles formed with star characters (*). m is the length of the rectangle, and n is its width.Sample Input:
7 3
Sample Output:
* * * * * * *
* * * * * * *
* * * * * * *
Write a program to display a numeric triangle illustrated as follows.Sample Input:
5
Sample Output:
1
12
123
1234
12345
Write a program to display a numeric triangle illustrated as follows.Sample Input:
5
Sample Output:
       ~~~~~~~ 1
     ~~~~~ 12
   ~~~ 123
 ~ 1234
12345
Write a program to display a numeric triangle illustrated as follows.Sample Input:
5
Sample Output:
       ~~~~~~~ 1
     ~~~~~ 121
   ~~~ 12321
 ~ 1234321
123454321
Write a program to display a numeric triangle illustrated as follows.Sample Input:
n = 5
Sample Output:
       ~~~~~~~ 1
     ~~~~~ 121
   ~~~ 12321
 ~ 1234321
123454321
 ~ 1234321
   ~~~ 12321
     ~~~~~ 121
       ~~~~~~~ 1

break dan continue

for (int i = 0; i < 10; i++) {
    if (i == 2) {
        continue; #To the next iteration
    } else if (i == 7) {
        break;    #Exit Loop
    }
    System.out.println(i);
}

Using break

Scanner in = new Scanner(System.in);
boolean ok = false;
while (!ok) {
    System.out.print("Enter a number: ");
    if (in.hasNextDouble()) {
        ok = true;
    } else {
        String word = in.next();
        System.out.println(word + " is not a number");
    }
}
double x = in.nextDouble();

Using continue

Scanner in = new Scanner(System.in);
int x = -1;
int sum = 0;
while (x != 0) {
    x = in.nextInt();
    if (x <= 0) {
        continue;
    }
    System.out.println("Adding " + x);
    sum += x;
}
System.out.println(sum);

Exercise break dan continue

By using break or continue, write a program to determine whether the number entered by the user is a prime number or not.Sample Input:
5
Sample Output:
Prime

Tambahan Materi Nested Loops di kelas

Tulis sebuah code program untuk menghitung total dari angka yang diinputkan oleh user.Sample Input:
Masukkan angka: = 12345
Sample Output:
5 4 3 2 1
sum of digits: = 15
Tulis sebuah code program untuk menghitung total dari angka yang diinputkan oleh user.Sample Input:
Masukkan angka: = 12345
Sample Output:
5 4 3 2 1
sum of digits: = 15
5 1
sum of digits: = 6\
Sample Output:
1 1 2 3 5 8 13 12 7 10 8 9 17 17 16 15 13 10 5 6 11 …
Status Exercise ProgressSample Input:
Masukkan angka: = 123
Masukkan angka: = 456
Masukkan angka: = 451
Sample Output:
1 + 2 = 3 (memenuhi syarat)
4 + 5 = 6 (tidak memenuhi syarat)
4 + 1 = 5 (memenuhi syarat)