While Statement

Definition - What does While Statement mean?

The while statement, in C#, is an iteration statement that allows for the execution of an embedded statement conditionally for zero or more times. The embedded statement implies the code block that contains single or multiple statements to be executed within the while statement.

The while statement is one of the control flow statements in C# that enables the execution of a sequence of logic multiple times in a loop until a specific condition is false. Since the controlling expression that represents the condition is evaluated at the beginning of the execution of the while loop, there is a possibility of not entering the loop when the condition returns false in the first iteration.

The while statement is generally used when the number of iterations to be executed is not known and cannot be estimated. The while statement can also be used in a loop where the condition always remains true but there are exit conditions within the loop body statements.

Techopedia explains While Statement

The while statement takes one parameter that specifies the Boolean expression representing the condition for the termination of the loop. In case of more than one condition, the Boolean expression can be built by combining multiple conditions with conditional logical operators (&& and ||) or Boolean logical operators (& and |).

The Boolean expression is evaluated while:
  • Entering the loop in the first iteration
  • The control reaches the end of the embedded statement of the while statement
  • The "continue" statement is executed within the loop

If the result of the evaluation of this expression is true, the control is transferred to the embedded statement. If the Boolean expression returns false, the control is transferred out of the while statement.

For example, the while statement can be used to print the numbers from one to 10 by providing an appropriate Boolean condition, along with the necessary update to the variable associated with the condition.

"Break", "return", "throw" and "goto" are the jump statements that can be used within the while loop to exit from the iteration of the statements embedded within that loop.

To terminate the while statement and avoid the occurrence of an infinite loop, the Boolean expression should be appropriately defined along with the proper initialization and update of the variables used in the expression.
This definition was written in the context of C#
Share this:

Connect with us

Email Newsletter

Join thousands of others with our weekly newsletter

The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.