0
0
C++programming~30 mins

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

Choose your learning style9 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++
Need a 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++
Need a 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++
Need a 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++
Need a hint?

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