0
0
C++programming~10 mins

Jump statement overview 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 exit the loop immediately when i equals 3.

C++
for (int i = 0; i < 5; i++) {
    if (i == 3) {
        [1];
    }
    std::cout << i << std::endl;
}
Drag options to blanks, or click blank then click option'
Abreak
Bcontinue
Creturn
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'continue' instead of 'break' will skip the rest of the loop but not exit it.
2fill in blank
medium

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

C++
for (int i = 0; i < 5; i++) {
    if (i == 2) {
        [1];
    }
    std::cout << i << std::endl;
}
Drag options to blanks, or click blank then click option'
Aexit
Bbreak
Creturn
Dcontinue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' will stop the loop entirely, not just skip one iteration.
3fill in blank
hard

Fix the error in the code to return from the function when x is negative.

C++
void checkNumber(int x) {
    if (x < 0) {
        [1];
    }
    std::cout << "Number is non-negative" << std::endl;
}
Drag options to blanks, or click blank then click option'
Areturn
Bcontinue
Cbreak
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' or 'continue' outside loops causes errors.
4fill in blank
hard

Fill both blanks to create a loop that skips even numbers and stops when i reaches 7.

C++
for (int i = 0; i < 10; i++) {
    if (i [1] 2 [2] 0) {
        continue;
    }
    if (i == 7) {
        break;
    }
    std::cout << i << std::endl;
}
Drag options to blanks, or click blank then click option'
A%
B==
C!=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators or 'break' instead of 'continue'.
5fill in blank
hard

Fill all three blanks to create a function that returns the first positive number in an array or -1 if none found.

C++
int firstPositive(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr[[1]] [2] 0) {
            [3] arr[i];
        }
    }
    return -1;
}
Drag options to blanks, or click blank then click option'
Ai
B>
Creturn
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong index or comparison, or forgetting to return the value.