Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions content/java/concepts/loops/loops.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---
Title: 'Loops'
Description: 'The while loop loops through a block of code as long as a specified condition is true: pseudo while (condition) { // Code block to be executed } In this example, the code in the loop will run again and again, as long as variable i is still less than 10:'
Description: 'Perform repeated execution of statements in Java using loops such as for, while, and do-while.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'For'
- 'Loops'
- 'While'
- 'For'
CatalogContent:
- 'learn-java'
- 'paths/computer-science'
Expand Down Expand Up @@ -43,6 +44,37 @@ The output would be:
8
```

## Do-While Loop

The `do-while` loop is similar to a `while` loop, but the code block **executes at least once**, even if the condition is false. This makes it useful when you want the loop to run **at least one time** before checking the condition.

```pseudo
do {
// Code block to be executed
} while (condition);
```

In this example, the code in the loop will run at least once, and then continue running as long as variable `i` is less than 10:

```java
int i = 5;

do {
System.out.println(i);
i++;
} while (i < 10);
```

The output would be:

```shell
5
6
7
8
9
```

## For Loop

A `for` loop iterates over a range of values. Its declaration is made up of the following three parts, each separated by a semicolon:
Expand Down