0
0
C++programming~10 mins

Loop execution flow in C++ - Interactive Code Practice

Choose your learning style9 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 for 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 < will stop before printing 5.
Using > or >= will cause the loop to not run as expected.
2fill in blank
medium

Complete the code to skip printing the number 3 inside the loop.

C++
#include <iostream>

int main() {
    for (int i = 1; i <= 5; i++) {
        if (i [1] 3) {
            continue;
        }
        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 != will skip all numbers except 3.
Using > or < will skip more numbers than intended.
3fill in blank
hard

Fix the error in the while loop condition to stop the loop after printing numbers 1 to 5.

C++
#include <iostream>

int main() {
    int count = 1;
    while (count [1] 5) {
        std::cout << count << std::endl;
        count++;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A>=
B<
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will stop before printing 5.
Using > or >= will cause the loop to never run.
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 num = 2; num [1] 10; num [2]) {
        std::cout << num << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A<=
B<
C++
D+= 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will exclude 10 from output.
Using ++ increments by 1, printing odd numbers too.
5fill in blank
hard

Fill all three blanks to create a loop that prints numbers from 10 down to 1.

C++
#include <iostream>

int main() {
    for (int i = [1]; i [2] 1; i [3]) {
        std::cout << i << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A1
B10
C>=
D>
E--
F++
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 1 will not count down.
Using ++ will increase i instead of decreasing.
Using > instead of >= will exclude 1.