nestedif.c Part 2

#include <stdio.h>

int main()
{
   int x;
   printf("Input x: "); scanf("%d", &x);
   if (x >= 0)
   {
       printf("x is greater or equal than zero.\n");
       if (x % 2 == 0)
           printf("x is even.\n");
       else
           printf("x is odd.\n");
   } else
       printf("We won't consider negative values.\n");
   return 0;
}
  • Note also the omission of the curly braces. If the curly braces contain only one statement, you can safely omit the braces as shown in the example.
  • The curly braces after if (x >= 0) can't be removed because it contains two statements: a printf and an if.
  • Pay attention to the greater or equal sign (>=). It is NOT (=>).

 


Where to go

Chapter 2
News Page
Lesson 1 contents
Contacting Me


Roby Joehanes © 2001