Do-While Loop

Sun 22 July 2018, in category Loop

do, loop, while

A do-while loop is a similar construct to the while loop as it allows you to iterate over a set of statements while the given expression is true. Note however, that a do-while loop will check the condition after executing the code inside the loop. In our examples below, the do-while loop increments the i value as long as its less than 10.

Java

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

C#

do
{
    Console.WriteLine(i);
    i++;
} while(i < 10);

Python

# Note:  Python doesn't support do while, but can be emulated as shown below.
while True:
    print(i)
    i=i+1
    if i >= 10:
        break

JavaScript

do {
    console.log(i);
    i++;
} while ( i < 10);

Go

for {
    fmt.Println(i)
    i++
    if i >= 10 {
        break
    }
}