Menu
Coddy logo textTech

Break and Continue

Part of the Logic & Flow section of Coddy's C# journey — lesson 21 of 66.

In C#, break and continue are statements that give you greater control over loop execution.

The break statement

The break statement immediately exits a loop:

for (int i = 1; i <= 10; i++)
{
    if (i == 5)
    {
        break; // Exit the loop when i equals 5
    }
    Console.WriteLine(i);
}

After executing the above code, the output will be:

1
2
3
4

The loop stops as soon as i equals 5, and execution continues with the code after the loop.

The continue statement

The continue statement skips the rest of the current iteration and jumps to the next iteration:

for (int i = 1; i <= 5; i++)
{
    if (i == 3)
    {
        continue; // Skip the rest of this iteration
    }
    Console.WriteLine(i);
}

After executing the above code, the output will be:

1
2
4
5

The value 3 is not printed because the continue statement skips the Console.WriteLine(i) for that iteration.

challenge icon

Challenge

Easy

Write a method called ProcessNumbers that takes an array of integers as a parameter. The method should:

  1. Print all numbers in the array
  2. Skip printing any negative numbers (use continue)
  3. Stop processing if it encounters a number greater than 100 (use break)
  4. Return the sum of all numbers that were printed

Cheat sheet

The break statement immediately exits a loop:

for (int i = 1; i <= 10; i++)
{
    if (i == 5)
    {
        break; // Exit the loop when i equals 5
    }
    Console.WriteLine(i);
}

The continue statement skips the rest of the current iteration and jumps to the next iteration:

for (int i = 1; i <= 5; i++)
{
    if (i == 3)
    {
        continue; // Skip the rest of this iteration
    }
    Console.WriteLine(i);
}

Try it yourself

using System;

public class Program
{
    public static int ProcessNumbers(int[] numbers)
    {
        // Write your code here
        return 0;
    }
    
    public static void Main(string[] args)
    {
        // Create a sample array for immediate testing
        int[] numbers = { 5, -3, 10, 15, -7, 105, 20 };
        
        int sum = ProcessNumbers(numbers);
        Console.WriteLine("Sum: " + sum);
        
        // Also try with user input if provided
        try
        {
            string input = Console.ReadLine();
            if (!string.IsNullOrEmpty(input))
            {
                string[] parts = input.Split(',');
                
                int[] inputNumbers = new int[parts.Length];
                for (int i = 0; i < parts.Length; i++)
                {
                    inputNumbers[i] = int.Parse(parts[i].Trim());
                }
                
                int inputSum = ProcessNumbers(inputNumbers);
                Console.WriteLine("Input Sum: " + inputSum);
            }
        }
        catch (Exception)
        {
            // If there's an error with input, just continue with the sample result
        }
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow