Break & Continue - C# Programming Exercise

This C# exercise aims to develop a program that displays the even numbers from 10 to 20, both inclusive, except for 16, using three different approaches. The program should implement increments of 2 in each step using the continue statement to skip the number 16, increments of 1 in each step employing continue to skip the number 16, and an infinite loop with continue to skip the number 16 and break to end the loop once the number 20 is reached. This exercise is helpful for learning how to manage loops in C# and control flow statements like continue and break, which allow managing specific situations within a loop, such as skipping certain values and terminating the loop when a condition is met.

 Category

Flow Control

 Exercise

Break & Continue

 Objective

Write a C# program to write the even numbers from 10 to 20, both inclusive, except 16, in 3 different ways:

Incrementing 2 in each step (use "continue" to skip 16)
Incrementing 1 in each step (use "continue" to skip 16)
With an endless loop (using "break" and "continue")

 Write Your C# Exercise

using System; // Importing the System namespace to use Console functionalities

class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        // First method: Incrementing by 2 and using "continue" to skip 16
        Console.WriteLine("Method 1: Incrementing by 2 and skipping 16");
        for (int i = 10; i <= 20; i += 2)  // Loop increments by 2 starting from 10
        {
            if (i == 16) // If the number is 16, skip it
            {
                continue;
            }
            Console.WriteLine(i); // Display the current number
        }

        Console.WriteLine(); // Empty line for separation between methods

        // Second method: Incrementing by 1 and using "continue" to skip 16
        Console.WriteLine("Method 2: Incrementing by 1 and skipping 16");
        for (int i = 10; i <= 20; i++) // Loop increments by 1
        {
            if (i % 2 == 0 && i != 16) // Only print even numbers and skip 16
            {
                Console.WriteLine(i); // Display the current number
            }
        }

        Console.WriteLine(); // Empty line for separation between methods

        // Third method: Using an endless loop with "break" and "continue"
        Console.WriteLine("Method 3: Endless loop with 'break' and 'continue'");
        int number = 10;
        while (true) // Endless loop
        {
            if (number > 20) // Break the loop when the number exceeds 20
            {
                break;
            }
            if (number == 16) // Skip 16
            {
                number++;
                continue;
            }
            if (number % 2 == 0) // Print only even numbers
            {
                Console.WriteLine(number); // Display the current number
            }
            number++; // Increment the number
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Rectangle V2

    This C# exercise aims to develop a program that asks the user for a number, a width, and a height, and then displays a rectangle with the provide...

  •  Repetitive structures

    This C# exercise aims to develop a program that prompts the user for two numbers and displays all the numbers between them (inclusive) three times using for, while...

  •  Digits in a number

    This C# exercise aims to teach how to develop a program that calculates the number of digits in a positive integer. The program uses an efficient technique by perform...

  •  Hollow square

    This C# exercise aims to develop a program that asks the user for a symbol and a width, and then displays a hollow square of that width using the provid...

  •  Product

    This C# exercise aims to develop a program that asks the user for two integer numbers and shows their multiplication, but without using the "*" operator. Inste...

  •  Absolute value

    This C# exercise aims to develop a program that calculates (and displays) the absolute value of a number x. The absolute value of a number is its distan...