What will be the output of the following code snippet?1234567891011121314#include <stdio.h> int main() { for (int i = 0; i < 5; i++) { if (i == 2) { printf("A "); goto continue_loop; } printf("%d ", i); continue_loop: continue; } return 0; }
Question
What will be the output of the following code snippet?1234567891011121314#include <stdio.h> int main() { for (int i = 0; i < 5; i++) { if (i == 2) { printf("A "); goto continue_loop; } printf("%d ", i); continue_loop: continue; } return 0; }
Solution
The output of the code will be:
0 1 A 3 4
Here's the step by step explanation:
- The for loop starts with i = 0.
- It checks if i == 2. Since it's not, it skips the if statement and prints "0 ".
- The loop continues with i = 1. Again, it checks if i == 2. It's not, so it prints "1 ".
- The loop continues with i = 2. This time, i == 2 is true, so it prints "A " and then jumps to the continue statement due to the goto continue_loop; line. This skips the rest of the loop for this iteration.
- The loop continues with i = 3 and i = 4, printing "3 " and "4 " respectively, as i is not equal to 2 in these cases.
- After i = 4, the loop condition i < 5 is no longer true, so the loop ends.
Similar Questions
What will be the output of the following code snippet?
What is the output for the following code?
What will be the output of the following code snippet?123456789101112131415#include <stdio.h> int main() { char direction = 'N'; if (direction == 'N') printf("North"); else if (direction == 'S') printf("South"); else if (direction == 'E') printf("East"); else if (direction == 'W') printf("West"); else printf("Unknown"); return 0; }
What will be the output of the following program?1234567891011121314151617181920#include <stdio.h>void one();void two();void one() { for(int i = 0; i < 2; i++){ two(); }}void two() { printf("TWO ");}int main(){ one(); two(); return 0;}
What will be the output of the following code snippet?1234567891011121314151617#include <stdio.h> int main() { int x = 20; switch (x) { case 10: if (x == 10) printf("Ten "); case 20: if (x == 20) printf("Twenty "); case 30: printf("Thirty "); default: printf("Other "); } return 0; }
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.