#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;
}
|
- Here is one example of nested if. Here we test if x is greater or equal than
zero. Negative values of x is neglected.
- If x is greater or equal than zero, then a further test is conducted to test
whether x is even or odd.
- A number is even if it is divisible by 2. Thus, the expression (x % 2 == 0)
is to test whether the remainder of x divided by 2 is zero (i.e. x mod 2 is 0). Remember,
remainder or modulo in C/C++ or even Java is expressed with % symbol.
|