WEBVTT

0
00:00.590 --> 00:08.270
Very often I've asked you in the quizzes especially to play computer, and the reason is because this

1
00:08.270 --> 00:13.580
skill of pretending to be a computer, reading through your code and imagining what you're going to

2
00:13.580 --> 00:18.830
do each time is really, really useful, especially when you're debugging.

3
00:18.860 --> 00:20.600
Take a look at this code.

4
00:20.810 --> 00:28.730
It takes an input in the form of an integer, and it asks the user for their year of birth, and then

5
00:28.730 --> 00:37.040
it uses an if statement to check if they were born between 1980 and 1994, in which case they're classified

6
00:37.040 --> 00:38.120
as a Millennial.

7
00:38.840 --> 00:44.300
But if they were born later than 1994, then they're classified as a Gen Z.

8
00:44.480 --> 00:50.720
So depending on which country you're from, there's different classifications for your particular generation.

9
00:50.720 --> 00:54.350
This is just the typical words that you hear on the internet,

10
00:54.350 --> 00:57.380
"You're a Millennial, you're a Gen Z, you're a Baby boomer."

11
00:57.380 --> 01:00.050
And this comes from a lot of the history of the US.

12
01:00.860 --> 01:08.720
But nonetheless, I want you to run the code, and I want you to see what happens when I put in 1994

13
01:08.870 --> 01:10.850
absolutely nothing.

14
01:10.850 --> 01:12.800
So we have our bug right there.

15
01:12.920 --> 01:19.610
Run through the code line by line and evaluate each statement and follow the logic and check what it

16
01:19.610 --> 01:20.960
will evaluate to.

17
01:20.990 --> 01:25.640
So I want you to play computer and figure out what is the problem,

18
01:25.640 --> 01:28.340
and then go ahead and fix the bug.

19
01:28.670 --> 01:29.810
Pause the video now.

20
01:32.720 --> 01:33.230
All right.

21
01:33.230 --> 01:37.670
So let's pretend that we got 1994 as the input.

22
01:37.670 --> 01:39.680
So now I'm the computer

23
01:39.680 --> 01:43.820
and I know that this year is equal to 1994.

24
01:43.850 --> 01:49.130
Now with this year being equal to 1994, I go into this if statement.

25
01:49.130 --> 01:53.720
So is 1994 greater than 1980?

26
01:54.380 --> 01:55.730
Yes it is.

27
01:55.730 --> 01:58.670
So this actually becomes True.

28
01:59.120 --> 02:01.760
Now I have to check the second condition,

29
02:01.760 --> 02:04.220
and it also has to be True for this

30
02:04.250 --> 02:05.930
if block to be triggered.

31
02:05.930 --> 02:10.370
So is 1994 less than 1994?

32
02:10.370 --> 02:11.240
No it's not.

33
02:11.240 --> 02:17.790
It's actually less than equal to 1994 or it's equal to 1994.

34
02:17.910 --> 02:21.630
So in this case, this condition becomes False,

35
02:21.630 --> 02:28.650
and we know that if we try to combine a True and a False, then it actually just becomes a False.

36
02:28.650 --> 02:30.510
So this gets skipped.

37
02:31.080 --> 02:34.590
Now next we look at the next statement, right?

38
02:34.590 --> 02:41.240
If 1994 is greater than 1994 well that's also not True.

39
02:41.270 --> 02:43.160
It could be greater or equal to,

40
02:43.160 --> 02:45.230
or it could just be equal to.

41
02:45.260 --> 02:48.860
So this condition is also False.

42
02:48.860 --> 02:52.190
So that means it's also going to skip this next line

43
02:52.190 --> 02:57.320
and there's no more lines of code left, which is why the computer doesn't print anything.

44
02:57.320 --> 03:02.060
So let's restore our code to before we started playing computer.

45
03:02.060 --> 03:12.170
And we can identify that this problem occurs because there is no bucket that actually catches the 1994.

46
03:12.320 --> 03:20.120
We could simply fix this code by changing one of these conditions to be greater than, or equal to, either

47
03:20.120 --> 03:21.590
here or here.

48
03:21.770 --> 03:27.080
And that means that the year 1994 is not skipped over in our conditions.

49
03:27.080 --> 03:32.390
And when we hit Run, it'll actually tell us that we are, in fact, a Gen Z.