Break/Continue

Break/Continue

Break and continue are keywords used to control the flow of execution in loops and switch statements.

  1. break statement: The break statement is used to terminate the current loop or switch statement and transfer control to the statement immediately following the loop or switch.

Example usage of break statement in a for loop:

				
					for (int i = 1; i <= 10; i++)
{
    if (i == 5)
    {
        break; // terminate the loop if i is equal to 5
    }
    Console.WriteLine(i);
}

				
			
  1. continue statement: The continue statement is used to skip the remaining statements in the current iteration of the loop and jump to the next iteration.

Example usage of continue statement in a while loop:

				
					int i = 0;
while (i < 5)
{
    i++;
    if (i == 3)
    {
	   continue; // skip the current iteration when i is equal to 3 }
    Console.WriteLine(i);
}

				
			

Note that break and continue statements can be used with for, while, do-while loops, as well as with switch statements.

Join To Get Our Newsletter
Spread the love