Aside from computing simple things like an average, programs aren’t very useful if they just follow a fixed set of steps. By using conditionals the operations performed by a program can be determined at runtime instead of when the program is compiled. For example, here we receive a signed long from the user and print a different statement depending on its value.

The if statement in the example first evaluates the expression within (), and then runs the code between {} if and only if that expression evaluated (i.e was equal) to true. If it didn’t, it checks the expression in the else if—of which there can be any number, including none at all—before finally resorting to the optional else code if that wasn’t true either. Note that only the first true if/else if/else statement will run, even if one after it is also true.

The expressions inside the () must be any expression that evaluates to a bool (true/false) value, which includes any mathematical comparison using <, >, <=, >=, ==, and != for less than, greater than, less than or equal to, greater than or equal to, equal to, and not equal to, respectively. Make sure you don’t use = instead of ==, the first is an assignment and not a comparison!

If the body of your conditional (the part between {}) is only a single statement (there’s only one ;) then you can omit the {} entirely. Hence the example is equivalent to

The condition and body don’t have to be on the same line.

### Logical Operators

Just like how numbers can be combined using arithmetic operators such as + and -, comparisons can be combined using the logical and and or operators && and ||. (These do not have compound assignment operators.)

When combining muliple logical operators together it’s important to insert () in the correct places as the order of evaluation may not be what you expect. For example, is 10 < 11 && 3 > 5 || 6 > 10 true or false? Try it out!

There’s also a unary negation operator ! which turns true to false and false to true. This is evaluated before other logical operators so make sure to insert parentheses around the expression you’re negating.

### The bool Type

To be consistent with C, where the bool type doesn’t exist as a built in type (although it’s available in stdbool.h from C99, if you were curious), 0 is treated as false and any other value is treated as true. (In the opposite direction true is treated as 1 and false as 0.) This is achieved via type conversion and promotion respectively.

Try not to abuse this as it complicates code, but it’s useful in general for checking for non-zero values which often occur when something hasn’t occured correctly. (Remember that we return a value of 0 at the end of the program to tell the operating system that everything went OK?)

### Switch Statement

The switch statement is a useful replacement for a long if-else construct when you are checking for different outcomes of a single expression. Consider for example this simple conditional

This can be equivalently written using a switch statement as

Inside the () is the expression whose value we want to check; it needn’t be a variable—any expression such as (foo+10) / 2 will do—although it must evaluate to an int or something easily convertable to one (which includes all the types we’ve covered so far).

Each case x: is then a check for the given value (x must be an int or char), and the code after the relevant case is run by the program. Unlike an if statement there are (usually) no {} and instead the code is run line by line until a break statement is found, in which case everything else is ignored and the program continues. We use this in the example to run the same piece of code if foo is either 1 or 2.

Finally there’s the default statement, which is run if none of the case values are met, a lot like the else part of a conditional. Unlike else however, default does not need to come last.

Here a value of 1 or 3 will print "World!", a value of 2 will print "Bye", and a value of 0 or anything else will print "Hello World!". Note that the last break statement is omitted as it isn’t necessary; there are no other case statements after it.

### Exercise

Write a program that accepts a two integers less than 10 from standard input and then outputs whether the largest is prime or not. (A prime number has no factors other than itself and 1.)

In the next section we’ll see a much easier way to do this, but you can use if statements here because the input is so small.

Solution