The goto statement in C is a way to jump directly to another part of the program marked by a label. It helps to change the flow of execution.
Click to reveal answer
beginner
How do you define a label for goto in C?
A label is defined by writing an identifier followed by a colon, like label_name:. The goto statement can jump to this label.
Click to reveal answer
intermediate
Why should goto be used carefully?
Using goto too much can make code hard to read and follow, like a messy map. It can cause bugs and confusion, so it is usually better to use loops and functions.
Click to reveal answer
beginner
Can goto jump outside a function in C?
No, goto can only jump to labels within the same function. It cannot jump between different functions.
Click to reveal answer
beginner
Example: What does this code do?
int x = 0;
goto skip;
x = 5;
skip:
printf("%d", x);
The code jumps over x = 5; and prints 0 because the goto skip; moves execution directly to the label skip:.
Click to reveal answer
What does the goto statement do in C?
AStarts a new function
BEnds the program
CJumps to a labeled part of the code
DDeclares a variable
✗ Incorrect
The goto statement jumps to a label inside the same function.
How do you mark a place to jump to with goto?
AWith a function name
BWith a label followed by a colon
CWith a comment
DWith a variable
✗ Incorrect
Labels are identifiers followed by a colon, like label:.
Can goto jump to a label in another function?
ANo, only within the same function
BYes, anywhere in the program
COnly if the functions are nested
DOnly if the label is global
✗ Incorrect
goto works only inside the same function.
What is a common problem with using goto too much?
AIt causes syntax errors
BIt makes the program run slower
CIt uses too much memory
DCode becomes hard to read and maintain
✗ Incorrect
Excessive use of goto can make code confusing and messy.
Which of these is a correct label for goto?
Astart:
Bint start;
Cgoto start;
Dlabel start
✗ Incorrect
Labels are written as an identifier followed by a colon, like start:.
Explain how the goto statement works and when it should be used.
Think about how <code>goto</code> changes program flow and why it can be risky.
You got /4 concepts.
Describe a simple example where goto skips a part of the code.
Imagine skipping a step in a recipe using a shortcut.
You got /4 concepts.
Practice
(1/5)
1. What does the goto statement do in C programming?
easy
A. It jumps to a labeled part of the code within the same function.
B. It creates a new function.
C. It declares a variable.
D. It ends the program immediately.
Solution
Step 1: Understand the purpose of goto
The goto statement is used to jump to a specific label inside the same function.
Step 2: Compare with other options
Other options describe unrelated actions like creating functions or ending programs, which goto does not do.
Final Answer:
It jumps to a labeled part of the code within the same function. -> Option A
Quick Check:
goto jumps to label [OK]
Hint: Remember: goto jumps only inside the same function [OK]
Common Mistakes:
Thinking goto can jump between functions
Confusing goto with function calls
Believing goto ends the program
2. Which of the following is the correct syntax to use goto in C?
easy
A. goto: label_name;
B. goto(label_name);
C. goto label_name;
D. goto = label_name;
Solution
Step 1: Recall correct goto syntax
The correct syntax is simply goto label_name; without parentheses or equals sign.
Step 2: Check other options for syntax errors
The other options use invalid syntax: one with a colon after goto, one with parentheses, and one with an equals sign.
Final Answer:
goto label_name; -> Option C
Quick Check:
Correct syntax is goto label; [OK]
Hint: Use 'goto label;' without parentheses or equals [OK]
Common Mistakes:
Adding parentheses after goto
Using equals sign with goto
Writing colon after goto
3. What will be the output of this C code?
int main() {
int x = 0;
start:
x++;
if (x < 3) goto start;
printf("%d", x);
return 0;
}
medium
A. 3
B. 0
C. 1
D. Infinite loop
Solution
Step 1: Trace the loop with goto
The label start is before x++. The code increments x, then checks if x < 3. If yes, it jumps back to start.
Step 2: Count increments until condition fails
x starts at 0, increments to 1, 2, then 3. When x is 3, condition x < 3 is false, so it stops looping and prints 3.
Final Answer:
3 -> Option A
Quick Check:
Loop increments x to 3 before printing [OK]
Hint: Count increments until condition fails [OK]
Common Mistakes:
Thinking it prints 0 or 1
Assuming infinite loop without checking condition
Confusing label position
4. Identify the error in this C code using goto:
int main() {
goto end;
printf("Hello");
end:
return 0;
}
medium
A. Cannot jump over variable declarations.
B. No error; code runs and prints nothing.
C. Label 'end' is not defined.
D. Syntax error in goto statement.
Solution
Step 1: Check label definition and usage
The label end is defined after the goto statement, so jumping to it is valid.
Step 2: Analyze program flow
The program jumps to end, skipping the printf. So it prints nothing and returns 0 without error.
Final Answer:
No error; code runs and prints nothing. -> Option B
Quick Check:
Jumping to label after goto is allowed [OK]
Hint: Jumping forward to label is allowed; no error [OK]
Common Mistakes:
Thinking label must be before goto
Expecting output when skipped
Assuming syntax error in goto
5. How can goto be used to break out of nested loops in C? Choose the best example.
hard
A. Use goto to jump to a label inside the inner loop.
B. Use goto to jump to a label after both loops finish.
C. Use goto to restart the inner loop from the beginning.
D. Use goto inside inner loop to jump to a label outside both loops.
Solution
Step 1: Understand nested loops and breaking out
Normally, break exits only one loop. To exit multiple loops, goto can jump to a label after all loops.
Step 2: Identify correct usage of goto
Using goto inside the inner loop to jump outside both loops immediately breaks out of all loops.
Final Answer:
Use goto inside inner loop to jump to a label outside both loops. -> Option D
Quick Check:
Goto jumps out of nested loops [OK]
Hint: Use goto to jump outside nested loops for quick exit [OK]
Common Mistakes:
Using goto to restart loops instead of exit
Jumping to label inside loops only
Thinking break exits all loops
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.