Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Controlling Loop Execution | While and Do-While Loops: Control and Flexibility
Quizzes & Challenges
Quizzes
Challenges
/
C# Loops Practice

bookControlling Loop Execution

When working with loops, you sometimes need to control how and when the loop continues or ends. Two important statements for this purpose are break and continue. The break statement is used to immediately exit a loop, regardless of whether the loop's original condition has been met. This is useful when you want to stop the loop as soon as a certain condition occurs. The continue statement, on the other hand, skips the remaining code in the current iteration and jumps straight to the next iteration of the loop. This helps you avoid executing specific code for certain values or situations within the loop.

Program.cs

Program.cs

copy
123456789101112131415161718192021
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int counter = 1; while (counter <= 10) { if (counter == 5) { System.Console.WriteLine("Reached 5, breaking the loop."); break; } System.Console.WriteLine("Counter: " + counter); counter++; } System.Console.WriteLine("Loop ended."); } } }

Using break as shown above allows you to exit the loop as soon as a specific condition is met, such as finding a certain value or reaching a threshold. In contrast, the continue statement is useful when you want to skip over certain iterations but keep the loop running for the remaining values. For example, you might want to process only odd numbers or skip invalid inputs within a loop.

Program.cs

Program.cs

copy
123456789101112131415161718
namespace ConsoleApp { public class Program { public static void Main(string[] args) { for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; } System.Console.WriteLine("Odd number: " + i); } } } }

In real-world programming, you might use break to stop searching through a list once you have found what you need, such as finding a user's record in a database or detecting a specific error condition. The continue statement is helpful for filtering out unwanted data, such as skipping blank lines when processing a file or ignoring invalid user input during data validation. Both statements give you more precise control over how your loops behave, making your code more efficient and easier to understand.

1. What does the break statement do in a loop?

2. How does continue affect loop execution?

question mark

What does the break statement do in a loop?

Select the correct answer

question mark

How does continue affect loop execution?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5.56

bookControlling Loop Execution

Swipe to show menu

When working with loops, you sometimes need to control how and when the loop continues or ends. Two important statements for this purpose are break and continue. The break statement is used to immediately exit a loop, regardless of whether the loop's original condition has been met. This is useful when you want to stop the loop as soon as a certain condition occurs. The continue statement, on the other hand, skips the remaining code in the current iteration and jumps straight to the next iteration of the loop. This helps you avoid executing specific code for certain values or situations within the loop.

Program.cs

Program.cs

copy
123456789101112131415161718192021
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int counter = 1; while (counter <= 10) { if (counter == 5) { System.Console.WriteLine("Reached 5, breaking the loop."); break; } System.Console.WriteLine("Counter: " + counter); counter++; } System.Console.WriteLine("Loop ended."); } } }

Using break as shown above allows you to exit the loop as soon as a specific condition is met, such as finding a certain value or reaching a threshold. In contrast, the continue statement is useful when you want to skip over certain iterations but keep the loop running for the remaining values. For example, you might want to process only odd numbers or skip invalid inputs within a loop.

Program.cs

Program.cs

copy
123456789101112131415161718
namespace ConsoleApp { public class Program { public static void Main(string[] args) { for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; } System.Console.WriteLine("Odd number: " + i); } } } }

In real-world programming, you might use break to stop searching through a list once you have found what you need, such as finding a user's record in a database or detecting a specific error condition. The continue statement is helpful for filtering out unwanted data, such as skipping blank lines when processing a file or ignoring invalid user input during data validation. Both statements give you more precise control over how your loops behave, making your code more efficient and easier to understand.

1. What does the break statement do in a loop?

2. How does continue affect loop execution?

question mark

What does the break statement do in a loop?

Select the correct answer

question mark

How does continue affect loop execution?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 5
some-alt