Very often I've asked you in the quizzes
especially to play computer.
And the reason is because this skill of pretending to be a computer reading
through your code and imagining what you going to do each time is really,
really useful, especially when you're debugging.
So let's comment out the previous block and try the next block.
Take a look at this code.
It takes an input
in the form of an integer and it ask the user for their year of birth.
And then it uses an if statement to check if they were born between 1980 and
1994, in which case they're classified as a millennial.
But if there were born later than 1994, then they're classified as a Gen Z.
So depending on which country you're from,
there's different classifications for your particular generation.
This is just the typical words that you hear on the internet.
You're a millennial, you're a Gen Z, you're a baby boomer,
and this comes from a lot of the history of the US. But nonetheless,
I want you to run the code and I want you to see what happens when I put in
1994. Absolutely nothing,
so we have our bug right there. Run through the code line-by-line and evaluate
each statement. For example, if year is equal to 1990,
will line 77 evaluate to true or to false?
What if year is equal to 1994?
Look at each line of code and follow the logic and check what it will evaluate
to.
So I want you to play computer and figure out what is the problem,
and then go ahead and fix the bug. Pause the video now.
All right. So
let's pretend that we got 1994 as the input.
So now I'm the computer
and I know that this year is equal to 1994.
Now with this year being equal to 1994,
I go into this if statement so is 1994,
greater than 1980? Yes it is.
So this actually becomes true.
Now I have to check the second condition and it also has to be true for this if
block to be triggered. So is 1994,
less than 1994? No, it's not.
It's actually less than equal to 1994,
or it's equal to 1994. So in this case,
this condition becomes false.
And we know that if we try to combine a true and a false then it actually just
becomes a false.
So this gets skipped.
Now, next, we look at the next statement, right?
If 1994 is greater than 1994,
well, that's also not true. It be greater or equal to,
or it could just be equal to.
So this condition is also false.
So that means it's also going to skip this next line and there's no more lines
of code left, which is why the computer doesn't print anything.
So let's restore our code to before we started playing computer
and we can identify that this problem occurs because there is no bucket that
actually catches the 1994.
We could simply fix this code by changing one of these conditions to be greater
than or equal to either here or here.
And that means that the year 1994 is not skipped over in our conditions.
And when we hit run, it will actually tell us that we are in fact,
a Gen Z.