# The Java do..while Loop Explained with Examples

**URL:** <https://forum.freecodecamp.org/t/the-java-do-while-loop-explained-with-examples/16732>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [July 3, 2016, 1:04am UTC](https://forum.freecodecamp.org/t/the-java-do-while-loop-explained-with-examples/16732 "2016-07-03T01:04:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [July 3, 2016, 1:04am UTC](https://forum.freecodecamp.org/t/the-java-do-while-loop-explained-with-examples/16732/1 "2016-07-03T01:04:41Z")

</div>

# Do…While Loop

The `do while` is similar to the `while` loop, but the group of statements is guranteed to run at least once before checking for a given condition. An important thing to note is ‘while’ loop is an exit control loop. while(it will not necessarily be executed), ‘do while’ is an entry controlled loop(it will be executed at least once , even if the conditon is not true).

```auto
do
{
    // Statements
}
while (condition);

```

## Example

```auto
int iter_DoWhile = 20;
do
{
    System.out.print (iter_DoWhile + " ");

    // Increment the counter
    iter_DoWhile++;
}
while (iter_DoWhile < 10);
System.out.println("iter_DoWhile Value: " + iter_DoWhile);

```

Output:

```auto
    20
    iter_DoWhile Value: 21

```

**Remember** : Unlike the `while` loop, the condition of a `do-while` loop is checked after the code body is executed once.

![:rocket:](https://forum.freecodecamp.com/images/emoji/twitter/rocket.png?v=2 ":rocket:") [Run Code](https://repl.it/CJYl/0)

## Exercise

Can you guess the output of the following code snippet?

```auto
int i = 10;
do
{
    System.out.println("The value of i is " + i);
    i--;
}
while (i >= 10);

```

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [August 7, 2017, 1:46am UTC](https://forum.freecodecamp.org/t/the-java-do-while-loop-explained-with-examples/16732/2 "2017-08-07T01:46:32Z")

</div>

Closed on August 7, 2017, 1:46am UTC


