(upbeat music)
- Inputting and outputting is great,
but what if we wanted to make a decision based on
the input that a user gives?
There are two decision structures that we will go over.
The first is the If-Then,
the structure of the it is such that
if this condition is true, it will do the body of code.
If the condition is not true,
it will skip over the entire curly brackets and continue on.
If we look at this flow chart based on this IF structure,
we can say x is equal to three,
if x is greater than zero, because that is a true statement,
we would follow the right-hand side of this flow chart
and execute the code body.
If this was a false statement, we would go to the other side
and continue on completely skipping over this code body.
There are many variations to the If-Then,
we could have an if with a condition,
and then an else, the else is the optional catch-all
that will execute if the IF condition is not true.
If you have an IF statement,
only the if is required, the else is optional.
If your body of code to be executed is only one line,
you can also eliminate the curly brackets.
If you have more than one line to be executed
in the body of code,
you need curly brackets around it to keep them together.
So the flow chart for the If...else looks like this.
If this condition is true, x greater than zero,
I will go to the true side and execute the code body.
If this condition is false, I will come over
to the false side and do Code body 2.
The last variation is to include the else if.
You notice the else if is a hybrid look between the else,
and the if.
We still have our condition in parentheses,
we still have our word if,
and we still have the keyword else.
So we kind of have the best of both worlds.
So if condition one is true,
it will do the body of code at the top.
Once it's done executing that body of code,
it will find the closed curly bracket
for the entire structure and continue on.
If condition one is false,
the structure will go to the second else if,
and test condition two.
If condition two is false,
it will go down and test condition three.
If condition three is true,
it will execute the body of code under condition three
and exit out of the structure.
You can only find one true condition
per if else if structure.
So even if multiple conditions are true,
it can only execute one.
And it will pick the first one that it finds and exit out.
Let's take a look at the flow chart.
If x is greater than 10, if that is true,
I will do Code body 1.
If that is false, I will calm down
and test if x is greater than seven.
If that is true,
I will do Code body 2 and so on.
I can only have one true condition
per if else if structure.
So once it finds a true condition, it will exit out.
In code if we said int x is equal to three,
if x is greater than 10, x is greater than 10.
Otherwise I can say, if x is greater than seven,
I'm going to print x is greater than seven
but less than 10.
The only way to get to the second condition
is by failing at the first.
So the first condition has to be false
in order for the second condition to even be checked.
If I get to the third condition,
that means I can safely say x is greater than five
but less than seven,
because it's not greater than 10,
and it's not greater than seven,
but it is greater than five.
So if this condition holds true,
it will print x is greater than five but less than seven.
If none of these conditions are true,
the else will be the catch-all
and it will print x is less than five.
So back to our game of chips,
we have this variable called bool player1Turn,
and I've initialized it to true.
Then we have an array of playerNames.
So I can add an IF statement and say,
if player1Turn is true,
then I want to print Player 1's name.
If player1Turn is false,
that means its Player 2's turn,
and I want to print Player 2's name.
Let's give it a try.
So after I've determined
the total number of chips in my starting pile,
I'm going to say if player1Turn
is equal to true,
I want to print out player1Turn.
So if player1Turn is true,
I'm going to print the player at
the first position in my array.
Because player1Turn is of type Boolean,
it's going to evaluate to true or false anyway
therefore eliminating the need for me to compare it to true.
It's either going to be true or it's going to be false.
So I don't need to say,
if it's equal to true, I could just say, if player1Turn.
Now I can do the same thing for Player 2.
So now the player whose name's at position one
will be prompted to enter how many chips they would like.
And then I will give them the prompt
of how many chips they can take.
And then we will input chipsTaken
from the user.
Let's give it a try.
So I'm prompted for my name,
and now it prompts me with my name,
how many chips would you like?
Because my chip pile started at 19,
I can take up to nine chips on this turn.
If I enter five, my game does nothing.
(upbeat music)