C Programming Break and Continue Statement
C Programming Break and Continue Statement
There are two statements built in C programming, break; and continue; to alter the normal flow of a
program. Loops perform a set of repetitive task until text expression becomes false but it is sometimes desirabl e
to skip some statement/s inside loop or terminate the loop immediately without checking the test expression. In
such cases, break and continue statements are used. The break; statement is also used in switch statement to
exit switch statement.
break Statement
In C programming, break is used in terminating the loop immediately after it is encountered. The break
statement is used with conditional if statement.
The break statement can be used in terminating all three loops for, while and do...while loops.
The figure below explains the working of break statement in all three type of loops.
Output
Maximum no. of inputs
4
Enter n1: 1.5
Enter n2: 12.5
Enter n3: 7.2
Enter n4: -1
Average=7.07
In this program, when the user inputs number less than zero, the loop is terminated using break statement with
executing the statement below it i.e., without executing sum=sum+num .
In C, break statements are also used in switch...case statement.
continue Statement
It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used.
For better understanding of how continue statements works in C programming. Analyze the figure below which
bypasses some code/s inside loops using continue statement.
Output
Enter num1:3
Enter num2:0
Enter num3:-5
Enter num4:2
product=-30