Nested If Statement in C

Level 267 keystrokes

Uses an if inside another if to check that x (5) is both positive and less than 10; both checks pass, so it prints "Single digit".

C code

// The value to test is 5
1int x = 5;
// Outer check: is x positive? 5 > 0 is true, so the block is entered
2if (x > 0) {
// Inner check, reached only if the outer one passed: is x below 10?
3 if (x < 10) {
// Both conditions are true (0 < x < 10), so "Single digit" is printed
4 printf("Single digit\n");
5 }
6}

Output

Single digit
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • (× 3
  • )× 3
  • ;× 2
  • "× 2
  • {× 2
  • }× 2
  • \× 1
  • <× 1
  • =× 1
  • >× 1

More C examples at level 2

← C typing practice