If / Else

Much of programming is taking different courses of action based on certain conditions. In C we do this with an if/else statement

if (condition) {
  // Do if condition is true
} else {
  // Do if condition is false
}

The trailing else is optional.

What is Truth?

C has a pretty simple concept of truth. The value 0 is false. All other values are considered true.

Comparison Tests

<
Less Than
>
Greater Than
<=
Less than or equal
>=
Greater than or equal
==
Equality
!=
Inequality

Boolean Logical Tests

&&
AND
||
OR
!
Negation

Exercises

  1. Write a program that performs several if/else checks. Use each of the comparison tests.
  2. Write a program that performs an if/else check that a number is less than 10 and greater than 20. Use only a single if statement and the '&&' operator.
  3. Write a program that will determine if a number input by the user is small, medium or large. If the number is less than 10, it is small, if it is greather than 100, it is large.