
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How do we use a break statement in while loop in C#?
The break statement terminates the loop and transfers execution to the statement immediately following the loop.
When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.
Let us see an example to learn how to work with break statement in while loop. The following code snippet terminates the loop using break statement.
if (a > 15) { break; }
The following is the complete code.
Example
using System; namespace Demo { class Program { static void Main(string[] args) { /* local variable definition */ int a = 10; /* while loop execution */ while (a < 20) { Console.WriteLine("value of a: {0}", a); a++; if (a > 15) { /* terminate the loop using break statement */ break; } } Console.ReadLine(); } } }
Output
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15
- Related Articles
- How do we use continue statement in a while loop in C#?
- How to use PowerShell Break statement with the While Loop?
- How to use PowerShell break statement in foreach loop?
- How to use ‘do while loop’ in Java?
- How to use PowerShell break statement with the For loop?
- What is do...while loop statement in JavaScript?
- How to use the break statement to come out of a loop in JavaScript?
- How to use C# do while loop?
- Can we use break statement in a Python if clause?
- How do we use throw statement in JavaScript?
- How do we use foreach statement to loop through the elements of an array in C#?
- do…while loop vs. while loop in C/C++
- How to use ‘while loop’ in Java?
- Do-while loop in Arduino
- What is while loop statement in JavaScript?

Advertisements