Bird
Raised Fist0
C++programming~30 mins

Jump statement overview in C++ - Mini Project: Build & Apply

Choose your learning style10 modes available
Jump Statement Overview in C++
📖 Scenario: Imagine you are writing a simple program to manage a small game where players can skip turns, exit early, or jump to specific parts of the game logic.
🎯 Goal: You will create a C++ program that uses jump statements like break, continue, and return to control the flow of a loop and a function.
📋 What You'll Learn
Create a for loop that counts from 1 to 5
Use a continue statement to skip printing the number 3
Use a break statement to stop the loop when the number reaches 4
Create a function called checkNumber that returns early using return if the number is 2
Print the numbers processed by the function
💡 Why This Matters
🌍 Real World
Jump statements are used in games, user input handling, and many programs to control flow based on conditions.
💼 Career
Understanding jump statements is essential for writing clear and efficient code in software development and debugging.
Progress0 / 4 steps
1
Create a for loop from 1 to 5
Write a for loop with the variable i that counts from 1 to 5 inclusive.
C++
Hint

Use for (int i = 1; i <= 5; i++) to count from 1 to 5.

2
Use continue to skip number 3
Inside the for loop, add an if statement that checks if i is 3. Use continue to skip printing when i is 3.
C++
Hint

Use if (i == 3) { continue; } inside the loop to skip number 3.

3
Use break to stop the loop at number 4
Add another if statement inside the for loop that checks if i is 4. Use break to stop the loop when i reaches 4.
C++
Hint

Use if (i == 4) { break; } to stop the loop early.

4
Create a function with return to exit early and print results
Create a function called checkNumber that takes an int parameter num. Inside the function, if num is 2, use return to exit early. Otherwise, print num. Then, call checkNumber inside the for loop for each i.
C++
Hint

Define checkNumber with an if that returns early if num == 2. Otherwise, print num. Call it inside the loop.

Practice

(1/5)
1. Which jump statement in C++ is used to immediately exit a loop or switch statement?
easy
A. break
B. continue
C. return
D. goto

Solution

  1. Step 1: Understand the purpose of break

    The break statement immediately exits the nearest enclosing loop or switch.
  2. Step 2: Compare with other jump statements

    continue skips to the next loop iteration, return exits a function, and goto jumps to a label.
  3. Final Answer:

    break -> Option A
  4. Quick Check:

    Exit loop early = break [OK]
Hint: Break stops loops/switch immediately [OK]
Common Mistakes:
  • Confusing break with continue
  • Thinking return exits loops only
  • Using goto without label
2. Which of the following is the correct syntax to skip the current iteration in a loop in C++?
easy
A. skip;
B. break;
C. continue;
D. return;

Solution

  1. Step 1: Identify the statement that skips current iteration

    The continue; statement skips the rest of the current loop iteration and moves to the next.
  2. Step 2: Verify syntax correctness

    skip; is invalid, break; exits loop, return; exits function.
  3. Final Answer:

    continue; -> Option C
  4. Quick Check:

    Skip iteration = continue [OK]
Hint: Continue skips current loop iteration [OK]
Common Mistakes:
  • Using break to skip iteration
  • Using return inside loops incorrectly
  • Writing skip instead of continue
3. What will be the output of the following C++ code?
int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) continue;
        std::cout << i << " ";
    }
    return 0;
}
medium
A. 1 2 4 5
B. 1 2
C. 1 2 3 4 5
D. 3 4 5

Solution

  1. Step 1: Analyze the loop and condition

    The loop runs from 1 to 5. When i == 3, continue; skips printing 3.
  2. Step 2: Determine printed values

    Values 1, 2, 4, and 5 are printed with spaces.
  3. Final Answer:

    1 2 4 5 -> Option A
  4. Quick Check:

    Skip 3 with continue = 1 2 4 5 [OK]
Hint: Continue skips printing 3, so output excludes 3 [OK]
Common Mistakes:
  • Including 3 in output
  • Confusing break with continue
  • Not printing spaces correctly
4. Identify the error in this C++ code snippet:
int main() {
    for (int i = 0; i < 5; i++) {
        if (i == 2) break
        std::cout << i << " ";
    }
    return 0;
}
medium
A. Missing return statement
B. Incorrect loop condition
C. Using break inside loop is invalid
D. Missing semicolon after break statement

Solution

  1. Step 1: Check syntax of break statement

    The break statement must end with a semicolon. Here it is missing.
  2. Step 2: Confirm other parts are correct

    The loop condition and return statement are correct; break is valid inside loops.
  3. Final Answer:

    Missing semicolon after break statement -> Option D
  4. Quick Check:

    break needs semicolon [OK]
Hint: Always put semicolon after break [OK]
Common Mistakes:
  • Omitting semicolon after break
  • Thinking break can't be inside loops
  • Confusing break with continue syntax
5. Consider this C++ function:
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?
hard
A. Skips the current iteration and continues the loop
B. Exits the loop and returns the first even number found
C. Exits the function only after the loop finishes
D. Jumps to a labeled statement

Solution

  1. Step 1: Understand return inside a loop

    The return inside the loop immediately exits the function when an even number is found.
  2. Step 2: Confirm behavior after return

    No further iterations or code run after return; function ends and returns value.
  3. Final Answer:

    Exits the loop and returns the first even number found -> Option B
  4. Quick Check:

    Return exits function immediately [OK]
Hint: Return exits function immediately, not just loop [OK]
Common Mistakes:
  • Thinking return skips iteration only
  • Confusing return with break
  • Assuming function continues after return
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard