Bird
Raised Fist0
C++programming~10 mins

If statement in C++ - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does an if statement do in C++?
easy
A. Defines a new variable
B. Repeats code multiple times
C. Runs code only if a condition is true
D. Ends the program immediately

Solution

  1. Step 1: Understand the purpose of if statement

    An if statement checks a condition and runs code only when that condition is true.
  2. Step 2: Compare with other options

    Repeating code is done by loops, defining variables is done by declarations, and ending the program is done by return or exit.
  3. Final Answer:

    Runs code only if a condition is true -> Option C
  4. Quick Check:

    If statement = runs code if true [OK]
Hint: If checks true condition to run code [OK]
Common Mistakes:
  • Confusing if with loops
  • Thinking if defines variables
  • Believing if ends program
2. Which of the following is the correct syntax for an if statement in C++?
easy
A. if condition { cout << "Yes"; }
B. if condition: cout << "Yes";
C. if (condition) cout << "Yes"
D. if (condition) { cout << "Yes"; }

Solution

  1. Step 1: Recall correct if syntax

    In C++, the condition must be inside parentheses and the code block inside curly braces.
  2. Step 2: Check each option

    if (condition) { cout << "Yes"; } uses parentheses and curly braces correctly. if condition { cout << "Yes"; } misses parentheses. if condition: cout << "Yes"; uses colon which is invalid in C++. if (condition) cout << "Yes" misses curly braces which is allowed only for single statements but here it is incomplete.
  3. Final Answer:

    if (condition) { cout << "Yes"; } -> Option D
  4. Quick Check:

    Parentheses + braces = correct if syntax [OK]
Hint: Use parentheses for condition and braces for code [OK]
Common Mistakes:
  • Missing parentheses
  • Using colon instead of braces
  • Omitting braces for multiple statements
3. What will be the output of this code?
int x = 10;
if (x > 5) {
  cout << "Hello";
} else {
  cout << "Bye";
}
medium
A. Hello
B. Bye
C. HelloBye
D. No output

Solution

  1. Step 1: Evaluate the condition x > 5

    Since x is 10, which is greater than 5, the condition is true.
  2. Step 2: Determine which block runs

    Because the condition is true, the code inside the if block runs, printing "Hello".
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    10 > 5 is true, prints Hello [OK]
Hint: Check condition true or false to pick output [OK]
Common Mistakes:
  • Confusing else output
  • Ignoring condition result
  • Assuming both blocks run
4. Find the error in this code snippet:
int a = 3;
if a > 0 {
  cout << "Positive";
}
medium
A. Missing semicolon after if
B. Condition must be inside parentheses
C. Missing curly braces
D. Variable a is not declared

Solution

  1. Step 1: Check the if statement syntax

    The condition in an if statement must be inside parentheses.
  2. Step 2: Identify the error

    The code writes if a > 0 { without parentheses, which is invalid syntax.
  3. Final Answer:

    Condition must be inside parentheses -> Option B
  4. Quick Check:

    If condition needs parentheses [OK]
Hint: Always put condition inside parentheses [OK]
Common Mistakes:
  • Omitting parentheses
  • Thinking semicolon needed after if
  • Assuming braces optional always
5. You want to print "Even" if a number is even, and "Odd" if it is odd. Which code correctly uses an if statement to do this?
int num = 7;
// Your code here
hard
A. if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; }
B. if (num / 2 == 0) { cout << "Even"; } else { cout << "Odd"; }
C. if (num % 2 = 0) { cout << "Even"; } else { cout << "Odd"; }
D. if (num % 2 != 0) { cout << "Even"; } else { cout << "Odd"; }

Solution

  1. Step 1: Understand how to check even numbers

    A number is even if dividing by 2 leaves remainder 0, checked by num % 2 == 0.
  2. Step 2: Analyze each option

    if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; } correctly uses num % 2 == 0. if (num / 2 == 0) { cout << "Even"; } else { cout << "Odd"; } uses division instead of modulo, which is wrong. if (num % 2 = 0) { cout << "Even"; } else { cout << "Odd"; } uses assignment = instead of comparison ==. if (num % 2 != 0) { cout << "Even"; } else { cout << "Odd"; } reverses the logic.
  3. Final Answer:

    if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; } -> Option A
  4. Quick Check:

    Modulo 2 equals 0 means even [OK]
Hint: Use modulo (%) to check evenness [OK]
Common Mistakes:
  • Using division instead of modulo
  • Using = instead of ==
  • Reversing condition logic
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