Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
Solidity - Break and Continue Statements
Next article icon

C# – Break statement

Last Updated : 02 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, the break statement is used to terminate a loop(for, if, while, etc.) or a switch statement on a certain condition. And after terminating the controls will pass to the statements that present after the break statement, if available. If the break statement exists in the nested loop, then it will terminate only those loops which contain the break statements. 

Syntax:

break;

Flow Chart:

Now we will see the usage of break statement:

  1. Simple Loop
  2. Nested Loop
  3. Infinite Loop 
  4. Switch-Case statement

1. Simple Loop:

Here we will discuss the use of break statements in simple for loop. As shown in the below example the for loop is programmed to execute from 0 to 20 but the output of this example is “0 1 2 3 4 5 6”. Because here we break the loop when the value of x is equal to 7.  If we do not use a break statement, then this loop prints 0….20 numbers. 

C#




// C# program to illustrate the use of 
// break statement in loop
using System;
  
class GFG{
      
static public void Main ()
{
      
    // Here, the break statement
    // terminates the loop when x = 7
    for(int x = 0; x <= 20; x++)
    {
        if (x == 7)
        {
            break;
        }
        Console.WriteLine(x);
    }
}
}
 
 

Output:

0  1  2  3  4  5  6

2. Nested Loop:

We can also use the break statements in the nested loops.  If you use a break statement in the innermost loop, then control will come out only from the innermost loop. Let us discuss the use of break statement in the nested loops with the help of an example:

C#




// C# program to illustrate the use of 
// break statement in nested loop
using System;
  
class GFG{
      
static public void Main ()
{
      
    // Outer Loop
    for(int x = 0; x < 4; x++)
    {  
        // Inner Loop
        for(int y = 1; y < 4; y++)
        {
            if (y > 2)
            {
                break;
            }
            Console.Write("#");
        }
        Console.Write("\n");
    }
}
}
 
 

Output:

##  ##  ##  ##

In the above example, the inner loop is programmed to execute for 4 iterations, but when the value of y is greater than 2 the inner loop stops executing because we use a break statement to restrict the number of iteration of the inner loop to 2. However, the outer loop remains unaffected.

3. Infinite Loops:

We can also use the break statement in the infinite loop to terminate the execution of the infinite loop. Let us discuss the use of break statement in the infinite loops with the help of an example:

C#




// C# program to illustrate 
// infinite loop
using System;
  
class GFG{
      
static public void Main ()
{
    int x = 1;
      
    // Creating infinite loop
    // using while loop
    while (true)
    {
        // This statement will be printed 
        // infinite times 
        Console.WriteLine("Hey GeeksforGeeks");
          x++;
    }
}
}
 
 

 
In the above example, the loop condition based on which the loop terminates is always true. So, the loop will execute an infinite number of times, or we can say never terminate. So, here we use the break statement to terminate the loop when the value of x is equal to 7

C#




// C# program to illustrate the use of
// break statement in the infinite loop
using System;
  
class GFG{
      
static public void Main ()
{
    int x = 1;
      
    while (true)
    {
        if (x == 7)
            break;
              
        Console.WriteLine("Hey GeeksforGeeks");
        x++;
    }
}
}
 
 

Output:

Hey GeeksforGeeks  Hey GeeksforGeeks  Hey GeeksforGeeks  Hey GeeksforGeeks  Hey GeeksforGeeks  Hey GeeksforGeeks

4. Switch-case Statement: 

As we know that the switch statement has a drawback, i.e. when the matching value is found it will execute all the statements until the end of the switch block. To avoid such type of problem we use break statement in every case. So that the break statement terminates the execution of the switch statement for nonmatching statements. As shown in the below example:

C#




// C# program to illustrate the use of 
// break statement in switch-case statement
using System;
  
class GFG{
      
static public void Main ()
{
      
    // Enter the value
    Console.Write("Select option(1, 2, 3): ");
    string str = Console.ReadLine();
    int x = Int32.Parse(str);
      
    // Using break statement in the switch-case
    // statements
     switch(x)
    {
        case 1:
            Console.WriteLine("You select group A");
            break;
        case 2:
            Console.WriteLine("You select group B");
            break;
        case 3:
            Console.WriteLine("You select group C");
            break;
        default:
            Console.WriteLine("Sorry, Not a valid selection!");
            break;
    }
}
}
 
 

Output:

Select option(1, 2, 3): 2  You select group B


Next Article
Solidity - Break and Continue Statements
author
ankita_saini
Improve
Article Tags :
  • C#

Similar Reads

  • C# - continue Statement
    In C#, the continue statement is used to skip over the execution part of the loop(do, while, for, or foreach) on a certain condition, after that, it transfers the control to the beginning of the loop. Basically, it skips its given statements and continues with the next iteration of the loop. Or in o
    2 min read
  • C# Jump Statements (Break, Continue, Goto, Return and Throw)
    In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#. Types of Jump StatementsThere are mainly five keywords in t
    4 min read
  • C# Loops
    Looping in a programming language is a way to execute a statement or a set of statements multiple times depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops. Loops are mainly divided into two categories
    5 min read
  • Ruby Break and Next Statement
    In Ruby, we use a break statement to break the execution of the loop in the program. It is mostly used in while loop, where value is printed till the condition, is true, then break statement terminates the loop. Syntax : Break Example : # Ruby program to use break statement #!/usr/bin/ruby -w i = 1
    2 min read
  • Solidity - Break and Continue Statements
    In any programming language, Control statements are used to change the execution of the program. Solidity allows us to handle loops and switch statements. These statements are usually used when we have to terminate the loop without reaching the bottom or we have to skip some part of the block of cod
    2 min read
  • Difference between break and continue statement in C
    In this article, we will discuss the difference between the break and continue statements in C. They are the same type of statements which is used to alter the flow of a program still they have some difference between them. break statement: This statement terminates the smallest enclosing loop (i.e.
    3 min read
  • Difference between exit() and break in C/C++
    In this article, the topic is to understand the difference between exit() and break. exit(): When a user wants to exit a program from this function is used.It is a void return type function that calls all functions registered at the exit and terminates the program.File buffers are flushed, streams a
    4 min read
  • Getting started with C
    C language is a popular programming language that was developed in 1970 by Dennis Ritchie at Bell Labs. The C programming language was developed primarily to build the UNIX operating system. It is widely used because it is simple, powerful, efficient, and portable. Features of C Programming Language
    5 min read
  • Exit codes in C/C++ with Examples
    The purpose of the exit() function is to terminate the execution of a program. The "return 0"(or EXIT_SUCCESS) implies that the code has executed successfully without any error. Exit codes other than "0"(or EXIT_FAILURE) indicate the presence of an error in the code. Among all the exit codes, the co
    3 min read
  • C Program to Make a Simple Calculator
    A simple calculator is a program that can perform addition, subtraction, multiplication, and division of two numbers provided as input. In this article, we will learn to create a simple calculator program in C. Example Input: a = 10, b = 5, op = +Output: 15.00Explanation: Chosen operation is additio
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences