Correct Answer: switch
Explanation: In C programming, a `switch` statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. When a match is found, the block of code associated with that case is executed.
Correct Answer: To exit the switch block
Explanation: The `break` statement is used within a `switch` block to terminate a case and prevent the execution from falling through to subsequent cases. Without a `break`, the program will continue executing the next case regardless of its condition, which can lead to unintended behavior.
Correct Answer: default
Explanation: The `default` keyword is used in a `switch` statement to specify a block of code to be executed if none of the `case` labels match the value of the switch expression. It acts as a fallback mechanism to handle unexpected or undefined values.
Correct Answer: No statement is executed
Explanation: If the condition in an `if` statement evaluates to `false` and there is no `else` block provided, the program will skip the `if` block entirely and continue executing the subsequent statements. The `if` block will not execute any code if the condition is not met.
Correct Answer: if (condition) { … } else { … }
Explanation: The correct syntax for an `if-else` statement in C includes the `if` keyword followed by the condition in parentheses, a block of code in curly braces for the `if` part, the `else` keyword, and another block of code in curly braces for the `else` part.
Correct Answer: No limit
Explanation: There is no limit to the number of `else if` clauses that can be chained in an `if` statement. This allows for complex decision-making by checking multiple conditions in sequence. The program will evaluate each condition in order and execute the corresponding block for the first `true` condition.
#include <stdio.h>
int x = 10;
if (x > 5) {
printf("A");
} else if (x > 8) {
printf("B");
} else {
printf("C");
}
Correct Answer: A
Explanation: The condition `x > 5` evaluates to `true` since `x` is 10. Therefore, the program executes the code inside the first `if` block and prints “A”. The `else if` and `else` blocks are not evaluated once a `true` condition is found, hence “A” is the only output.
Correct Answer: Execution continues to the next case
Explanation: If there is no `break` statement in a `case`, the program will continue executing the subsequent cases until it encounters a `break` statement or reaches the end of the `switch` block. This behavior is called “fall through” and can be used intentionally, but often it is the source of logical errors if not handled correctly.
Correct Answer: They can have any level of nesting
Explanation: Nested `if` statements are allowed in C and can be nested to any depth. This means an `if` statement can be placed inside another `if` statement, allowing for complex decision-making structures. Proper indentation and code organization are crucial to maintaining readability and avoiding errors.
#include <stdio.h>
int x = 3;
switch(x) {
case 1:
printf("One");
case 2:
printf("Two");
case 3:
printf("Three");
default:
printf("Default");
}
Correct Answer: ThreeDefault
Explanation: The `switch` statement evaluates `x`, which is 3, and matches the `case 3`. Since there is no `break` statement after `case 3`, the execution continues to the `default` case as well. Hence, “ThreeDefault” is printed. This demonstrates the importance of `break` statements to control the flow of execution in `switch` statements.
Correct Answer: To terminate the loop immediately
Explanation: The `break` statement, when encountered inside a loop, immediately terminates the loop and transfers control to the statement following the loop.
Correct Answer: for, while, and do-while
Explanation: The `break` statement can be used in all types of loops in C: `for`, `while`, and `do-while` loops. It can also be used within `switch` statements.
#include <stdio.h>
for (int i = 0; i < 5; i++) {
if (i == 3) {
break;
}
printf("%d ", i);
}
Correct Answer: 0 1 2
Explanation: The loop prints the values of `i` until `i` equals 3. When `i` is 3, the `break` statement is executed, terminating the loop. Hence, only "0 1 2" is printed.
Correct Answer: Only the innermost loop
Explanation: In nested loops, the `break` statement only terminates the innermost loop in which it is executed. The outer loops continue executing as usual.
#include <stdio.h>
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
if (i == 3) {
break;
}
}
Correct Answer: 0 1 2
Explanation: The `while` loop prints the values of `i` and increments `i` each iteration. When `i` becomes 3, the `break` statement terminates the loop, so only "0 1 2" is printed.
Correct Answer: No
Explanation: The `break` statement can only terminate the innermost loop in which it is placed. To exit multiple levels of nested loops, additional logic or flags are required.
Correct Answer: The loop executes only once
Explanation: If a `break` statement is used unconditionally in a loop, the loop will execute its body only once before the `break` statement terminates the loop.
#include <stdio.h>
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == j) {
break;
}
}
}
#include <stdio.h>
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == j) {
break;
}
}
break;
}
#include <stdio.h>
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == j) {
continue;
}
}
}
#include <stdio.h>
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == j) {
i = 5;
break;
}
}
}
Correct Answer: D
Explanation: The code snippet in D uses the `break` statement to exit the inner loop and sets `i` to 5 to ensure the outer loop condition fails, effectively breaking out of both loops.
#include <stdio.h>
int x = 0;
while (x < 10) {
if (x == 5) {
break;
}
x++;
}
printf("%d", x);
Correct Answer: 5
Explanation: The loop increments `x` until `x` equals 5, at which point the `break` statement terminates the loop. The final value of `x` is printed, which is 5.
Correct Answer: Exiting a function
Explanation: The `break` statement is used to exit loops (`for`, `while`, `do-while`) and `switch` cases, but it is not used to exit functions. To exit a function, the `return` statement is used.
Correct Answer: It prevents the fall-through behavior
Explanation: In a `switch` statement, the `break` statement prevents fall-through by terminating the current case and skipping the execution of subsequent cases.
#include <stdio.h>
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i + j == 3) {
break;
}
printf("%d %d\n", i, j);
}
}
Correct Answer: 0 0\n0 1\n1 0\n1 1\n2 0
Explanation: The inner loop prints pairs of `i` and `j` values until `i + j` equals 3, where the `break` statement terminates the inner loop. This process repeats for each value of `i` in the outer loop.
Correct Answer: The `break` statement can be used in both `switch` and loop constructs.
Explanation: The `break` statement is versatile and can be used to exit both `switch` statements and loop constructs (`for`, `while`, `do-while`).
#include <stdio.h>
for (int i = 0; i < 3; i++) {
switch (i) {
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
default:
printf("Default ");
}
}
Correct Answer: Zero One Two
Explanation: The `switch` statement matches each value of `i` to the corresponding case, and the `break` statement prevents fall-through to other cases. The output is "Zero One Two".
Correct Answer: `break` terminates the loop, while `continue` skips to the next iteration.
Explanation: The `break` statement exits the loop entirely, whereas the `continue` statement skips the current iteration and moves control to the next iteration of the loop.
#include <stdio.h>
int i = 0;
do {
if (i == 2) {
break;
}
printf("%d ", i);
i++;
} while (i < 5);
Correct Answer: 0 1
Explanation: The `do-while` loop prints the values of `i` until `i` equals 2. When `i` is 2, the `break` statement terminates the loop. Hence, only "0 1" is printed.
Correct Answer: The switch statement terminates
Explanation: When a `break` statement is encountered in a `case` block, the `switch` statement terminates and control passes to the statement following the `switch` block.
Correct Answer: Use a `goto` statement
Explanation: To break out of multiple nested loops in C, a `goto` statement can be used to jump to a specific label outside all loops. This is generally discouraged due to readability and maintainability concerns.
#include <stdio.h>
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) {
break;
}
printf("%d%d ", i, j);
}
}
Correct Answer: 00 10 20
Explanation: The inner loop prints pairs of `i` and `j` values until `j` equals 1, where the `break` statement terminates the inner loop. This repeats for each `i` in the outer loop, resulting in "00 10 20".
Correct Answer: No
Explanation: The `break` statement is specifically designed to exit loops and `switch` statements. Using it outside of these constructs will result in a compile-time error.
#include <stdio.h>
int x = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 3) {
break;
}
x++;
}
}
printf("%d", x);
Correct Answer: 15
Explanation: The inner loop runs until `j` equals 3, breaking out after 3 iterations. This occurs 5 times (for each `i` from 0 to 4), so `x` is incremented 3 times per outer loop iteration, resulting in 3 * 5 = 15.
Correct Answer: It exits the loop immediately.
Explanation: The `break` statement in a `do-while` loop causes the loop to terminate immediately, transferring control to the statement following the loop.
#include <stdio.h>
int n = 10;
while (n > 0) {
printf("%d ", n);
n--;
if (n == 5) {
break;
}
}
Correct Answer: 10 9 8 7 6
Explanation: The loop decrements `n` each iteration and prints its value. When `n` equals 5, the `break` statement terminates the loop, resulting in "10 9 8 7 6".
#include <stdio.h>
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 2) {
break;
}
printf("%d %d\n", i, j);
}
}
Correct Answer: 0 0\n0 1\n1 0\n1 1\n2 0\n2 1
Explanation: The inner loop prints pairs of `i` and `j` values until `j` equals 2, where the `break` statement terminates the inner loop. This process repeats for each value of `i` in the outer loop.
Correct Answer: After each case block
Explanation: The `break` statement is typically used after each case block in a `switch` statement to prevent fall-through to subsequent cases and to exit the `switch` block.
#include <stdio.h>
int x = 0;
while (x < 3) {
switch (x) {
case 0:
printf("A");
break;
case 1:
printf("B");
break;
case 2:
printf("C");
break;
}
x++;
}
Correct Answer: ABC
Explanation: The `while` loop increments `x` each iteration and the `switch` statement prints "A", "B", and "C" for `x` values 0, 1, and 2 respectively. The `break` statement ensures each case is executed only once.
Correct Answer: The compiler generates an error.
Explanation: The `break` statement must be used within a loop or `switch` statement. If used outside these constructs, the compiler will generate an error indicating improper use of the `break` statement.
Correct Answer: By using the `return` statement in a function
Explanation: In a function, a `return` statement can be used within a `switch` case to exit the `switch` statement by returning control to the caller. This also terminates the function execution.
#include <stdio.h>
for (int i = 0; i < 4; i++) {
switch (i) {
case 1:
printf("One ");
break;
case 3:
printf("Three ");
break;
}
}
Correct Answer: One Three
Explanation: The `switch` statement prints "One" when `i` is 1 and "Three" when `i` is 3. The `break` statement prevents fall-through. Other values of `i` result in no action.
#include <stdio.h>
int i = 0;
do {
if (i == 3) {
break;
}
i++;
} while (i < 5);
#include <stdio.h>
int i = 0;
do {
if (i == 3) {
continue;
}
i++;
} while (i < 5);
#include <stdio.h>
int i = 0;
do {
i++;
if (i == 3) {
break;
}
} while (i < 5);
#include <stdio.h>
int i = 0;
do {
if (i == 3) {
break;
}
i++;
} while (i <= 5);
Correct Answer: C
Explanation: Option C demonstrates the correct use of the `break` statement in a `do-while` loop. The loop increments `i` before checking the condition `i == 3` and breaking out of the loop.
Correct Answer: To skip the current iteration and proceed to the next iteration
Explanation: The `continue` statement, when encountered inside a loop, skips the remaining code in the current iteration and proceeds to the next iteration of the loop.
Correct Answer: for, while, and do-while
Explanation: The `continue` statement can be used in all types of loops in C: `for`, `while`, and `do-while`. It causes the loop to skip the rest of the code in the current iteration and move to the next iteration.
#include <stdio.h>
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
printf("%d ", i);
}
Correct Answer: 0 1 2 4
Explanation: The loop prints the values of `i` except when `i` equals 3. The `continue` statement causes the loop to skip the rest of the code in that iteration, so 3 is not printed.
Correct Answer: The loop skips the current iteration and checks the condition again
Explanation: When a `continue` statement is encountered in a `while` loop, the loop skips the remaining code in the current iteration and checks the loop condition again to decide whether to proceed with the next iteration.
Correct Answer: Only the innermost loop
Explanation: In nested loops, the `continue` statement affects only the innermost loop in which it is placed. It skips the current iteration of that loop and proceeds with the next iteration.
#include <stdio.h>
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
printf("%d ", i);
}
Correct Answer: 1 2 4 5
Explanation: The `while` loop increments `i` before the `continue` statement. When `i` equals 3, the `continue` statement skips the `printf` function, so 3 is not printed.
#include <stdio.h>
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) {
continue;
}
printf("%d %d\n", i, j);
}
}
Correct Answer: 0 0\n0 2\n1 0\n1 2\n2 0\n2 2
Explanation: The inner loop prints pairs of `i` and `j` values, but skips the iteration when `j` equals 1 due to the `continue` statement. As a result, "0 1", "1 1", and "2 1" are not printed.
Correct Answer: No
Explanation: The `continue` statement only skips the current iteration of the innermost loop in which it is placed. It cannot be used to skip multiple levels of nested loops.
#include <stdio.h>
int i = 0;
do {
i++;
if (i % 2 == 0) {
continue;
}
printf("%d ", i);
} while (i < 5);
Correct Answer: 1 3 5
Explanation: The `do-while` loop increments `i` each iteration. The `continue` statement causes the loop to skip the `printf` function when `i` is even, so only odd numbers are printed.
Correct Answer: `break` terminates the loop, while `continue` skips to the next iteration
Explanation: The `break` statement exits the loop entirely, whereas the `continue` statement skips the remaining code in the current iteration and moves control to the next iteration of the loop.
#include <stdio.h>
int n = 10;
while (n > 0) {
n--;
if (n == 5) {
continue;
}
printf("%d ", n);
}
Correct Answer: 9 8 7 6 4 3 2 1 0
Explanation: The loop decrements `n` each iteration and prints its value, except when `n` equals 5 due to the `continue` statement. Hence, 5 is not printed.
Correct Answer: Exiting a loop prematurely
Explanation: The `continue` statement is used to skip the remaining code in the current iteration and proceed to the next iteration of the loop. It is not used for exiting a loop prematurely; the `break` statement is used for that purpose.
#include <stdio.h>
for (int i = 0; i < 4; i++) {
if (i % 2 == 0) {
continue;
}
printf("%d ", i);
}
Correct Answer: 1 3
Explanation: The loop prints the values of `i` that are odd. The `continue` statement skips the iterations where `i` is even, so only 1 and 3 are printed.
Correct Answer: No
Explanation: The `continue` statement is used within loops and cannot be used in `switch` statements. In `switch` statements, the `break` statement is used to exit a case block.
#include <stdio.h>
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == j) {
continue;
}
printf("%d%d ", i, j);
}
}
Correct Answer: 01 02 03 04 10 12 13 14 20 21 23 24 30 31 32 34 40 41 42 43
Explanation: The `continue` statement skips the iterations where `i` equals `j`, so pairs like "00", "11", "22", "33", and "44" are not printed.
#include <stdio.h>
int i = 0;
do {
if (i == 2) {
i++;
continue;
}
printf("%d ", i);
i++;
} while (i < 5);
Correct Answer: 0 1 3 4
Explanation: The `do-while` loop increments `i` before and after the `continue` statement. When `i` equals 2, the `continue` statement skips the `printf` function, so 2 is not printed.
#include <stdio.h>
for (int i = 1; i <= 5; i++) {
if (i % 2 != 0) {
continue;
}
printf("%d ", i);
}
Correct Answer: 2 4
Explanation: The loop prints the values of `i` that are even. The `continue` statement skips the iterations where `i` is odd, so only 2 and 4 are printed.
#include <stdio.h>
int i = 1;
while (i <= 5) {
if (i % 3 == 0) {
i++;
continue;
}
printf("%d ", i);
i++;
}
Correct Answer: 1 2 4 5
Explanation: The `while` loop prints the values of `i` that are not multiples of 3. The `continue` statement skips the iterations where `i` is a multiple of 3, so 3 is not printed.
Correct Answer: It skips the rest of the current iteration and checks the loop condition
Explanation: In a `do-while` loop, the `continue` statement skips the remaining code in the current iteration and checks the loop condition to determine if the loop should continue.
#include <stdio.h>
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i + j == 5) {
continue;
}
printf("%d%d ", i, j);
}
}
Correct Answer: 00 01 02 03 10 11 12 13 20 21 22 30 31 40 41
Explanation: The `continue` statement skips the iterations where the sum of `i` and `j` equals 5. Therefore, pairs like "04", "13", "22", "31", and "40" are not printed.
Correct Answer: To transfer control to a labeled statement within a function
Explanation: The `goto` statement is used to transfer control to a labeled statement within the same function. It can be used to jump to different parts of the code.
#include <stdio.h>
goto label;
int label = 5;
#include <stdio.h>
label: printf("Hello");
goto label;
#include <stdio.h>
goto label;
label: printf("Hello");
#include <stdio.h>
goto label:
printf("Hello");
Correct Answer: C
#include <stdio.h>
goto label;
label: printf("Hello");
Explanation: Option C demonstrates a correct use of the `goto` statement. The `goto` statement transfers control to the labeled statement `label:`.
#include <stdio.h>
int main() {
int x = 0;
if (x == 0) {
goto label;
}
x = 1;
label:
printf("%d\n", x);
return 0;
}
Correct Answer: 0
Explanation: The `goto label;` statement transfers control to the `label:` statement, skipping the assignment `x = 1;`. Therefore, `x` remains 0 and is printed.
Correct Answer: It makes code harder to read and maintain
Explanation: The `goto` statement can lead to "spaghetti code," making it difficult to follow the program's logic and maintain the code. It is generally discouraged in favor of structured programming constructs.
Correct Answer: No
Explanation: The `goto` statement can only transfer control within the same function. It cannot be used to jump out of a function to another function.
#include <stdio.h>
int main() {
int x = 10;
for (int i = 0; i < 5; i++) {
if (i == 3) {
goto end;
}
x++;
}
end:
printf("%d\n", x);
return 0;
}
Correct Answer: 13
Explanation: The loop increments `x` three times before `goto end;` transfers control to the `end:` label, skipping the rest of the loop. Thus, `x` becomes 13.
Correct Answer: none of the above
Explanation: A label in C is defined by an identifier followed by a colon (`:`). There is no specific keyword for defining a label.
#include <stdio.h>
int main() {
int i = 1;
start:
printf("%d ", i);
i++;
if (i <= 3) {
goto start;
}
return 0;
}
Correct Answer: 1 2 3
Explanation: The `goto start;` statement creates a loop by transferring control back to the `start:` label until `i` exceeds 3, printing 1, 2, and 3.
Correct Answer: Within the function
Explanation: A label used with the `goto` statement has function scope. It can be accessed anywhere within the same function where it is defined.
#include <stdio.h>
int main() {
int x = 5;
goto skip;
x = 10;
skip:
printf("%d\n", x);
return 0;
}
Correct Answer: 5
Explanation: The `goto skip;` statement transfers control directly to the `skip:` label, bypassing the assignment `x = 10;`. Thus, `x` remains 5 and is printed.
Correct Answer: No
Explanation: Using `goto` to jump into a loop can lead to undefined behavior and is generally not allowed. Control structures should be used to manage loop entries and exits.
#include <stdio.h>
int main() {
int a = 1;
int b = 2;
if (a < b) {
goto greater;
}
printf("b is greater\n");
return 0;
greater:
printf("a is less\n");
return 0;
}
Correct Answer: a is less
Explanation: The `goto greater;` statement transfers control to the `greater:` label, so "a is less" is printed, bypassing the `printf("b is greater\n");` statement.
#include <stdio.h>
int main() {
int num = 1;
while (num < 3) {
num++;
if (num == 2) {
goto skip;
}
printf("%d ", num);
}
skip:
printf("%d\n", num);
return 0;
}
Correct Answer: 3
Explanation: The `goto skip;` statement transfers control to the `skip:` label, which prints the current value of `num`, which is 3.
Correct Answer: The code will produce a compilation error
Explanation: If the `goto` statement refers to a label that is never defined, the compiler will generate an error because it cannot resolve the label's target.
#include <stdio.h>
int main() {
int x = 0;
if (x == 0) {
goto label1;
}
x = 5;
label1:
printf("%d\n", x);
return 0;
}
Correct Answer: 0
Explanation: The `goto label1;` statement transfers control to `label1:`, skipping the assignment `x = 5;`. Therefore, `x` remains 0 and is printed.
Correct Answer: Spaghetti code
Explanation: Overuse of `goto` statements can lead to "spaghetti code," which is complex, tangled, and difficult to read and maintain. Structured programming constructs are preferred.
#include <stdio.h>
int main() {
int x = 10;
goto print_label;
x = 20;
print_label:
printf("%d\n", x);
return 0;
}
Correct Answer: 10
Explanation: The `goto print_label;` statement transfers control to `print_label:`, bypassing the assignment `x = 20;`. Thus, `x` remains 10 and is printed.
Correct Answer: Yes
Explanation: It is possible to use `goto` to jump to a label defined before the `goto` statement within the same function.
#include <stdio.h>
int main() {
int count = 0;
while (count < 5) {
if (count == 3) {
goto end;
}
printf("%d ", count);
count++;
}
end:
printf("End\n");
return 0;
}
Correct Answer: 0 1 2 End
Explanation: The `goto end;` statement transfers control to the `end:` label when `count` is 3, skipping the remaining iterations of the loop. Thus, 0, 1, 2, and "End" are printed.
Correct Answer: Exiting deeply nested loops or conditionals
Explanation: The `goto` statement is typically used to exit from deeply nested loops or conditionals, as it can simplify the control flow in such cases.
#include <stdio.h>
int main() {
int i = 0;
loop:
printf("%d ", i);
i++;
if (i < 3) {
goto loop;
}
return 0;
}
Correct Answer: 0 1 2
Explanation: The `goto loop;` statement creates a loop that prints the values of `i` from 0 to 2, then exits when `i` becomes 3.
#include <stdio.h>
int main() {
int i = 0;
for (i = 0; i < 5; i++) {
if (i == 2) {
goto done;
}
}
done:
printf("%d\n", i);
return 0;
}
Correct Answer: 2
Explanation: The `goto done;` statement transfers control to the `done:` label when `i` is 2, so 2 is printed.
#include <stdio.h>
int main() {
int a = 1;
int b = 2;
if (a < b) {
goto print_b;
}
printf("a is greater\n");
return 0;
print_b:
printf("b is greater\n");
return 0;
}
Correct Answer: b is greater
Explanation: The `goto print_b;` statement transfers control to the `print_b:` label, so "b is greater" is printed, bypassing the `printf("a is greater\n");` statement.
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
if (i == 3) {
goto skip;
}
printf("%d ", i);
i++;
}
skip:
printf("skip\n");
return 0;
}
Correct Answer: 0 1 2 skip
Explanation: The `goto skip;` statement transfers control to the `skip:` label when `i` is 3, skipping the remaining iterations of the loop. Thus, 0, 1, 2, and "skip" are printed.
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
if (a > b) {
goto label;
}
printf("b is greater than a\n");
return 0;
label:
printf("a is greater than b\n");
return 0;
}
Correct Answer: b is greater than a
Explanation: The condition `if (a > b)` is false, so the `goto label;` statement is not executed. "b is greater than a" is printed instead.
Correct Answer: An identifier followed by a colon
Explanation: A label in C programming is an identifier followed by a colon (`:`). It is used as a target for `goto` statements within a function.
Correct Answer: Anywhere within a function
Explanation: A label can be defined anywhere within a function. It serves as a target for the `goto` statement.
Correct Answer: No
Explanation: Labels must be unique within the same function. Duplicating a label within a function will result in a compilation error.
#include <stdio.h>
int main() {
int x = 0;
if (x == 0) {
goto label;
}
x = 5;
label:
printf("%d\n", x);
return 0;
}
Correct Answer: 0
Explanation: The `goto label;` statement transfers control to the `label:` statement, bypassing the assignment `x = 5;`. Therefore, `x` remains 0 and is printed.
Correct Answer: Function
Explanation: The scope of a label is limited to the function in which it is defined. It cannot be accessed from outside that function.
Correct Answer: No
Explanation: Each label name must be unique within a function. Reusing the same label name within a function will result in a compilation error.
#include <stdio.h>
int main() {
int i = 0;
loop:
printf("%d ", i);
i++;
if (i < 3) {
goto loop;
}
return 0;
}
Correct Answer: 0 1 2
Explanation: The `goto loop;` statement creates a loop that prints the values of `i` from 0 to 2, then exits when `i` becomes 3.
Correct Answer: Yes
Explanation: A label can be defined anywhere within a function, including inside an `if` statement.
#include <stdio.h>
int main() {
int num = 1;
if (num == 1) {
goto label;
}
num = 2;
label:
printf("%d\n", num);
return 0;
}
Correct Answer: 1
Explanation: The `goto label;` statement transfers control to the `label:` statement, skipping the assignment `num = 2;`. Therefore, `num` remains 1 and is printed.
Correct Answer: label_1
Explanation: A valid label name must start with a letter or underscore and can be followed by letters, digits, or underscores. "label_1" meets these criteria.
#include <stdio.h>
int main() {
int x = 5;
goto skip;
x = 10;
skip:
printf("%d\n", x);
return 0;
}
Correct Answer: 5
Explanation: The `goto skip;` statement transfers control to the `skip:` label, bypassing the assignment `x = 10;`. Thus, `x` remains 5 and is printed.
Correct Answer: Yes
Explanation: A label can be defined inside a loop. It can be used with `goto` to jump to the labeled statement within the same function.
#include <stdio.h>
int main() {
int i = 0;
for (i = 0; i < 5; i++) {
if (i == 2) {
goto done;
}
printf("%d ", i);
}
done:
printf("%d\n", i);
return 0;
}
Correct Answer: 0 1 2
Explanation: The `goto done;` statement transfers control to the `done:` label when `i` is 2, so the loop terminates early, printing 0, 1, and 2.
Correct Answer: Yes
Explanation: Labels can be used with `goto` statements inside switch statements to transfer control to specific parts of the code within the same function.
#include <stdio.h>
int main() {
int x = 0;
while (x < 3) {
if (x == 2) {
goto end;
}
printf("%d ", x);
x++;
}
end:
printf("end\n");
return 0;
}
Correct Answer: 0 1 end
Explanation: The `goto end;` statement transfers control to the `end:` label when `x` is 2, so 0, 1, and "end" are printed.