do-while Loop
The do-while
loop is similar to the while
loop however it executes the specified code block first and then checks the condition hence it always executes the code block at least once even if the condition is false
.
Following is the syntax of the do-while
loop:
javascript9123do {// code to be executed} while (condition);
Let's consider the practical example:
main
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
namespace ConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
do {
Console.WriteLine("Hello World");
} while(1 < 0);
}
}
}
1234567891011121314using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { do { Console.WriteLine("Hello World"); } while(1 < 0); } } }
The above code outputs "Hello World"
even though the condition 1 < 0
is false. This will become more clear by looking at the flow diagram of the do-while loop:
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 4. Hoofdstuk 4
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.