C# Loop Control Statements
Quick Answer
Loop control statements in C#—break, continue, and goto—allow you to alter the flow of loops by exiting early, skipping iterations, or jumping to labeled statements. These statements help write clearer and more efficient loops when used appropriately.
Learning Objectives
- Explain the purpose of Loop Control Statements in a practical learning context.
- Identify the main ideas, terms, and decisions involved in Loop Control Statements.
- Apply Loop Control Statements in a simple real-world scenario or practice task.
Introduction
Loops are fundamental in programming to repeat actions. However, sometimes you need to control how these loops behave beyond their natural flow.
C# provides loop control statements that let you exit loops early, skip certain iterations, or jump to specific parts of your code.
Control your loops, don’t let your loops control you.
Break Statement
The break statement immediately exits the nearest enclosing loop or switch statement.
It is useful when you want to stop looping once a condition is met.
- Terminates the current loop instantly.
- Control moves to the statement following the loop.
- Can be used in for, while, do-while loops, and switch statements.
Example of break in a for loop
In this example, the loop stops when the counter reaches 5.
Continue Statement
The continue statement skips the current iteration of the loop and proceeds with the next iteration.
It is helpful when you want to ignore certain conditions but continue looping.
- Skips the remaining code inside the loop for the current iteration.
- Control moves to the next iteration of the loop.
- Works with for, while, and do-while loops.
Example of continue in a while loop
This example skips printing even numbers by continuing to the next iteration.
Goto Statement
The goto statement transfers control to a labeled statement within the same method.
While powerful, it should be used sparingly as it can make code harder to read and maintain.
- Jumps to a specified label in the code.
- Can be used to exit nested loops or switch statements.
- Overuse can lead to spaghetti code.
Example of goto to exit nested loops
This example uses goto to break out of two nested loops immediately.
Practical Example
This loop prints numbers from 0 to 4 and stops when i equals 5 due to the break statement.
This loop prints only odd numbers by skipping even numbers using continue.
This example uses goto to exit both loops immediately when i and j equal 1.
Examples
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
Console.WriteLine(i);
}This loop prints numbers from 0 to 4 and stops when i equals 5 due to the break statement.
int i = 0;
while (i < 10) {
i++;
if (i % 2 == 0) {
continue;
}
Console.WriteLine(i);
}This loop prints only odd numbers by skipping even numbers using continue.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
goto EndLoops;
}
Console.WriteLine($"i={i}, j={j}");
}
}
EndLoops:
Console.WriteLine("Exited nested loops.");This example uses goto to exit both loops immediately when i and j equal 1.
Best Practices
- Use break to exit loops early when a condition is met to improve efficiency.
- Use continue to skip unnecessary iterations cleanly.
- Avoid overusing goto; prefer structured control flow for readability.
- Comment your use of loop control statements to clarify intent.
- Test loops thoroughly to ensure control statements behave as expected.
Common Mistakes
- Using break or continue without clear conditions, causing unexpected behavior.
- Overusing goto leading to confusing and hard-to-maintain code.
- Placing code after continue that never executes in the current iteration.
- Forgetting that break exits only the nearest loop, not all nested loops.
Hands-on Exercise
Using break in a loop
Write a for loop that prints numbers from 1 to 10 but stops printing when it reaches 7 using break.
Expected output: Numbers 1 through 6 printed on separate lines.
Hint: Use an if condition inside the loop to check the number and break.
Using continue to skip iterations
Create a while loop that prints numbers from 1 to 10 but skips multiples of 3 using continue.
Expected output: Numbers 1, 2, 4, 5, 7, 8, 10 printed on separate lines.
Hint: Check if the number is divisible by 3 and use continue to skip printing.
Using goto to exit nested loops
Write nested for loops from 0 to 3 and use goto to exit both loops when the inner loop variable equals 2.
Expected output: Prints pairs until inner loop variable is 2, then exits.
Hint: Label the statement after loops and use goto inside the inner loop.
Interview Questions
What is the difference between break and continue in C# loops?
InterviewBreak exits the entire loop immediately, while continue skips the current iteration and proceeds with the next one.
When should you use the goto statement in C#?
InterviewGoto should be used sparingly, typically to exit multiple nested loops or switch statements when other control structures are less clear.
What is Loop Control Statements, and why is it useful?
BeginnerLoop control statements in C#—break, continue, and goto—allow you to alter the flow of loops by exiting early, skipping iterations, or jumping to labeled statements.
MCQ Quiz
1. What is the best first step when learning Loop Control Statements?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce Loop Control Statements?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. Loop control statements in C#—break, continue, and goto—allow you to alter the flow of loops by exiting early, skipping iterations, or jumping to labeled statements.
B. Loop Control Statements never needs examples
C. Loop Control Statements is unrelated to practical work
D. Loop Control Statements should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- Loop control statements in C#—break, continue, and goto—allow you to alter the flow of loops by exiting early, skipping iterations, or jumping to labeled statements.
- These statements help write clearer and more efficient loops when used appropriately.
- Loops are fundamental in programming to repeat actions.
- However, sometimes you need to control how these loops behave beyond their natural flow.
- C# provides loop control statements that let you exit loops early, skip certain iterations, or jump to specific parts of your code.
Summary
Loop control statements in C# provide powerful ways to manage the flow of loops.
Break exits loops early, continue skips iterations, and goto jumps to labeled code.
Using these statements wisely can make your code more efficient and readable.
Frequently Asked Questions
Can break be used outside loops?
No, break can only be used inside loops or switch statements to exit them.
Is goto recommended in modern C# programming?
Goto is generally discouraged because it can make code harder to read and maintain, but it can be useful in specific scenarios like exiting nested loops.
What happens if continue is used in a do-while loop?
Continue skips the remaining statements in the current iteration and proceeds with the next iteration, including evaluating the loop condition.
What is Loop Control Statements?
Loop control statements in C#—break, continue, and goto—allow you to alter the flow of loops by exiting early, skipping iterations, or jumping to labeled statements.
Why is Loop Control Statements important?
These statements help write clearer and more efficient loops when used appropriately.
How should I practice Loop Control Statements?
Loops are fundamental in programming to repeat actions.

