Conditionals
Contents
if
statements
A conditional is a structure that allows use to branch the flow of execution based on something being true or not. The simplest conditional structure in C++ is the if
statement. Lets see how to use it; say we want to read a number in from the user and, if the number is less than 10, we'll print out a message. The code might look like this:
The general structure is:
We call this thing in the parentheses an expression. An expression always evaluates to a boolean value, so either true or false. An expression can be:
- a boolean:
true
,false
- a comparison of two variables or values, using these test operators:
==
,>=
,>
,<=
,<
, and!=
; works between numeric and string types (and any type that has those operators defined) - two or more expressions joined with logic operators:
&&
and||
- a single expression preceded by the negation operator
!
Everything within the curly braces immediately following an if
statement is the body of the conditional. If the expression evaluates to true, then the code inside of the curly braces will executed; if the expression evaluates to false, then the code inside the curly braces is skipped and will not be executed.
If the body consists of a single statement, then we don't need to include the curly braces. For example, we can rewrite \ref as follows:
Be careful with this, however—sometimes programmers start off with no braces because there's only one statement in the body of the conditional, but then they add another statement and forget to add in the curly braces. This can cause hard-to-spot errors!
else
statements
if
statements are good, but it would be nice to say something like: "if x is true, do this, otherwise, do that". Luckily, C++ has a built in statement called else
, which goes along with if
. Here's the structure:
The else must directly follow an if
statements (or an else if
statement, which we will learn about next). The body of the else will execute if the the body of the if is not executed. Exactly one of them will execute: either the if
body or the else
body. Similar to if
, we do not need the curly braces if there is only a single statement. Here is an updated version of \ref that uses an else
to print out an alternative message when the number provided by the user is 10 or greater:
else
else if
statements
C++ has another structure that can be used with if
, called else if
. Use this when you want to test another expression when the previous expression evaluates to false. Here's the general structure:
The final else
is optional. In addition, you may have as many else if
statements after the initial if
.
It's important to knot that when an else
statement is present, exactly one branch will be executed—the others will be ignored. If no else
statement is present, then it is possible that none of the branches will be executed.
switch
statements
For case-based conditions, you can also use a switch
statement. These can be used with int
and char
types. Here's the structure:
Here, variable
is the int
or char
type to be evaluated. Each case covers one of the possibilities, where val1
is the value you want to test against. You need a break
statement at the end of each case, or else the code within the next case will be evaluate (sometimes you want that, but usually not). Here's an example where we use a switch statement to determine what choice the user just entered:
Complex expressions
We talked about using complex expression in \ref. It's helpful to see some examples. Lets suppose that we want a program that asks the user how happy they are on a scale of 1–10. If we want only numbers in that range, then we had better check to make sure the user did that correctly. To check it, we need to make sure the number they provide is greater than or equal to 1, and also less then or equal to 10. That's really two subexpressions: the first compares the input to 1, the second compares the input to 10. We want both expressions to evaluate to true. So, we can use the logical "and" operator, &&
, between the two sub expressions. Here's what the program looks like:
Now, in this example, our if
statement checks if it's in the valid range. What we really want is to check if it's not in the valid range, and if so, exit the program. If the rating is valid, then the code that exits the program will be skipped and we can carry on doing whatever we want with the valid range. To do this, we have a couple of options...
Option 1. We can change the if
statement so that we check if the rating is less than 1 or is greater than 10. This requires the logical "or" operator: ||
. Here's what option 1 look like:
!
. This is a unary operator and is placed just before the expression we want to negate.
You can have much more complex expressions if you'd like. You can also directly use booleans. Something to keep in mind as you build more and more complex expressions is that there is an order of precedence among operators. Take a look at the bottom of this page for more information about operator precedence.
(Back to top)