The break Keyword

The Break Keyword

The break keyword in C is used to exit a loop or switch statement prematurely.

In a loop, the break statement is used to immediately exit the loop, even if the loop condition has not been met. For example:

				
					for (int i = 0; i < 10; i++) 
{
 if (i == 5) 
{
 break; // exit loop when i == 5 
}
 printf("%d ", i); 
} 

				
			

In the above code, the loop will exit when i becomes 5, so the output will be 0 1 2 3 4.

In a switch statement, the break statement is used to exit the switch statement and continue executing code after the switch statement. It’s important to include a break statement at the end of each case statement to prevent “falling through” to the next case statement.

Note that the break statement is also used in nested loops or switch statements to exit the innermost loop or switch statement

Join To Get Our Newsletter
Spread the love