What will the following code output?#include <stdio.h>int main() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) break; printf("%d%d ", i, j); } } return 0;}
Question
What will the following code output?#include <stdio.h>int main() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) break; printf("%d%d ", i, j); } } return 0;}
Solution
The code is written in C language. It consists of two nested for loops, both running from 0 to 2 (inclusive). Inside the inner loop, there is a condition that checks if the outer loop counter (i) is equal to the inner loop counter (j). If they are equal, the inner loop breaks and continues with the next iteration of the outer loop. If they are not equal, it prints the values of i and j.
Let's go through the code step by step:
-
In the first iteration, i = 0 and j = 0. Since i == j, the inner loop breaks and no value is printed.
-
In the second iteration, i = 1. For j = 0, since i != j, it prints "10 ". Then j becomes 1, i == j, so the inner loop breaks.
-
In the third iteration, i = 2. For j = 0 and j = 1, since i != j, it prints "20 21 ". Then j becomes 2, i == j, so the inner loop breaks.
So, the output of the code will be: "10 20 21 ".
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.