RVBTCC/test/loop/break.c

40 lines
716 B
C
Raw Permalink Normal View History

2024-12-24 15:18:03 +00:00
int printf(char* format, ...);
int main() {
int i;
// For loop
printf("For loop:\n");
for (i = 0; i < 5; i++) {
if (i == 3) {
break; // Exit the loop when i is 3
}
printf("%d ", i);
}
printf("\n");
// While loop
printf("While loop:\n");
i = 0;
while (i < 5) {
if (i == 3) {
break; // Exit the loop when i is 3
}
printf("%d ", i);
i++;
}
printf("\n");
// Do-while loop
printf("Do-while loop:\n");
i = 0;
do {
if (i == 3) {
break; // Exit the loop when i is 3
}
printf("%d ", i);
i++;
} while (i < 5);
printf("\n");
}