(upbeat music)
- [Bill] A conditional statement allows you to run
one block of code or another based on a condition.
This is an example of a conditional statement in C++.
If the condition evaluates as true,
then the statement block will be executed.
The statement block may be one or more statements
enclosed in curly braces.
The if statement is the simplest form of a conditional.
When the condition is evaluated,
it is found to be either true or false.
If the condition is true,
then the enclosed statement block is executed
and flow continues after the conditional.
If the condition is false,
then the flow continues after the conditional
without executing the enclosed statement block.
An if statement may also have an else clause,
which looks like this in C++.
Notice that there is a separate statement block
for the else condition.
This block of statements is executed
if the condition is evaluated as false.
The if else construct works like this.
If the condition is true,
then the statement block is executed
and the flow continues after the conditional.
If the condition is false,
then the else statement block is executed
and flow continues after the conditional.
In either case, one and only one of the blocks of statements
will be executed.
Let's take a look at this in practice.
We're going to come down here
and create a simple conditional.
Take two ints, call them x and y.
And then we'll have a simple conditional,
if x is greater than y
and we'll put condition is true.
And when I build and run this,
you see it says condition is true
because x is indeed greater than y.
So in C++, logical values are represented as integers.
Zero is false and any non-zero value is true.
So if I just say printf
value is %d
and x is greater than y,
I'm evaluating this condition and printing the integer.
The integer resolved.
And so when I build and run,
you see it says the condition is true.
It says value is one.
And if I just change this greater than to a less than,
you'll see that it says value is zero.
This condition is still true
because I'm still testing if x is greater than y.
But if instead of this, I just say one, if one,
the condition is always true.
And if I say zero,
then it says here code will never be executed
because the condition is zero.
It's always false.
If I just put in a number here,
it'll be true because any number other than zero is true.
So the way this works is zero is false
and any other number is evaluated as true in C++.
So again, if I say x is greater than y
and I have an else clause here,
I can say puts condition is false.
Now when I build and run this, it's true.
But if I change this to the less than, now it's false.
It says condition is false.
Likewise, if I put a zero here, it says condition is false.
And if I put a one here, it says condition is true.
And now my else says it'll never be executed.
And the same thing happens if I put in any non-zero value.
So these conditionals,
the if and the else are inherited from C
and are exactly the same in both languages.
C++ provides a special multi-way conditional
using the switch statement.
This is a feature from the C language.
Let's take a look first at how this works.
You notice that I've declared four constants,
they're integers, and I call them iONE,
iTWO, iTHREE, and iFOUR,
and they have the values of one, two, three,
and four respectively.
And I've declared another integer called x
which has a value of three.
And now I have this switch statement
which has a number of different options.
You notice it has case clauses, case iONE, case iTWO,
and wherever x matches one of these constants,
that block of code will be executed
up until it hits the break statement.
And we'll talk more about break later on in this chapter.
A break is a branching statement that breaks out of a block.
So when it sees break,
it comes down here to the matching curly brace,
the one that matches the one at the top.
So there's all of this as a block
and the break will break out of the block
and continue execution after.
And then you notice there's also a default.
If x doesn't match any of the constants,
then default is executed
and all of these have to have a break.
Otherwise, the execution will fall through to the next one
and it'll execute both of them.
And so let's go ahead and run this, build and run.
You notice that it says three
and so that's because x is three
and it matches iTHREE
which is declared up here on line eight.
And so that says puts three, the string, T-H-R-E-E,
and then break.
And if I take this break out,
it'll print both three and four, watch,
because it falls through.
And so it's good to understand
that occasionally you might use it,
but for the most part when you see a switch statement,
you'll see breaks after each of the cases.
Switch can be very handy
in cases where you need to select a condition
from a list of possibilities.
The case clause requires a constant
so you'll need to take special care
to use the proper type for this purpose.
The multi-way switch control is inherited from C,
although there's different restrictions
on the case clause in C.
(upbeat music)