The break statement in C# is a control flow statement used to exit from the innermost loop immediately. It is used within loops, such as for, for each, while, and do while.
Syntax
break;
Example 1. Break in For Loop
for (int i = 0; i <= 10; i++)
{
if (i == 5)
{
break;
}
Console.WriteLine(i);
}
In this provided code snippet, a for loop iterates from 0 to 10, printing the value of i at each iteration. However, if i is equal to 5, the loop terminates prematurely using the break statement.
Steps
- Initialization: The loop starts with I set to 0.
- Condition check: The loop continues as long as i is less than or equal to 10.
- Inside the Loop.
- The loop checks if i is equal to 5 using the condition if (i == 5).
- If i is indeed equal to 5, the break statement is executed, causing the loop to exit immediately.
- If i is not equal to 5, the loop continues to execute, printing the value of i.
Explanation
- The loop starts with i = 0 and prints it.
- The loop increments i to 1 and prints it.
- This process continues until I become 5.
- When i equals 5, the if condition becomes true, and the break statement is executed, causing the loop to terminate prematurely.
- Therefore, only the numbers 0 through 4 are printed.
Result
Example 2. Break in Foreach Loop
int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
foreach (int number in numbers)
{
if (number == 5)
{
break; // Exit the loop when number equals 5
}
Console.WriteLine(number); // Print the current number
}
Explanation
- The foreach loop iterates over each element in the numbers array.
- For each iteration, it checks if the current number is equal to 5 using the condition if (number == 5).
- If the number is indeed equal to 5, the break statement is executed, causing the loop to exit immediately.
- If the number is not equal to 5, it prints the value of a number.
- Therefore, only the numbers 0 through 4 are printed, and the loop terminates when 5 is encountered.
Result
Example 3. Break in While Loop
int i = 0;
while (i <= 10)
{
if (i == 5)
{
break;
}
Console.WriteLine(i);
i++;
}
Explanation
- Initialization: Start with i set to 0.
- The loop continues as long as i is less than or equal to 10.
- Inside the Loop.
- Check if i is equal to 5 using the condition if (i == 5).
- If i is indeed equal to 5, the break statement is executed, causing the loop to exit immediately.
- If i is not equal to 5, the loop continues to execute, printing the value of i.
- After printing the value of i, it increments i by 1.
- Therefore, the loop terminates prematurely when i equals 5, and only the numbers 0 through 4 are printed.
Result
Example 4. Break in Do While Loop
int i = 0;
do
{
if (i == 5)
{
break; // Exit the loop when i equals 5
}
Console.WriteLine(i); // Print the current value of i
i++; // Increment i
} while (i <= 10);
Explanation
- Initialization: Start with i set to 0.
- Inside the Loop.
- Check if i is equal to 5 using the condition if (i == 5).
- If i is indeed equal to 5, the break statement is executed, causing the loop to exit immediately.
- If i is not equal to 5, the loop continues to execute, printing the value of i.
- After printing the value of i, it increments i by 1.
- Condition Check: The loop continues as long as i is less than or equal to 10.
- Therefore, the loop terminates prematurely when i equals 5, and only the numbers 0 through 4 are printed.
Result
If you have any doubts or need further clarification on the break statement in C#, please leave a comment, and I'll be happy to assist..!