6. "If Statements" 2025 Winter APS105 Section 3 (University of Toronto)
Watch on YouTubeVideo summary
The lecture introduces "if statements" as a fundamental mechanism to move beyond writing boring, repetitive programs by enabling conditional execution of code. In C programming, an if statement allows the computer to evaluate a Boolean expression—resulting in either true (1) or false (0)—and execute specific blocks of code only when that condition is met. The syntax requires placing this logical test inside parentheses followed by curly brackets containing the statements to run; crucially, these internal commands are skipped entirely if the initial expression evaluates to false. To construct meaningful conditions, the instructor explains relational operators like equal-to-equal (==), not-equal (!=), less-than (<), and greater-than-or-equal (>=), noting that C uses double equals for comparison rather than a single one because the latter is reserved for variable assignment. These operators work with numbers to produce Boolean results, adhering to standard precedence rules where mathematical operations are performed before relational checks, though using parentheses can simplify understanding without needing to memorize complex order-of-operations hierarchies.
Building on these basics, the lesson explores logical operators such as OR (||), AND (&&), and NOT (!) which combine multiple Boolean values into more sophisticated conditions. A significant portion of the lecture is dedicated to a practical example where students write a program to determine if an input number is even or odd using the modulo operator (%). The instructor highlights a subtle but critical "gotcha" in C regarding negative numbers: while checking for divisibility by zero works perfectly regardless of sign, relying on non-zero remainders (like 1 or -1) can lead to bugs because the modulo operation with negatives returns a negative result. Consequently, the best practice is always to check if the remainder equals zero rather than trying to account for specific positive and negative non-zero values separately. Additionally, the lecture demonstrates implicit type conversion rules where any non-zero number converts to true (1), while only 0.0 explicitly represents false in Boolean contexts, a nuance that can cause confusion when dealing with floating-point literals like 0.4 which incorrectly evaluates as true if not handled carefully.
The video concludes by addressing the "dangling else" problem, where an ambiguous syntax error occurs when multiple if statements are followed by a single else without proper grouping; in C, this lone else always attaches to the nearest preceding if rather than the outermost one, potentially causing logic errors that do not exist in languages like Python. To resolve this ambiguity and ensure mutually exclusive branches run correctly, programmers should use "else if" chains or explicitly group statements with curly brackets. The final segment introduces a simple gambling game where users bet on whether a simulated die roll is odd or even, utilizing the `rand()` function to generate random numbers between one and six. To make the randomness unpredictable across different runs of the program, the instructor explains how to seed the random number generator using the current time via `srand(time(NULL))`, setting the stage for future lessons on creating loops that allow users to play multiple rounds without restarting the application.
Read the full video transcript
[Music]
alrighty welcome back to 105 thank you
for joining me again today so today we
continue on our programming Journey with
something called if
statements why do we need something like
f statements well it's because right now
the programs we write are very boring
because computers are very stupid right
right now we run our program it always
does the same thing there is no
variation whatsoever to it so when you
run your program starts executing at
Main and it runs line by line the input
might be different but that's about it
for right now and at the end of today's
lecture we're going to learn how to
actually write useful programs
so the whole reason computers can
actually do something special is because
there is a way to conditionally run code
and if you do this enough times your
program actually appears smart so in C
it's called an if statement and it uses
a Boolean so we saw that type of Boolean
is just either true one or false zero so
the syntax of an if statement or the
rules for writing it in C is if and then
parentheses and then an expression
should go in there and then at the end
it's a statement so it should be some
code that actually does something
however you should always write it like
if expression and then curly braak
brackets and statements so these
statements will only run if this
expression is true and we will see that
is a bit and just remember and
expression is just a combination of
operands and operators we're going to
see some new ones today that operate on
booleans or produce
booleans so the statements inside the
curly brackets after the if only run if
that expression is actually true so c
will compute the result of that
expression and convert it to a Boolean
so remember zero is false and one is
true if the expression is true then we
will run these
statements all of them between the curly
brackets after the if and after those I
run whatever the next statement is here
after the curly brackets if the
expression is false we immediately just
skip everything in the curly brackets
after the if and start executing right
after the curly brackets of the if so we
don't actually run those statements if
that expression is
false so the expression in an if
statement again will convert it to a
Boolean there's more operators that
result in a Boolean you probably don't
want a number to be implicitly converted
but with a Boolean we'll see some
examples of that but you should use
something called a rational operator so
the r or sorry relational the relational
operators work with numbers and produce
a Boolean so they are the following so
in C if I write equal equal then that's
equivalent to just a single equal sign
in math and say whether the right hand
side is equal to the left hand side the
reason we have to use equal equal in C
instead of just a single equal statement
like in math is because well we already
used the equal statement in C and that
was to assign a value to a variable so
we can't use that and in fact one of the
biggest programming errors you will
probably encounter that might be a bit
confusing is accidentally using equal
when you should use equal equal but
nowadays your compiler will try and give
you a warning to say that hey you may be
doing something you shouldn't be doing
so if I wanted not equals well I can't
just write an equal sign with a slash on
my keyboard I have to write it come up
with a different symbol in C if I want
to write not equal that is just
exclamation point equal so that means
not equal in math so that will return
true if the leftand side does not equal
the right hand side and then again
there's less than meaning less than that
is something you can actually type and
then if we wanted less than or equal to
then it's just a less than sign with an
equal after that so that means you know
less than or equal to in math and then
similarly for greater than it's just you
know the other angle bracket in math and
if we want greater than or equal to it
is the same idea we just put an equal
character after it so some examples in C
so this would be my expression to the
left of the uh the yellow arrow here and
the result the final result of it to the
right of the arrow so if I ask C is 2
equal to two that would be 2 equal equal
two and in this case that would be true
two indeed is equal to two so C is going
to give me back a one signifying true if
I ask hey is three less than two the
answer to that is no so c will give me
the answer its answer or representation
of no which is zero or false and the way
I still remember the lesson sign
everyone in grade school like they told
you that think of that as an alligator
and the alligator wants to eat the
bigger thing
yeah I I have a PhD I still remember it
like that so don't worry if you can't if
you still have to remember that it never
really goes away so yeah this the
alligator would rather eat the three not
the two so it is false and then
similarly if I asked hey is three
greater than or equal to three the
answer to that is yes they're in fact
equal to each other so the result would
be one or
true all right so so the relational
operators follow similar type rules to
normal like addition subtraction all
that if both operands are an INT it does
the operator using integers if at least
one operand is a double the other gets
implicitly converted to a double if it's
not one already and the operator uses
like the real numbers so real numbers
are numbers with a decimal point but the
result of either case is still a Boolean
it doesn't matter whether you do it on
integers or doubles the final result is
always going to be a
Boolean so for example if I ask if the
integer 2 is equal to the double
2.0 well the answer is going to be true
but it's going to do a bit of implicit
type conversion so it's going to convert
this two as an integer to a double so
it' be 2.0 and then 2.0 is equal to 2.0
so we get a one or true
all
right so there's also some operators
that actually operate with booleans so
these operators are called logical
operators and all operand are booleans
and the result is a Boolean so it's more
of a way to tie together several
booleans to come up with a more
complicated condition so The Logical
operators are the following so two bars
means logical or so again they're B
operators so they need an argument on
the left and the right and the rule for
logical or is if either is one the
result is zero otherwise or sorry if
either is one the result is one
otherwise if both of them are false then
the final result is
false the two ends mean logical and so
it's only true if both the left and the
right hand side are true otherwise it's
zero and then just an exclam sign it's a
unary operator and all that does is a
logical knot so it flips a value so not
true it's false not false is
true so here's some examples with that
so again both the left and the right
hand side have to be
booleans it's up to you you can either
use one or zero or you can if you
include STD
b.h like we saw in lecture two you can
use true and false just to represent uh
zero or one if you want so in this case
if I do zero or zero well the result of
that is zero because neither one of
those are a one or true if I do one or
zero that's how we would read it then
the result of that is a one because at
least one of them is a one if I do zero
and one well both of them are not ones
so the final result is a zero and
finally if I do one and one then the
final result is a one because both sides
are true and then similarly the not will
just flip it so not one is zero and not
zero is one so questions about
that all right after this we can
actually start almost writing funner
programs so again rules for converting
numbers to booleans might be a bit
weird uh let's see what's our example
so might be a bit strange so if C
requires a Boolean it will like if it
wants a double it will implicitly cast
your value and how it works is as a
general rule if the number represents
zero it gets converted to zero otherwise
it gets converted to a one so what would
that look
like
so that might give us some weird things
oh actually no I should not show that
yet sorry we'll get back to that so just
remember that we'll see an example of
how that gets weird in a little
bit
so these operators follow precence rules
as before again you don't really need to
memorize this just know where they kind
of fit in so all of the uh relational
operators that operate on number have a
higher precedence than all of the
assignment or than all of the sorry have
a lower precence than the math operators
so they're done after them so all of the
math operations will be done first like
multiplication division mod addition
subtraction so all of them have a lower
precedence so they will be done after
that
so all you really need to know is
anything operating on booleans will have
a lower present so they'll be done last
so these are the relational operators
and these are the logical operators here
so don't have to worry about them if you
don't want to remember these rules
easiest thing to do is to just put
brackets around whatever you want to be
done first in C and that way you don't
have to remember these rules but
typically your log your statements or
your expression should be fairly small
so you shouldn't have to worry about
these rules otherwise you're writing too
much
code so let's write a fun little program
so now we can write a program that
behaves slightly differently now
depending on what the user inputs so
maybe I want to write a little program
that actually tells me whether or not
the uh the number a user inputs is even
or not so I can Define main like before
I can nicely ask the user to input an
integer create a variable for it called
num just in case I initialize it to zero
and then of of course if we want input
from the user from the terminal we have
to use scan F and we want to read an
integer so we use the format specifier D
and we want to assign or have C assign
the value the user inputs into our
variable called num so we have to give
it the address of and then after that
the number is set so now I can use my
new handy dandy if statement to check if
the number is even and one trick you
will see over and over again is if you
want to check if something is even
easiest thing to do is to use the modul
operator so if I do num mod 2
everything's an INT that will tell me
the remainder of dividing the number by
two if the remainder is zero that means
it's even right so it cleanly divides
into two otherwise it does not in this
case it should either return zero or one
and in this case I just want to print
off something if that number is even
oops so if I go ahead and run my program
now it's called is even it will ask me
to input an integer if I type something
like I don't know five nothing happens I
don't see any output because 5 mod 2 is
equal to 1 so 5 mod 2 does not is equal
to one which does not equal zero so this
code will not run in the if statement
and it will just skip here and then do
return exit success and exit my
program but if I run it again give it a
different input maybe I say six this
time it will say the number is even and
now my program behaves slightly
differently depending on what I input so
any questions about that little program
there neat all
right everyone's
good all right so here's the code again
in the slides just so you have it and
it's also in the examples repository you
have access
to so here is kind of a diagram if you
want to think about it that way I can
actually break each statement and like
connect them all together to see which
is going to run for an if statement so
you can actually get rid of the if
statement itself and just kind of have a
flow diagram as to what's going on so
whenever I run my program it it's always
going to do print F input an integer
declare a variable called num and do
scan F and then it will always evaluate
whatever is in the if statement so
whatever is in the parentheses so in my
case oops what was in the parentheses
was num mod 2 equal equal Z so it will
evaluate that and now it has a choice we
also call this a branch in programming
so if that expression is true it's going
to execute the statements inside of the
curly brackets with the if which in this
case is just print F the number is even
if it is false it's going to skip to the
end and just skip over that statement so
if the result of this so that's why
there's two lines coming out of the
result of this expression if it's true
it will print F the numberers even and
then go to the end of the curly bracket
which just does return exit success and
if it evaluates the false it just
directly goes to return exit success
does not pass go does not collect
$200 so everyone okay with that as
another way to represent what happens
with an if statement so it's basically a
way to skip over some code or jump
forward a little bit so we'll get into
the next lecture we'll figure out how to
jump backwards and then that's as
powerful as programs get it's just more
understanding
problem solving at that point so we've
got one piece of the real programming
puzzle so other things we can do is well
instead of just skipping over some code
if the expression evaluates to false
maybe I want to run something different
if that expression is false so another
valid syntax of an if statement is I can
do something like if expression
statement and then have something called
an else statement so in that case if
it's false it will run the statements in
the else Branch so you should always
write it again with curly brackets so I
should write if expression curly
brackets and then some statements that
will only run if that expression is true
and now with else I do else and then
some curly brackets and then some more
statements and these statements will
only run if that expression is false so
I can never run both of those statements
at once it's either one Branch or the
other
so again the statements between the
curly brackets after the else again only
run if that expression is false so now I
can start writing some more neat
programs so maybe I can write a program
now that actually tells me whether or
not the number is odd or even so I can
just take what I wrote before that told
me whether the number is even and now
well
I can write an else branch that will
print off that the number is odd if this
expression is false right if that
expression is false then it will
immediately fall through and print the
or start executing all the statements in
the else part of the if statement so
should print this number as odd and then
return zero in this case I could write
exit success again but by this point I
kind of know that I Sav myself some
typing because I know that zero means
okay so if I run this now it's just
called odd or
even now like before if I type something
like four it should immediately print
off to me that this number is even which
is
great or if I run it again now and type
the number five now I see this number is
odd and it will only go into one branch
or the other so it will never print off
the number is even and the number is odd
it's only one or the other so any
questions about
that all right well I'll come up with a
question to you so let's
say I wanted to write my program a
little bit
differently so maybe I thought about it
in a way where maybe I want this if
statement to be true if the number is
odd and if it's and then kind of
reorganize my if statement so if I do
something like this so I'll change it to
check if the result of the module is
equal to one and if it's equal to one
then I'll say the number is odd
otherwise if it's false I'll assume that
the number is even so should this do the
exact same thing as I have written
before
we got some head nodding I just kind of
flipped it on its head right so instead
of checking if the number in that
expression is even I'm now checking if
it's odd so it should be true only if
it's odd then I print off the numberers
odd if that is true otherwise I print
this as even so everyone agreed this
should do the exact same thing I just
decided to write it a bit differently
right all right so this is also why
programming is hard
so if I compile it and then I run
it uh build odd or
even then if I input the number four
that should still be even right so four
says this is even five should say this
is odd right seems to do the exact same
thing yeah so my question is like we f
it around right Y and then question is
what if what if we don't equ what just
say if
n that do something so yes one question
is what happens if I just do something
like
this so if I do something like this this
will work for the even case so what this
will do is numb mod 2 so the result of
that is an integer right which should be
either zero or one and in which case um
an if statement needs a Boolean right
but zero is false and one is true so I
don't actually have to write anything
else so this will return zero if it is a
even number which would be false so it's
set up properly but I'm kind of relying
on conversion in this case right so if I
do this it should still
work four yeah so it still works in this
case but I'm relying on conversion there
so instead
again there's lots of ways to write
programs so maybe I do something like
that all right
so yeah what's the difference between
build
and so the question is what's the
difference between doing build slash and
do slash so do slash means the
executable is in the same directory
you're currently in and build slash
means it's in a folder called build so
it just
happens in this rep repository I always
put in a folder called build I think in
in your code space for the lab it's
called Mason Das build but same idea it
just goes to a different folder it's
just where that compiled executable
is all right so you guys all said that
this is like an equivalent program turns
out this is actually wrong and it
outputs wrong number or it outputs the
wrong result while the other one was
always correct hm anyone want to Hazard
to guess why this program is now
wrong yeah does zer does it have
something to do with
zero so Z maybe Zer mod 2 screws it up
so that's a good guess let's enter a
number zero is even that's fine so zero
seems to
work
yeah exactly remember all those weird
stupid mod rules that I told you about
before and how they're really really
weird especially with negative numbers
so this works perfectly if your input is
always uh if your input is always
positive but
now if I do something like I don't know
negative
uh7 so
ne7 everyone would agree with me that's
an odd number right
the number is even uhoh our first real
bug why did that
happen
yeah yeah so in this case because it's a
negative number -7 mod 2 is actually
negative 1 not one so just because of
those stupid rules of C so this is
actually a fairly subtle problem that
doesn't appear if I do this so turns out
that if I wanted to actually check if
the number was odd well I might get a
remainder
of one or negative one so if I wanted to
properly do this and I didn't want to
flip it on its head I would have to use
one of those or statements because the
result of the mod could either be one or
negative 1 so if I wanted to write that
out I would have to write the mod
statement again and
then so now this will be true if num mod
2 is equal to one or again it just has
to be one of these is true for the whole
thing to be true or it's equal to
negative one yeah is there a way to make
it it to
abute yeah so you could also do an
absolute value if you want but that's
another function you probably have to
look up in the math library or something
else else like that so if I do that
change now -7 correctly says the number
is odd so just as a word of caution if
you want to check if a number is even
don't try and explicitly check if it is
odd always just mod compared to zero
because zero doesn't matter right
doesn't matter if it's negative doesn't
matter if it's positive as long as the
remainder is zero there's no thankfully
there's no negative zero so I would
submit to you that this version that we
wrote first is probably the better
version because we don't even have to
worry about negative numbers it just
works for negative numbers right so if I
compile that now and run it and do -7 it
just immediately says the number is odd
without any problems because well the
result of an odd number is going to be
either one or negative 1 it will never
be zero so I don't have to worry about
it so sometimes it's better than
some ways to write programs are better
than other others so if you're checking
an even number you should probably
always compare it to zero or if you're
checking if something is divisible by a
number always check if just the
remainder is zero and save yourself a
bit of a
headache that was a fun bug all right
any questions about that yeah um about
your V code why is that blinking why is
what blinking text curs how you do that
oh my text cursor is blinking
because I have a different input method
and like keyboard shortcuts and some of
that so that's just from that um if
you've heard of Vim before I have Vim
key bindings
here yeah all right so just to
illustrate this again the logic for that
when we ran our previous program for
even or odd again when we run main these
statements will always run in order it
will always ask us to input an integer
always declare num always get input from
the user and then always evaluate num
mod 2 equal equal to zero but now
because we have an else statement we
might actually run code if or we might
actually run different code if the
result is false so if it's true we print
the numberers even then go to success
and if it's false we print the numbers
odd and we go meet up at return AIDS as
success so we always meet up at the same
place so questions about that that's how
to visualize
it all right
cool all right so before we get into
that example so another weird gotcha
here so here's something about the
implicit conversion to uh a Boolean so
if I write something like if 0.4 which
is a double it's going to get implicitly
convered converted uh to a Boolean so
any guesses what happens if I actually
run this program let's see hands up it
will print
true no one all right hands up it will
print
false flail your hands if you have no
idea all right we got some flailing good
so let's just run it and see and see if
we remember the rules so it actually
says true which I think only one person
raised their hands up so why does it say
true yeah
any any literal is always one nope so
here we can go back a bit in the slides
let's go back a bit in the
slides so if C requires a Boolean so it
requires a Boolean for an expression
it will convert or cast your value
general rule if the number represents
zero it gets converted to zero otherwise
it gets converted to a one the only way
to represent zero as a double is just
0.0 so that's the only representation of
zero literally any other value evaluates
to True which might be a bit confusing
so that's what so
0.4 that does not equal zero so so it
gets converted to the Boolean one and
then we print off true yeah
so
yeah right yeah so no matter what that's
going to put the right answer yeah so
when we had num mod
2 like just num mod 2 so when we just
had num mod 2 the answer was the integer
one or zero which just gets converted to
the Boolean one or zero so that's fine
that's easy to invert but I explicitly
put the equal brackets here because that
was like relying on it get getting an
INT converted to a Boolean here if I do
equal equal the result is always a
Boolean right so it kind of jives so
your type should always dve Jive so you
should aim to have the expression in the
if statement always return a
Boolean otherwise some some surprising
things may
happen all right speaking of surprising
things
so there was that so let's see this
example so here I'll just show you the
code so I write a new program and I
write please input an integer again like
before I declare num get input from the
user and then maybe I have something
like this so I check if it's even and
then print if it's even and then maybe I
have another if
statement here that checks if the number
is 10 or greater if that's
true then it prints the number as 10 or
greater and then else the number is less
than 10
so this is actually a bit confusing
right because I don't know what should
actually happen which else does this or
sorry which if does this else belong to
does it belong to this if
or this if so if it belongs to this if
and I type a odd number then it should
immediately skip over this code and
maybe go directly to the else and for
any odd number it will print this number
is less than 10 H or
maybe what will happen is you know I
input a number if it's even it just
prints this as even even then checks hey
is a number greater than
10 if it's greater than 10 and that's
true oh well it would print this if it's
false then it's going to fall through
and print the else statement
so the question is if I accidentally or
well not accidentally if I input a odd
number will I see this line accidentally
get printed and my program is
wrong well let's just try it so here
would I call it two if
else so if I let's say I input an eight
it writes the number is even and the
number is less than
10 hm if I write something like seven it
says the number is less than 10 and
immediately and it doesn't say anything
else well there I can't tell which else
it was connected to so maybe I need to
try a odd number greater than 10 so
maybe 13 and if I do that it says the
number is 10 or greater H so something
is going on here and it is a little bit
confusing so the rule in C unlike other
languages is this actually has a problem
called the dangling else problem because
it's actually not clear based off the
syntax rules of C which if this else is
connected to if it's connected to this
one or if it's connected to this one
based off the inputs we actually tried
and how it actually works this else
seems to actually be connected to this
if because we'll never
see both the number is 10 or greater and
the number is less than 10 at the same
time so that is how C resolves the kind
of ambiguity of it this doesn't exist in
other languages like python that have
like an L if or something like that uh
one second so the rule is the else
always applies to the F directly above
it that was the question oh that was the
question yeah so the rule here the else
always applies to the if directly above
it yeah so there's
no yeah so there's no l in C there's we
can chain together if else but there's
no technical LF in C so we have to chain
them together so we'll see that in a
little bit
too
so oh
yeah how how you would do elsf uh did I
save that for yeah we can do that now so
let's say I only wanted one of these to
happen at a time right well to chain
them
together technically an if statement is
just a statement right and after an else
all I need is a statement so if I want
to change together things I could just
write else if like
that so if I write else if like that if
this number is false so if this is an
odd number this is false right so it's
going to obey the rules and take the
else branch in this case it's
immediately going to execute this if
statement so it will check if the number
is greater than 10 if the number is
greater than 10 then it'll do this print
off this number and then well it will
fall through at the end and not take the
else Branch here right if this is false
then it will take the else Branch so
that's how we can them together only one
of these will ever run ever if we chain
it together like that so if you've done
python this is a it's python just has a
shortcut to make it prettier this does
the exact same thing
though yeah do that answer your question
yeah it's kind of weird though but see
all about simple they don't need an L if
or anything like that you can chain
together lsfs if you want only one of
them to go by but technically we just
stacked up if statements right and of
course you can have if statements and if
statements so every if statement has two
choices it could run inside you could
have another if statement so that's
another two choices inside those you
could have another two choices boom
exponential growth and if you have
millions of them you have an llm model
essentially so llm models are basically
just the if statements are a bit more
complicated and there's a lot of them
but same power you now have in your
hands so we can actually do
something uh actually no let's so we can
actually do something a bit more useful
and create a fun little game that we can
you know start doing some gambling on
so let's start this number so we learned
about random numbers we now learned
about if statements so we can actually
go ahead and start betting because
betting is
fun
so let's make a game where we'll
simulate rolling a die so we'll simulate
rolling a die and then we're allowed to
bet on the result of the die whether or
not it's odd or even if we guess right
we win if we guess wrong we lose so I
can have the first line enter your bet
just for ease I'll just say even or odd
and I'll have the user input an
integer zero if they want to bet for
even and one if they want to bet for odd
and then after that get user get some
input from the user and then I can think
about the rest of my program so they
should again I'll just assume that the
user actually inputs either a one or
zero so they're nice to me
so if I want to simulate rolling a die I
probably need a source of Randomness
right so the value of a D should be
between one and six right so anyone
remember how I
would actually get a random number
between one or six so from last lecture
we had Rand Rand would return just a
random positive integer for us always
right so if I wanted to take the result
of Rand and kind of Squish it into being
a value between 1 and six what would I
have to do to Rand
yeah yeah mod 6 plus one right if I
remember everything or even if I don't
remember
it if I take Rand and I mod something uh
oh I called it Ram right good spelling
error so if I take any number from Rand
and mod 6 basically I'm going to get
that many different possibilities
ranging from zero all the way to
five so that is the result of ran mod 6
some number 0 to five and then if I want
to make it always be one to six all I
have to do is add one to that so yep you
just do
R so if I do Rand mod s that's a good
question so what happens if I do Rand
mod
7 yeah yeah then I include zero right so
Rand mod 7 that's some number between
zero and six inclusive but a die I don't
want to represent zero so six but that's
that's a good good observation all right
so that should be a random die roll
right so let's be fun it's a game so
I'll just say we rolled a and then
number so we can print to the user
whatever we rolled right so let's
compile
that run it as a game so enter my BET
right now it's not doing anything with
my bet let's
say just zero and it says I rolled a two
so seems to work that's a valid rule
right so knowing what we know now how
might I check to see if the user has one
or do they just always win because I
could just print you
know you win smiley
face so should I do that
yeah number and get the same number that
they in the yeah so I'm going to need an
if statement for this game right they
can either win or they can lose so I
probably want to do something show them
that they a winner or show them that
they're a loser so to do that I need an
if statement and like you said what you
might want to check
is we take the die roll so it was in a
variable called dice we can mod two so
that will give us in this case the die
will always be positive so it will
always result in either a zero or one
and then we can just check if it matches
whatever the user inputed so that's why
I had them input zero for even and one
for odd just to have it match of course
I could have flipped it around and then
flipped uh my if statements but this way
I think just looks a bit cleaner so I
can open the brackets for the if
statement then print you win so this
should say I win right so it matches
whatever I inputed and then
else maybe
maybe I'm a bit
meaner so perfectly good game right
so everyone agree that this is the
beginning of a fun betting game everyone
loves gambling right all right so run my
game enter your bet zero for even one
for odd
well I'll bet zero I rolled a two I won
again yay cool it's a game right so run
it again hopefully I roll a different
die number
right I'll just bet even again two oh
right two oh I'm such a winner why is
that well because Rand remember rand's a
pseudo number generator so comes up with
the same result every single time I
think I misspoke last lecture and saying
sometimes it will randomize
itself turns out we actually have to do
that so if I want to make this game a
bit more fun again this is not something
you actually need to know or something
you need to program anything like this
will be given to you but if I want to
introduce a source of Randomness
remember I can set the seed using this
is srand function and maybe I want to
set the seed based off something more
random so one thing that is a bit more
random is the current time hopefully
it's not predictable it's not the
greatest it is a bit predictable but it
is at least some value that's going to
change so there is a function called
time that if I give the Special Value
null it will give me back the current
time in the number of seconds if I set
that as my seed it's actually going to
be a bit different each time I run my
program again you don't actually need to
know that function this is just to make
my game a bit more fun you'd probably
just look this up figure out how to get
a better source of Randomness and this
is not the only source of Randomness so
I have to compile again of course
because this is C so now I can play my
game and hopefully it's actually random
and won't just have it to every single
time so I'll place my bed again I'll say
it's even and I run it and roll a two
okay that was just lucky I won
again I'll run it
again run it again I rolled a one and
I'm now a loser
finally cool huh there's a game it's not
it's not that fun well it's kind of fun
but I have to run my program every
single time I want to play it right I
can only play one round which is kind of
lame so next lecture we'll figure out
how to play multiple rounds without
having to relaunch our program all the
time so I'll Stick Around for another
five minutes but other than that I will
let you leave early so just remember
pulling for you we're all in this
together