Bird
0
0

Which of the following code snippets correctly implements an if-else statement in C to check if a number is positive?

easy📝 Syntax Q3 of 15
C - onditional Statements
Which of the following code snippets correctly implements an if-else statement in C to check if a number is positive?
Aif (num > 0) { printf("Positive"); } else { printf("Not positive"); }
Bif num > 0 { printf("Positive"); } else { printf("Not positive"); }
Cif (num > 0) printf("Positive"); else printf("Not positive"); }
Dif (num > 0) printf("Positive"); else if (num <= 0) printf("Not positive");
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax of if condition

    The condition must be inside parentheses: if (num > 0).
  2. Step 2: Use braces for if and else blocks

    Braces { } are required to group multiple statements or to clearly define the blocks.
  3. Step 3: Verify else block syntax

    The else block must follow the if block without a condition and with braces.
  4. Final Answer:

    if (num > 0) { printf("Positive"); } else { printf("Not positive"); } correctly uses parentheses and braces for the if-else statement.
  5. Quick Check:

    Parentheses and braces are mandatory for correct if-else syntax. [OK]
Quick Trick: Always use parentheses and braces in if-else statements. [OK]
Common Mistakes:
  • Omitting parentheses around the condition
  • Missing braces for if or else blocks
  • Using else with a condition (else if is different)
  • Incorrect placement of braces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes