C Tutorial/Statement/Break
Breaking out of a for loop
#include <stdio.h>
int main()
{
char ch;
puts("Start typing");
puts("Press ~ then Enter to stop");
for(;;)
{
ch=getchar();
if(ch=="~")
{
break;
}
}
printf("Thanks!\n");
return(0);
}Start typing
Press ~ then Enter to stop
12312
1
231
12
12
3~
Thanks!
The break statement
break statement can break any type of loop.
#include<stdio.h>
main(){
int i = 0;
while (1)
{
i = i + 1;
printf(" the value of i is %d\n",i);
if (i>5) {
break;
}
}
}the value of i is 1
the value of i is 2
the value of i is 3
the value of i is 4
the value of i is 5
the value of i is 6