VIPUL SHEKHAWAT: Hi.
In this video I'll be introducing you to one of most
fundamental concepts in logic and
programming, the Boolean value.
If you're curious about the name, Boolean values and
conditions are named after George Boole, a 19th century
mathematician who pioneered what is now called Boolean
logic, which is based by grouping and
comparing Boolean values.
>> So what is a Boolean value?
A Boolean value is a variable that just has two possible
conditions, true and false.
You can think of it as a light switch.
It can be either on or off, true or false.
Similarly, binary numbers can be either one or zero, which
is analogous to the same thing, true or false.
Simple, right?
The concept of a Boolean variable is easy to
understand, but the ways in which you can manipulate and
combine them allows for much of greater complexity.
In addition to the two fundamental Boolean values,
there are many Boolean operators that can combine two
Boolean values into a single one.
>> Two of the most basic, but most important
operators, are AND and OR.
The AND operator results in a value of true only if both of
the values it takes are true, so false AND true is false.
Likewise, false AND false is false.
Only true AND true equals true.
The OR operator results in the value of true if either of the
values it takes are true.
So false OR false is false, but true OR false is true, and
true OR true is also true.
The NOT operator simply takes a Boolean variable and gives
you the opposite of it.
So true becomes false and false becomes true.
If you put the whole thing together, variables with
operators, you create a Boolean expression.
>> Now let's look at an example of nesting these Boolean
expressions.
Do you remember the order of operations?
As with numbers, Boolean expressions can be grouped by
using parentheses.
So there are basically three expressions here, NOT z, y OR
NOT z, and x AND y OR NOT z.
We can figure out the values of these by looking at the
inside and working our way out.
So let's suppose x is true, y is true, and z is also true.
What would NOT z evaluate to?
Since we start with true, NOT z would simply be false.
So now we have false OR y.
If you look on top, you can see that y is true, y OR false
would still just be true.
Lastly we, have x AND true.
So what's x AND true?
x is true and true is also true, so this whole thing
evaluates to true.
>> Next, let's look at how these Boolean expressions can
actually be used in a programming language.
In C, the syntax for Boolean operations is a bit different
from the words and, or, and not.
Let's cover the syntax.
To use the AND operator, we write a double ampersand.
The OR operator is a double pipe line character.
This is the straight vertical line, which you can probably
find above the Enter or Return keys on your keyboard.
And the NOT operator is simply an exclamation mark.
So to rewrite the expression we had before, we would just
write this x && y || !z.
That's just taking exactly what we had before and turning
it into C syntax.
>> Now that we've translated our Boolean expression into code,
how do we actually use it?
Let's say we have some code that should only execute if a
certain expression is true.
For this purpose, pretty much all programming languages
support the if condition.
Let's say we have a Boolean variable, x, and we want some
code to execute only if x is true.
We would simply write the word if, put parentheses, and put
the Boolean expression within those parentheses.
After that, we wrap the code we want to
execute in curly braces.
What if there's some code you'd like to
execute if x is not true?
Simply write the word else after the if statement, wrap
the other code in curly braces, and then that code will
execute if x is not true.
>> Another useful language construct is else if.
Suppose there are two Booleans you would like to consider,
let's call them x and y.
We declare these variables to be true and false.
If x and y are true, you execute the first block of
code within those curly braces.
Else if x or y are true, you execute the next block of
code, and else you execute the last block of code.
Working with Boolean values like this is useful, but
you're really only limited to a few conditions.
Booleans can become much more powerful when you introduce
comparisons.
These are ways to compare values that are
not originally Boolean.
To see if two values are the same, you can use equals
equals, which is true if they're equal and false if
they are not.
Other common comparisons are less than, greater than, less
than or equal to, and greater than or equal to.
>> Everything I've covered so far has been pretty abstract, so
let's introduce these comparisons in one last
concrete example.
Suppose there are two variables,
temperature and isHungry.
Temperature is a floating point number, so it can have
decimal places.
You're programming a very simple application which tells
someone what to eat depending on the temperature.
If you're hungry, AND AND the temperature is greater than or
equal to 100, you can print eat ice cream.
Else if you're hungry AND AND the temperature is less than
or equal to zero, you can printf("eat spicy food").
Lastly, if you're not hungry at all, you can print "don't
eat anything."
>> I am Vipul Shekhawat, and this is CS50.