Hello, and welcome. In this lecture, I want to talk about decision making,
specifically if statements.
So the c programming language provides a
general decision making capability in the form of an if statement.
We know what statements are up to this point.
So now we're just going to add a keyword in front of it and also put it inside parentheses.
So what it looks like is something like this.
If keyword and then inside the left and right parentheses
you have something called an expression.
We know what expressions are and the difference between statements.
That expression has to evaluate to true or false.
And then you have a program statement.
And so this is the most simple kind of if statement.
So if you say expression and then the program statement,
you could actually substitute that with some pseudocode
something like if it is not raining then I will go swimming.
And if you translate that pseudocode into c, pseudocode is like
not real syntax,
it's a way of describing something in code in the English language.
And so if we say
an if statement in c, all you would do is you would put the condition inside the parentheses, it is not raining.
And then after the if statement
either a single statement or multiple statements.
If you have multiple statements, you would put them inside curly braces.
But in this case,
we have one statement. So inside the body the if statement is I will go swimming.
So it has to not be raining in order to execute I will go swimming.
This is why it's in the control flow. You're not actually always executing I will go swimming.
It depends on the condition and if it's true or false.
So you may be skipping over code in this case. So you don't have a sequential process.
So the if statement is used to stipulate the execution of a
program statement based upon a specified condition, a decision making
capability. So you're only going to go swimming if it's not raining.
You're only going to execute the code in the body of the if statement if it's not raining.
The curly brackets are required for compound statements inside the if block.
So for example, if you were say if it's not raining, I will go swimming
or before you go swimming, you will put on your bathing suit,
you will get dressed and then you will go swimming.
Then you'd have to put those curly braces in there.
And again, we'll have specific examples to show that.
But the curly braces aren't required if you only have a single statement, they're optional.
You can provide them or not.
I usually provide the curly braces in all my if statements just for consistency.
Here's an example of a common if statement in the c programming language.
In this case, we're declaring two variables,one named score and one named big.
Score is 95, big as 90.
Here we have just a simple statement in the body
and it has no brackets. And basically, we're saying if score is greater than big,
score is 95. So if 95 is greater than 90,
it's a Boolean expression. It's going to evaluate to true in this case.
So what's going to be displayed to the screen is jackpot.
And then we have that same example except we use the curly braces
because we have more than one statement in the body of the if statement.
So in the bottom example, if score is greater than big,
we have a left curly brace to start the block of the if statement,
and we have an ending brace to end the block of the if statement.
That's referred to the body of the if statement.
And so in this case, we have two statements. We're going to first increment the score by 1,
and then we're going to display you win.
So notice the difference here, brackets versus non-brackets.
You can still put the brackets in a single statement, they're optional.
But if you have multiple statements, the brackets are required.
The left bracket signifies the starting of the block,
the right bracket signifies the ending of the block.
If statements are pretty simple when they're just a single lift
If keyword parentheses left and right, inside the parentheses in an expression.
And when if statements get more complicated, those expressions can become multiple expressions.
So you can start using some of the logical operators we learned about
in terms of we can use the and or the or.
So if score is greater than big and something else
is required.
So we can have multiple questions in a single if statement or we can break them down.
But this is a basic if statement example,
again syntax pretty basic,
you don't see any semicolons at the end of the if statement,
the only at the end of the printfs.
So you just have the ending and starting brackets.
Now you can also add an else clause to your if statement.
This is supported by c. So you can extend it a bit
with a small addition that allows you to have a lot more flexibility.
So for example, if we were to say if the rain today is worse
than the rain yesterday,
I will take my umbrella. Else if it's not raining
as bad as yesterday, I'm going to take my jacket.
So the else is saying what happens when
the question to the answer is false.
So else, I will take my jacket, then I will go to work.
So this is exactly the kind of decision making the if else statement provides.
Do something when it's true, also do something when it's false.
So you have the if, keyword and then inside the parentheses you have your Boolean expression,
something that evaluates to true or false.
Then you have your statement that gets executed statement one if it's true.
Else, you use the else keyword.
It's optional, you don't always need to provide the else. Only if you want to do something if
the Boolean expression is false, you say statement two.
And again in this case, we don't have any brackets inside
the body of the if block or the else block
because we only have single statements.
If we have more than one statement inside either one of those bodies,
we have to provide a bracket for starting and ending.
So here's an example of an else statement with an if.
In this case, we're going to determine if a number is even or odd.
So we have our main function that's where we enter.
And we create a couple variables, one is called number to test, one is called remainder.
They're both of type int.
We're declaring them on the same line by separating with commas.
We didn't initialize them yet.
On the next line of code, we're going to print out some output to ask the user to enter some information.
We then use the scanf function.
We're reading in an integer so we can do percent i or percent d.
And we're not reading a string or any words.
So we're going to put the ampersand in front of the variable where we're going to store the data.
So the user is going to enter something number to test.
And then we're going to also get the remainder of whatever they entered
by 2
because that will tell us whether or not something is even or odd. If you can
divide a number by 2 and the remainder is 0,
that means it's an even number. Otherwise, it's odd.
The important part of this program is the bottom where we're using the condition.
Up until this point, every single line has been executed.
But when we have an if statement and a condition inside the parentheses,
if the remainder equals equals zero
and you'll notice that you always have to use the equals equals for comparison,
you don't use equals. Remember, equals is for assignment,
assigning data to a value. Equals equals is for comparing numbers.
We're comparing a remainder variable to zero.
And if it's zero, we're going to print out even.
Else, the else keyword is on that next line, we're going to print out odd.
You'll notice there's no brackets in the if or the else because there's only single statements.
Let's run this and see how it works in code blocks.
Well, here's our program. It's described, as I just mentioned, the exact same way.
We're just going to print out even or odd. So let's go ahead and build this.
And we want to make sure we don't have any errors.
You have a couple extra line feeds here.
Now when we run this program, we're just going to basically be able to
have output for even or odd.
It's going to ask us the number to be tested. I'm going to enter 33, which is odd.
It's going to say the number is odd. So it's pretty simple program.
The important thing here is that you're not executing certain code.
You're either going to execute line 19, if the remainder is equal to 0.
If it's not, you're going to actually line 21. And the brackets again are optional.
You can put those in there,
and the IDE will do some automatic completion for you
which is pretty nice.
But again, I usually like to add them because they are easy
and it makes code even more readable.
It depends on your formatting style.
Some people like to have the bracket on the beginning of the else statement, like this.
Some like to have it on the other line, depends on how your format.
And actually, this can be done for you automatically in the IDE by formatting the code.
But this works the same way, and it's almost even more easier to read.
And if you ever have to do add more statements,
you easily add them and you don't have to worry about adding the brackets.
So I get in the habit of adding brackets.
Again, the other important thing here is this equals equals.
You can't do equals. If you do equals and run the program,
you would see there's a big difference. So a very simple if else statement,
very, very useful. You're only going to execute line 19 if the remainder is odd.
Otherwise, you're going to execute line 22.
Let's talk a bit more about an else if statement.
So you can also have an else. If so we've learned about if, we've learned about else.
You can handle additional complex decision making by adding an if statement to your else clause.
So it's like asking an additional question.
You're not actually going to execute something if the condition in the original if is false,
you're going to ask another question.
So you say if expression 1,
you have some code that's going to execute if it's true.
Otherwise,
Otherwise, you say if after it
which is perfectly legal. This is an if statement inside of an else statement.
Then you execute program two. Else, you execute something else.
So this extends the if statement from a two-valued logic decision to a
three-valued logic decision.
Here you're asking two questions.
And if the question to the first answer is true,
you execute program statement one.
Then you ask your second question, only if the first question was true
or if the first question was false.
And if that's true, then you actually some code else you execute something else.
So you have three branches here essentially,
three separate lines of code that may be executed or may not be executed.
Program statement 1, program statement 2 or program statement 3.
You can rewrite this code to use an else if.
Right now the if is on the separate line, but you can actually just do something like this, if expression one
do something else if.
You don't have to put it on a separate line. It can be a single line
and then you can have an else outside of that.
The only thing tricky about this is that else clause is going to get executed.
And this is the same thing. So this else clause is going to get executed
for all three.
So it's a little
confusing in the first example because it looks like the second else
is only being executed if the other if
is false.
But it's really being executed if the first expression is false as well.
If you would have brackets in there, you could have actually nest those if statements.
And the outer, the last else would only execute
based on the question of the second if answer.
We'll get into nesting when we talk about nested if statements and nested loops.
Nested if statements are just putting an if side and if,
and you have to be careful where your else
clause is because the else has to be corresponding to the last if statement.
Here's another example where we're going to actually try to implement the sign function.
We're going to return either negative 1 0 or 1.
So this is a three-tiered evaluation here.
What this program is doing in the main routine
is we're creating a couple variables of type int, number and sign.
We're having the user enter a number, and we're storing it in the number variable.
And if the number is less than 0, the sign is going to be equal to negative 1.
Else if the number is equal to equal to 0, it's going to be 0.
Otherwise, if both of those are false, its sign is going to be equal to 1, meaning it's positive.
So this is an example of how the else if can be very useful.
And you can have multiple else ifs. We only see one there, but we could have 100 if we wanted.
Else if number is equal to 2, else if number is equal to 3, else if number is equal to 4.
And you can also you see the brackets aren't used here but the brackets
will be used if you had multiple statements inside the body.
And when you use if statements, you should also
notice that you're going to be using a lot of logical operators
and comparison operators and relational operators.
So you're using the less than symbol here. Logical operators,
remember, are the ampersands. Two of them for and or,
and relational operators are where you're comparing values.
So when you're doing things like less than or less than equal or greater than.
Those are used quite a bit when you're using numbers in your if statement expressions.
When you use the ampersand ampersand, you're actually asking multiple questions.
If this is true and this is true or if this is true or.
It's a way of reducing other else if statements as well.
So you could rewrite this code right here if number is less than 0.
And also,
if you wanted to have the sign, you couldn't actually the way it is now.
But if you could put an ampersand ampersand in there and you could say if number is less than 0
and it's equal to 0,
then the sign is negative 1 and 0. I mean you can't do that specifically,
but you can see how you could add multiple questions here.
So we're going to have a specific challenge where we're going to write a program that uses if statements.
These are some good examples. You'll want to put these examples inside your IDE and see what happens.
But the else if is again a way to ask multiple questions.
Nested if else statement.
Nested if l statements means you can use one if or an else if
statement inside another if or an else if statement.
So that's why it's referred to as nesting.
You can use multiple things inside and you can keep doing that over and over and over.
There's no limit on the number of nesting that you haven't see.
So the syntax looks something like this.
If a Boolean expression is equal to true,
then inside that and when you do nesting, usually, you want to use the brackets.
Even though you don't have to for single statements because it makes it much more readable.
So if the Boolean expression 1 is true,
then inside there you say if Boolean expression 2 is true.
You would not actually execute if Boolean expression 2
unless Boolean expression 1 was true.
And then inside the body of the Boolean expression 2, you would execute code if that's true.
So an example of a nested if statement would look something like this.
If game is over equals equals 0 and then inside there if player to move equals equals you,
then you have a print.
The key here noticing there's no brackets,
and it's a little harder to follow. The only way you follow this is through the indentation
which is another thing you want to do when you write if statements in blocks of code.
You always want to indent the block because it makes it easier to read.
Imagine if this did not have
the indentation, how harder it would be to match up the else and the ifs.
So use indentation but also probably use brackets, brackets help to make it even more readable.
And remember, readability is a big deal because readability helps you find and fix your errors much quicker.
So in this case, we're only going to execute your move
printed out to the screen if player to move is equal to u and game over is equal to 0.
We're going to only execute my move displayed to the screen
if game is equal to 0 but player to move is not equal to u.
And then lastly, we're going to only execute the game is over
if the game is not equal to 0.
So we have three conditions here
for printing out three different statements.
And you could actually write if the game is over
equals equals 0 and ampersand ampersand, the player to move equals u.
But the problem with that would be your else my move
might not be right because you still have to compare player to move.
So there are times when you can use the ampersands to simplify code,
there are times when you cannot. In this case, you can't because the
first else is inside the nested if.
So nesting is quite applicable, and you'll use it, so
get familiar with it. Again, the syntax for the if statement is very straightforward.
The Boolean expression inside and then use brackets for single statements.
Otherwise, you use brackets.
You don't use brackets for single states. Otherwise, use brackets.
There's also a third
type of way to ask questions in your program. This is called the conditional operator,
and we talked about when we talked about operators, but it
needs a little bit more understanding.
So the conditional operator is a very unique operator.
It's unlike all the other operators in c.
Most of the operators in c are either unary or binary.
Unary means that the operators take a single argument or a single operand.
Binary means they have operands on both sides.
So the plus operator is a binary operator because you do 5 plus 3,
two operands.
The negative is a unary operator because you say like negative 3.
Well, there's a third option, there's a ternary operator, which takes three operands.
And this is the conditional operator.
You have two symbols that are used to denote this operator, this conditional operator.
It's the question mark and the colon.
And it really is equivalent to an if else statement.
The way that it looks is the first operand is placed before the question mark,
that would be the condition, the Boolean expression in the earlier examples.
And then the second thing between the question mark and the colon would be
basically the body of the if statement if the if statement was true.
And then the colon is the third, and
that would be if the condition evaluates to false,
you would be executing that body of code, so that's like the body of the else statement.
So this turning operator is nothing more than an if else statement just shorthand notation,
you can write it much quicker.
The difference here also is there's no brackets, so you can only have single expressions
or single statements inside the body of the after the question mark or after the colon.
So if the conditional operator evaluates to one of two expressions
depending on whether the logical expression those to,
depending on whether condition is true.
If the condition is false, expression 2 is evaluated.
So again, it behaves just like an if else statement.
Also notice how the operator is arranged in relation to the operands.
The question mark character follows the logic expression condition.
The right of the question mark are the two operands, expression one and expression two.
They represent the choices.
The value that results from the operation will be the value of the expression one
if the condition evaluates to true.
And then the value of expression two if it's false. So exactly what I said if else.
Let's look at an example. So looks funny to read,
but what this is saying is if x equals the y
is greater than 7,
so what this is doing? Is it's doing an assignment?
X equals y. So if x is 5 and y is 9, it's going to say if x is greater than 9,
it's going to then set
the number to 25 x.Otherwise, it's going to set x to 50.
So it's a little confusing to read it first.
What this results in is x being set to 25 if y is greater than 7 or to 50 otherwise.
So it's an easy way to do an assignment of the expression.
X equals y is greater than 7.
If y is greater than 7, x equals 25, otherwise 50.
That's why the x equals there is a porn.
So the ternary operator and the conditions are on the right hand side of the assignment,
and this is typically how you use it. You say x equals, the result of that
ternary operator.
So the entire ternary operator is y greater than 7. So it's just saying if y is greater than 7,
25, otherwise 50. And then you assign that to x.
And this is typically how you use it. It's the same as saying
using an if statement. If y is greater than 7, x equals 25 else x equals 50.
But it's much less code.
The limitation of this conditional operator
is you can only have two variables written using the conditional operator,
a maximum minimum.
You can't just do a ton of them. So it's a little limiting. It's usually used for assignment.
So quite a long lecture, a lot of different examples.
This will be more applicable when we do our challenge.
But if statements are going to be used ton,
and they're really, really easy to learn. Before you know it, you're going to be
knowing the syntax like the back of your hand,
parentheses, brackets, if else keywords.
And then occasionally, you're going to use this conditional operator.
Again, that's maybe as you become more advanced in c, you'll start realizing wow,
this if statement's taken me a long time to write,
maybe I can write in a single line using the conditional operator
with my question mark and my colon.
Again, very useful for assigning variables based on conditions. Thank you.