Lam Nguyen

Lam Nguyen

Lam Nguyen

August 18, 2023

 • 1 min read

0

Java II Loops

For-each statement in Java, While loop, Do/While Loop, For Loop

hacker-mind

Java While Loop

The while loop loops through a block of code as long as a specified condition is true:

int i = 0;
while (i < 5) {
  System.out.println(i);
  i++;
}

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

int i = 0;
do {
  System.out.println(i);
  i++;
}
while (i < 5);

Java For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

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

Nested Loops

It is also possible to place a loop inside another loop. This is called a nested loop.

The “inner loop” will be executed one time for each iteration of the “outer loop”:

// Outer loop
for (int i = 1; i <= 2; i++) {
  System.out.println("Outer: " + i); // Executes 2 times

  // Inner loop
  for (int j = 1; j <= 3; j++) {
    System.out.println(" Inner: " + j); // Executes 6 times (2 * 3)
  }
}

For-each statement in Java

In Java, the for-each statement allows you to directly loop through each item in an array or ArrayList and perform some action with each item.

When creating a for-each statement, you must include the for keyword and two expressions inside of parentheses, separated by a colon. These include:

  1. The handle for an element we’re currently iterating over.

  2. The source array or ArrayList we’re iterating over.

// array of numbers
int[] numbers = {1, 2, 3, 4, 5};

// for-each loop that prints each number in numbers
// int num is the handle while numbers is the source array
for (int num : numbers) {
	System.out.println(num);
}

There is also a “for-each” loop, which is used exclusively to loop through elements in an array:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
  System.out.println(i);
}

Topics

Java

More stories

Aug, 2023 • 2 min read

Java I Object-Oriented

object-oriented

Aug, 2023 • 2 min read

Java I Conditionals and Control Flow

proud-coder

Social media

avatar

GitHub

0 followers

Follow
avatar

LinkedIn

438 followers

Follow
avatar

Instagram

410 followers

Follow
avatar

Medium

23 followers

Follow
avatar

Twitter

49 followers

Follow