Jump statements help you change the flow of your program quickly. They let you skip parts or repeat actions easily.
Jump statement overview in C++
break; continue; return expression; goto label; label: // code here
break stops the nearest loop or switch.
continue skips to the next loop iteration.
return exits a function and optionally sends back a value.
goto jumps to a labeled statement (use carefully).
for (int i = 0; i < 5; i++) { if (i == 3) break; std::cout << i << " "; }
for (int i = 0; i < 5; i++) { if (i == 2) continue; std::cout << i << " "; }
int add(int a, int b) { return a + b; }
int x = 0; goto skip; x = 5; skip: std::cout << x;
This program loops from 0 to 4. It skips printing when i is 1, and stops the loop when i is 3.
#include <iostream> int main() { for (int i = 0; i < 5; i++) { if (i == 3) { std::cout << "Breaking at " << i << "\n"; break; } if (i == 1) { std::cout << "Skipping " << i << "\n"; continue; } std::cout << "Number: " << i << "\n"; } return 0; }
Use break and continue only inside loops or switch statements.
return ends the current function immediately.
goto can make code hard to read; use it only when necessary.
Jump statements control program flow by skipping or stopping parts.
break exits loops or switches early.
continue skips to the next loop cycle.
return exits a function and optionally sends back a value.
goto jumps to a labeled spot in code (use carefully).
Practice
Solution
Step 1: Understand the purpose of
Thebreakbreakstatement immediately exits the nearest enclosing loop or switch.Step 2: Compare with other jump statements
continueskips to the next loop iteration,returnexits a function, andgotojumps to a label.Final Answer:
break-> Option AQuick Check:
Exit loop early =break[OK]
- Confusing break with continue
- Thinking return exits loops only
- Using goto without label
Solution
Step 1: Identify the statement that skips current iteration
Thecontinue;statement skips the rest of the current loop iteration and moves to the next.Step 2: Verify syntax correctness
skip;is invalid,break;exits loop,return;exits function.Final Answer:
continue; -> Option CQuick Check:
Skip iteration = continue [OK]
- Using break to skip iteration
- Using return inside loops incorrectly
- Writing skip instead of continue
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
std::cout << i << " ";
}
return 0;
}Solution
Step 1: Analyze the loop and condition
The loop runs from 1 to 5. Wheni == 3,continue;skips printing 3.Step 2: Determine printed values
Values 1, 2, 4, and 5 are printed with spaces.Final Answer:
1 2 4 5 -> Option AQuick Check:
Skip 3 with continue = 1 2 4 5 [OK]
- Including 3 in output
- Confusing break with continue
- Not printing spaces correctly
int main() {
for (int i = 0; i < 5; i++) {
if (i == 2) break
std::cout << i << " ";
}
return 0;
}Solution
Step 1: Check syntax of break statement
Thebreakstatement must end with a semicolon. Here it is missing.Step 2: Confirm other parts are correct
The loop condition and return statement are correct; break is valid inside loops.Final Answer:
Missing semicolon after break statement -> Option DQuick Check:
break needs semicolon [OK]
- Omitting semicolon after break
- Thinking break can't be inside loops
- Confusing break with continue syntax
int findFirstEven(int arr[], int size) {
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) return arr[i];
}
return -1;
}What does the
return statement inside the loop do?Solution
Step 1: Understand return inside a loop
Thereturninside the loop immediately exits the function when an even number is found.Step 2: Confirm behavior after return
No further iterations or code run after return; function ends and returns value.Final Answer:
Exits the loop and returns the first even number found -> Option BQuick Check:
Return exits function immediately [OK]
- Thinking return skips iteration only
- Confusing return with break
- Assuming function continues after return
Ready to prove it?
Pass the test and this pattern moves unknown
