RVBTCC/test/old/loop.c
2024-12-24 09:55:26 +08:00

105 lines
2.0 KiB
C

int printf(char* format, ...);
int scanf(char* format, ...);
/* **dummy** loop test generated by copilot */
void test_continue() {
int i;
// For loop
printf("For loop:\n");
for (i = 0; i < 5; i++) {
if (i == 3) {
continue; // Skip the rest of 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) {
i++;
continue; // Skip the rest of 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) {
i++;
continue; // Skip the rest of the loop when i is 3
}
printf("%d ", i);
i++;
} while (i < 5);
printf("\n");
}
void test_break() {
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");
}
void test_nested() {
int i;
int j;
// For loop
printf("For loop:\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (i >= 2 && j >= 2 && i + j >= 5) {
return; // Exit nested loop via return
}
printf("(%d, %d) ", i, j);
}
printf("\n");
}
}
int main() {
test_continue();
test_break();
test_nested();
}