Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Do-While Loop | Section
Java Fundamentals

bookDo-While Loop

What is the Difference Between while and do-while?

The do-while loop is another type of loop in Java that is similar to the while loop. However, it has one important difference: the condition is checked at the end of each iteration. This means the code block will always execute at least once before evaluating the condition.

Here's the basic syntax of the do-while loop:

Main.java

Main.java

copy
123
do { // Code block } while (condition);

Here are some key points about the do-while loop:

  • Execution flow: the code block runs first, then the condition is checked; if true, the loop continues, otherwise it ends;
  • Guaranteed execution: the loop always runs at least once because the condition is checked after execution;
  • Variable scope: variables declared inside the loop are accessible only within it;
  • Use cases: useful when you need to execute code at least once, such as prompting user input or iterating through a list.

Let's look at a simple example of usage and compare the results of a while loop and a do-while loop on a very basic example:

main.java

main.java

copy
123456789
package com.example; public class Main { public static void main(String[] args) { do { System.out.println("Do-while loop executed successfully"); } while (1 < 0); } }

while loop:

main.java

main.java

copy
123456789
package com.example; public class Main { public static void main(String[] args) { while (1 < 0) { System.out.println("While loop executed successfully"); } } }

We can see that when executing the do-while loop with a condition that is always false, we executed the body of the loop once, while the while loop simply gave us an error.

1. What will be the output of the code?

2. What will be the output of the code?

question mark

What will be the output of the code?

Select the correct answer

question mark

What will be the output of the code?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 17

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookDo-While Loop

Swipe um das Menü anzuzeigen

What is the Difference Between while and do-while?

The do-while loop is another type of loop in Java that is similar to the while loop. However, it has one important difference: the condition is checked at the end of each iteration. This means the code block will always execute at least once before evaluating the condition.

Here's the basic syntax of the do-while loop:

Main.java

Main.java

copy
123
do { // Code block } while (condition);

Here are some key points about the do-while loop:

  • Execution flow: the code block runs first, then the condition is checked; if true, the loop continues, otherwise it ends;
  • Guaranteed execution: the loop always runs at least once because the condition is checked after execution;
  • Variable scope: variables declared inside the loop are accessible only within it;
  • Use cases: useful when you need to execute code at least once, such as prompting user input or iterating through a list.

Let's look at a simple example of usage and compare the results of a while loop and a do-while loop on a very basic example:

main.java

main.java

copy
123456789
package com.example; public class Main { public static void main(String[] args) { do { System.out.println("Do-while loop executed successfully"); } while (1 < 0); } }

while loop:

main.java

main.java

copy
123456789
package com.example; public class Main { public static void main(String[] args) { while (1 < 0) { System.out.println("While loop executed successfully"); } } }

We can see that when executing the do-while loop with a condition that is always false, we executed the body of the loop once, while the while loop simply gave us an error.

1. What will be the output of the code?

2. What will be the output of the code?

question mark

What will be the output of the code?

Select the correct answer

question mark

What will be the output of the code?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 17
some-alt