0
0
Cprogramming~10 mins

Goto statement overview in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Goto statement overview
Start
Execute statements
Check for goto
Jump to label
End
The program runs statements in order, but when it hits a goto, it jumps to a labeled spot and continues from there.
Execution Sample
C
int main() {
  int x = 0;
  if (x == 0) goto skip;
  x = 1;
  skip:
  return x;
}
This code uses goto to jump over the assignment x = 1 if x is 0, then returns x.
Execution Table
StepLineActionVariable xNotes
1int x = 0;Initialize x to 00x set to 0
2if (x == 0) goto skip;Check if x is 00Condition true, jump to label skip
3skip:Label reached0Jumped here, skip x=1
4return x;Return x value0Function returns 0
5-End of program0Program ends
💡 Program ends after returning x, which is 0 because assignment x=1 was skipped
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
xundefined0000
Key Moments - 2 Insights
Why does the assignment x = 1 not happen?
Because at step 2 in the execution_table, the condition x == 0 is true, so the goto jumps directly to the label 'skip', skipping the assignment line.
What happens if the goto condition is false?
If the condition was false, the program would continue normally without jumping, executing the assignment x = 1 before reaching the label.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x after step 3?
Aundefined
B0
C1
D2
💡 Hint
Check the 'Variable x' column at step 3 in the execution_table.
At which step does the program jump to the label 'skip'?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table for the goto decision.
If the condition in step 2 was false, what would be the value of x at the end?
A1
B0
Cundefined
D2
💡 Hint
If no jump occurs, the assignment x=1 would run before return.
Concept Snapshot
Goto statement overview in C:
- Syntax: goto label;
- Execution jumps to the label anywhere in the function
- Labels end with a colon (label:)
- Use carefully to avoid confusing flow
- Skips normal sequential execution
- Useful for simple jumps or error handling
Full Transcript
This example shows how the goto statement works in C. The program starts by setting x to 0. Then it checks if x equals 0. Since it does, the program jumps to the label 'skip', skipping the assignment x = 1. Finally, it returns x, which is still 0. The execution table shows each step and variable values. The variable tracker confirms x stays 0 throughout. Beginners often wonder why the assignment is skipped; it's because of the jump at step 2. If the condition was false, the program would run the assignment normally. The goto statement lets you jump to a labeled spot in the code, changing the normal flow.