-
Notifications
You must be signed in to change notification settings - Fork 0
C_Break_Continue.c
SunilOS edited this page Sep 9, 2024
·
1 revision
Here’s your C code with added comments:
#include <stdio.h> // Includes the standard input/output library, needed for printf function
int main() {
// A 'for' loop that iterates from i = 0 to i < 10
for (int i = 0; i < 10; i++) {
// If 'i' equals 5, exit the loop using 'break'
if (i == 5) {
break; // Terminates the loop
}
// If 'i' equals 2, skip the rest of the loop and continue with the next iteration
if (i == 2) {
continue; // Skips the current iteration
}
// Print the value of 'i' for each iteration that is not skipped
printf("i: %d\n", i);
}
return 0; // Returns 0 to indicate successful execution
}-
if (i == 5) { break; }: Thebreakstatement stops the loop wheniis equal to 5, exiting the loop completely. -
if (i == 2) { continue; }: Thecontinuestatement skips the current iteration wheniis equal to 2, and the loop moves on to the next iteration. -
printf("i: %d\n", i);: Prints the value ofifor each iteration, except for the ones skipped bycontinueor terminated bybreak.