Loop Control Statements

Loop Control Statement

Loop control statements in C programming allow you to alter the normal flow of a loop. There are three main loop control statements in C: break, continue, and goto.

1.    break statement: The break statement is used to exit a loop prematurely. When executed inside a loop, the break statement causes the loop to terminate immediately and the program control is transferred to the next statement after the loop. The syntax for the break statement is as follows:

				
					while (condition) 
 {
   // Statements
   if (test_condition)
 {
      break;
   }
   // Statements
}

				
			

2.    continue statement: The continue statement is used to skip over the remaining statements in the current iteration of a loop and proceed to the next iteration. When executed inside a loop, the continue statement skips over the remaining statements in the loop body and returns the control to the loop’s condition check. The syntax for the continue statement is as follows:

				
					while (condition) {
   // Statements
   if (test_condition) {
      continue;
   }
   // Statements
}

				
			

3.    goto statement: The goto statement is a control transfer statement that is used to jump to a specified label. It allows you to transfer control to any part of the program, including outside of loops. However, the use of goto is generally discouraged because it can make code difficult to understand and debug. The syntax for the goto statement is as follows:

				
					goto label;
// ...
label: statement;

				
			

In summary, loop control statements in C allow you to alter the normal flow of a loop. The break statement is used to exit a loop prematurely, the continue statement is used to skip over the remaining statements in the current iteration of a loop, and the goto statement is a control transfer statement that is used to jump to a specified label.

Join To Get Our Newsletter
Spread the love