if3.c

  #include <stdio.h>

  int main()
  {
     int x;
     printf("Input x: ");
     scanf("%d", &x);
     if (x < 0)
     {
         printf("x is negative.\n");
     } else
     {
         printf("x is positive.\n");
     }
     return 0;
  }
  • Try to run this program.
  • Uh oh! Something's wrong here? Why?
  • It's because when you input 0 to x, computer will say: x is positive, which is wrong. Zero is supposedly neither positive nor negative.
  • So, how can we fix this program? Can we insert another if inside the existing if's curly braces?
  • Yes, you can. Let's see how.

 


Where to go

Chapter 2
News Page
Lesson 1 contents
Contacting Me


Roby Joehanes © 2001