Bird
0
0

Which of the following is the correct syntax for a nested if statement in C?

easy📝 Syntax Q12 of 15
C - onditional Statements

Which of the following is the correct syntax for a nested if statement in C?

if (x > 0) {
    if (y < 10) {
        // code
    }
}
Aif x > 0 { if (y < 10) { /* code */ } }
Bif (x > 0) if (y < 10) { /* code */ }
Cif (x > 0) { if y < 10 { /* code */ } }
Dif (x > 0) { if (y < 10) { /* code */ } }
Step-by-Step Solution
Solution:
  1. Step 1: Check correct use of braces and parentheses

    In C, conditions must be inside parentheses and blocks inside braces. if (x > 0) { if (y < 10) { /* code */ } } uses both correctly.
  2. Step 2: Identify syntax errors in other options

    if (x > 0) if (y < 10) { /* code */ } misses braces for outer if block. if (x > 0) { if y < 10 { /* code */ } } misses parentheses around y < 10. if x > 0 { if (y < 10) { /* code */ } } misses parentheses around x > 0.
  3. Final Answer:

    if (x > 0) { if (y < 10) { /* code */ } } -> Option D
  4. Quick Check:

    Correct syntax = if (x > 0) { if (y < 10) { /* code */ } } [OK]
Quick Trick: Use parentheses for conditions and braces for blocks [OK]
Common Mistakes:
  • Omitting parentheses around conditions
  • Forgetting braces for code blocks
  • Mixing syntax from other languages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes