0
0
C++programming~10 mins

If statement 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 check if the number is positive.

C++
int number = 5;
if (number [1] 0) {
    std::cout << "Positive" << std::endl;
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will check if the number is negative.
Using '==' checks for equality, not positivity.
Using '!=' checks if number is not zero, which includes negatives.
2fill in blank
medium

Complete the code to print "Even" if the number is divisible by 2.

C++
int number = 4;
if (number [1] 2 == 0) {
    std::cout << "Even" << std::endl;
}
Drag options to blanks, or click blank then click option'
A*
B%
C/
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' divides the number but does not check remainder.
Using '*' or '+' are arithmetic operations not related to divisibility.
3fill in blank
hard

Fix the error in the if statement to check if x is not equal to 10.

C++
int x = 7;
if (x [1] 10) {
    std::cout << "Not 10" << std::endl;
}
Drag options to blanks, or click blank then click option'
A=
B==
C!=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' causes assignment, not comparison.
Using '==' checks equality, not inequality.
Using '<' checks if less than, not inequality.
4fill in blank
hard

Fill in the blank to print "Adult" if age is 18 or more.

C++
int age = 20;
if (age [1] 18) {
    std::cout << "Adult" << std::endl;
}
Drag options to blanks, or click blank then click option'
A<=
B||
C&&
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' checks if age is less than or equal to 18, which is opposite.
Using '&&' or '||' incorrectly changes logic.
5fill in blank
hard

Fill all three blanks to print "Teenager" if age is between 13 and 19 inclusive.

C++
int age = 15;
if (age [1] 13 [2] age [3] 19) {
    std::cout << "Teenager" << std::endl;
}
Drag options to blanks, or click blank then click option'
A>=
B<=
C&&
D||
Attempts:
3 left
💡 Hint
Common Mistakes
Using '||' instead of '&&' allows ages outside the range.
Swapping '>=' and '<=' changes the logic.
Using '=' instead of comparison operators causes errors.