Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

do: practical reference

do: practical reference: do executes its body before the first while-condition check.

Version: C# language; verified against C# 14 reference

Syntax

do /* grammar-specific form */

do executes its body before the first while-condition check.

Use it when at least one iteration is required by the contract.

The trailing condition is easy to miss, and the body runs even when it is false.

continue proceeds to the condition test; unchanged state can create an infinite loop.

The complete program actually uses do; its exact expected stdout is 2.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var value = 0; do { value++; } while (value < 2); Console.WriteLine(value);

Result

2

The complete program actually uses `do`; its exact expected stdout is `2`.

Sources