break
The break statement in loop is used to terminate from the loop and no further code after for loop will be executed.
break statement in for loop:
int main() { for(int i=1; i<=5; i++){ if(i==3){ break; } printf("%d\n", i); } return 0; }
break statement in while loop
int main() { while(1){ int number; printf("Enter the number: "); scanf("%d", &number); if(number < 0){ break; } printf("%d\n", number); } return 0; }
continue
The continue statement skips the current iteration of the loop and starts the loop with next iteration.
Example:
int main() { for(int i=1; i<=5; i++){ if(i==3){ continue; } printf("%d\n", i); } return 0; }
Below is the code using both break and continue statement:
#include <stdio.h> int main() { while(1){ int number; printf("Enter a number: "); scanf("%d", &number); if(number <= 0){ break; } if((number % 2) != 0){ continue; } printf("%d\n", number); } return 0; }
Top comments (0)