Bird
Raised Fist0
C++programming~10 mins

Why loops are needed in C++ - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 5 using a loop.

C++
#include <iostream>

int main() {
    for (int i = 1; i [1] 5; i++) {
        std::cout << i << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < prints only 1 to 4.
Using > or >= causes the loop to never run or run incorrectly.
2fill in blank
medium

Complete the code to sum numbers from 1 to 10 using a loop.

C++
#include <iostream>

int main() {
    int sum = 0;
    for (int num = 1; num [1] 10; num++) {
        sum += num;
    }
    std::cout << sum << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using < excludes 10 from the sum.
Using > or >= causes the loop to not run as expected.
3fill in blank
hard

Fix the error in the loop condition to print numbers from 0 to 4.

C++
#include <iostream>

int main() {
    for (int i = 0; i [1] 5; i++) {
        std::cout << i << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A>
B<=
C>=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= prints 0 to 5 instead of 0 to 4.
Using > or >= causes the loop to not run or run incorrectly.
4fill in blank
hard

Fill both blanks to create a loop that prints even numbers from 2 to 10.

C++
#include <iostream>

int main() {
    for (int i = [1]; i [2] 10; i += 2) {
        std::cout << i << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A2
B1
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 prints odd numbers.
Using < excludes 10 from the output.
5fill in blank
hard

Fill all three blanks to create a loop that prints the squares of numbers from 1 to 5.

C++
#include <iostream>

int main() {
    for (int [1] = 1; [2] [3] 5; [1]++) {
        std::cout << [1] * [1] << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Ai
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in initialization and condition causes errors.
Using < excludes 5 from the output.

Practice

(1/5)
1. Why do we use loops in C++ programming?
easy
A. To repeat a set of instructions multiple times without writing them again
B. To store multiple values in a single variable
C. To make the program run faster by skipping code
D. To create new variables automatically

Solution

  1. Step 1: Understand the purpose of loops

    Loops allow repeating the same instructions many times without rewriting the code.
  2. Step 2: Compare other options

    Options A, C, and D describe other programming concepts, not loops.
  3. Final Answer:

    To repeat a set of instructions multiple times without writing them again -> Option A
  4. Quick Check:

    Loops repeat actions [OK]
Hint: Loops repeat code to save writing it many times [OK]
Common Mistakes:
  • Confusing loops with variables
  • Thinking loops speed up code by skipping
  • Mixing loops with data storage
2. Which of the following is the correct syntax for a for loop in C++?
easy
A. for (int i < 5; i = 0; i++) { /* code */ }
B. for i = 0 to 5 { /* code */ }
C. for (int i = 0; i < 5; i++) { /* code */ }
D. loop (int i = 0; i < 5; i++) { /* code */ }

Solution

  1. Step 1: Recall correct for loop syntax

    The correct syntax uses: for (initialization; condition; update) { code }
  2. Step 2: Check each option

    for (int i = 0; i < 5; i++) { /* code */ } matches the correct syntax. Options A, B, and C have syntax errors.
  3. Final Answer:

    for (int i = 0; i < 5; i++) { /* code */ } -> Option C
  4. Quick Check:

    Standard for loop syntax [OK]
Hint: For loops use three parts inside parentheses separated by semicolons [OK]
Common Mistakes:
  • Using 'for i = 0 to 5' like other languages
  • Writing 'loop' instead of 'for'
  • Mixing order of initialization and condition
3. What will be the output of this C++ code?
for (int i = 1; i <= 3; i++) {
    std::cout << i << " ";
}
medium
A. 0 1 2
B. 1 2 3
C. 1 2 3 4
D. 3 2 1

Solution

  1. Step 1: Analyze loop range and output

    The loop starts at i=1 and runs while i <= 3, printing i each time.
  2. Step 2: List printed values

    Values printed are 1, 2, and 3, each followed by a space.
  3. Final Answer:

    1 2 3 -> Option B
  4. Quick Check:

    Loop prints 1 to 3 [OK]
Hint: Check loop start, end, and print inside loop [OK]
Common Mistakes:
  • Starting loop at 0 instead of 1
  • Including extra number beyond 3
  • Printing in reverse order
4. Identify the error in this C++ loop code:
int i = 0;
while (i < 3) {
    std::cout << i << " ";
}
medium
A. Missing semicolon after std::cout
B. Variable i is not declared
C. Incorrect while loop syntax
D. No update to i inside the loop causing infinite loop

Solution

  1. Step 1: Check loop structure

    The while loop condition is correct and variable i is declared.
  2. Step 2: Look for loop update

    There is no statement changing i inside the loop, so i stays 0 forever, causing infinite loop.
  3. Final Answer:

    No update to i inside the loop causing infinite loop -> Option D
  4. Quick Check:

    Loops need variable update to end [OK]
Hint: Always update loop variable inside while loops [OK]
Common Mistakes:
  • Forgetting to increment loop variable
  • Assuming loop ends automatically
  • Misreading syntax errors
5. You want to print all even numbers from 2 to 10 using a loop in C++. Which loop is best and why?
hard
A. Use a for loop with i starting at 2, incrementing by 2 until 10
B. Use a while loop starting at 1, incrementing by 1, and print if even
C. Use a do-while loop starting at 10, decrementing by 2 until 2
D. Use a for loop starting at 0, incrementing by 1, and print if even

Solution

  1. Step 1: Consider loop control and efficiency

    Starting at 2 and incrementing by 2 prints only even numbers directly.
  2. Step 2: Compare other options

    Use a while loop starting at 1, incrementing by 1, and print if even checks each number if even, less efficient. Use a do-while loop starting at 10, decrementing by 2 until 2 counts backward, which is okay but less natural here. Use a for loop starting at 0, incrementing by 1, and print if even starts at 0 and prints even numbers but includes 0 and extra checks.
  3. Final Answer:

    Use a for loop with i starting at 2, incrementing by 2 until 10 -> Option A
  4. Quick Check:

    Directly loop over even numbers [OK]
Hint: Increment by 2 to print even numbers efficiently [OK]
Common Mistakes:
  • Using loops that check every number unnecessarily
  • Starting loop at wrong number
  • Confusing increment direction
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