- [Instructor] 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 is 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.
Here's a working copy of working.cpp
from chapter two of the exercise files.
And 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 nonzero 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 result.
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 your 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, says condition is true,
and now my else says it'll never be executed.
And the same thing happens
if I put in any nonzero value.
So these conditionals, the if and the else
are inherited from C
and are exactly the same in both languages.
There is one more type of conditional,
a multi way switch
that we'll cover in the next lesson.