What is the output of the following C code?
#include <stdio.h> int main() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == 2) { break; } printf("%d%d ", i, j); } } return 0; }
Remember that break exits the innermost loop immediately.
The inner loop prints values until j == 2, then breaks. So only j == 1 prints for each i. The output is 11 21 31 .
What is the output of this C program?
#include <stdio.h> int main() { int i = 0; while (i < 5) { i++; if (i == 3) { continue; } printf("%d ", i); } return 0; }
Think about what continue does inside a loop.
When i == 3, continue skips the printf for that iteration. So 3 is not printed. The output is 1 2 4 5 .
What is the output of this C code?
#include <stdio.h> int main() { for (int i = 0; i < 5; i++) { printf("%d ", i); i++; } return 0; }
Notice the extra i++ inside the loop body.
The loop increments i twice each iteration: once in the for statement and once inside the loop. So i takes values 0, 2, 4 and prints them.
What is the output of this C program?
#include <stdio.h> int main() { int i = 0; do { if (i == 2) { break; } printf("%d ", i); i++; } while (i < 5); return 0; }
Remember that break exits the loop immediately.
The loop prints i values 0 and 1. When i == 2, it breaks before printing. So output is 0 1 .
Consider this C code snippet:
int count = 0;
for (int i = 1; i <= 4; i += 2) {
for (int j = 0; j < i; j++) {
count++;
}
}
What is the final value of count after the loops finish?
Calculate how many times the inner loop runs for each i value.
The outer loop runs for i = 1 (inner loop: 1 iteration) and i = 3 (inner loop: 3 iterations). Total: 1 + 3 = 4. Final value of count is 4.