Advertisement



< Prev
Next >



C# Break Statement





In C#, when the break statement is encountered within a loop of a program, it stops executing the remaining statements in the loop and takes the control out of it to execute the first statement right after the loop.




Note :


The break statement must be inside a loop, otherwise a compile error is issued by compiler at the time of compilation of the program.





Advertisement




Break statement example


//C# Break statement example 

using System;

class BreakEx       
{
public static void Main()
{
	// A for loop to print the digits from 0 to 9
	for(int i=0;i<10;i++)
	{	
		//checking if the value in i is equal to 5 
		// If yes, the break statement is called.
		if(i==5)
			break;	//when i is equal to 5, this break statement takes the control out of loop

		//Printing the value of i
		Console.WriteLine(i);
	}

	Console.WriteLine("Out of the loop");
}
}


Output


0
1
2
3
4
Out of the loop

As you can see in the program and its output, when i is equal to 5, break statement breaks the execution of the loop and it takes the control out of loop to execute the first statement outside the loop.



Please share this article -





< Prev
Next >
< C# Do While Loop
C# Continue Statement >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement