Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Do-While Loop | Loops
C# Basics
course content

Course Content

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

bookDo-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:

Example:

cs

main

copy
1234567891011121314
using 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:

How many iterations will the following loop have?

How many iterations will the following loop have?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
some-alt