Home / Programs / C program break statement in for loop
Programming Example

C program break statement in for loop

👁 996 Views
💻 Practical Program
📘 Step by Step Learning

The break statement has two uses. You can use it to terminate a case in the switch statement. You can also use it to force immediate termination of a loop, bypassing the normal loop conditional test. When the break statement is encountered inside a loop, the loop is immediately terminated, and program control resumes at the next statement following the loop. For example,

Program Code

#include <stdio.h>
void main ()
{ 
int a =  0 ; 
while( a < 100 )
{
	printf("value of a: %d\n", a);
	a++;
	if( a == 50 )
	{ 
	 break;
	}
 }
 
}

Output

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
Press any key to continue . . .

Explanation

This will print 10 to 16 after that break will terminate the loop. When the break statement is encountered inside a loop, the loop is immediately terminated, and program control resumes at the next statement following the loop.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.