Skip to content

Latest commit

 

History

History
42 lines (35 loc) · 2.21 KB

File metadata and controls

42 lines (35 loc) · 2.21 KB
title while (C# Reference)
ms.date 07/20/2015
ms.prod .net
ms.technology
devlang-csharp
ms.topic article
f1_keywords
while_CSharpKeyword
while
helpviewer_keywords
while keyword [C#]
ms.assetid 72a0765c-6852-4aca-b327-4a11cb7f5c59
caps.latest.revision 22
author BillWagner
ms.author wiwagn

while (C# Reference)

The while statement executes a statement or a block of statements until a specified expression evaluates to false.

Example

[!code-csharpcsrefKeywordsIteration#5]

Example

[!code-csharpcsrefKeywordsIteration#6]

Example

Because the test of the while expression takes place before each execution of the loop, a while loop executes zero or more times. This differs from the do loop, which executes one or more times.

A while loop can be terminated when a break, goto, return, or throw statement transfers control outside the loop. To pass control to the next iteration without exiting the loop, use the continue statement. Notice the difference in output in the three previous examples, depending on where int n is incremented. In the example below no output is generated.

[!code-csharpcsrefKeywordsIteration#7]

C# Language Specification

[!INCLUDECSharplangspec]

See Also

C# Reference
C# Programming Guide
C# Keywords
while Statement (C++)
Iteration Statements