What is the output of this C code?
#include <stdio.h> int main() { for (int i = 0; i < 5; i++) { if (i == 3) { break; } printf("%d ", i); } return 0; }
Remember, break stops the loop immediately when the condition is met.
The loop prints numbers from 0 up to but not including 3. When i == 3, the break stops the loop, so 3 and 4 are not printed.
What will be the output of this C program?
#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; }
The break only exits the inner loop when j == 2.
For each i, the inner loop prints i1 then breaks when j == 2. So only 11, 21, and 31 are printed.
What is the output of this C code?
#include <stdio.h> int main() { int count = 0; while (count < 10) { if (count == 5) { break; } printf("%d ", count); count++; } return 0; }
The break stops the loop before printing 5.
The loop prints numbers starting from 0. When count reaches 5, the break stops the loop, so 5 is not printed.
What will be the output of this C program?
#include <stdio.h> int main() { int x = 2; switch (x) { case 1: printf("One\n"); break; case 2: printf("Two\n"); case 3: printf("Three\n"); break; default: printf("Default\n"); } return 0; }
Without a break after case 2, execution continues to case 3.
The switch matches case 2 and prints "Two". Because there is no break after it, it continues to case 3 and prints "Three" before breaking.
What is the output of this C program?
#include <stdio.h> int main() { int i, j; for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) { if (i * j == 4) { break; } printf("%d%d ", i, j); } } return 0; }
The break stops only the inner loop when i * j == 4.
For i=1, no break occurs (1*1=1, 1*2=2, 1*3=3), prints 11 12 13. For i=2, prints 21 (2*1=2), then at j=2 (2*2=4) break skips printing 22 and 23. For i=3, no break (3*1=3, 6, 9), prints 31 32 33.