1
00:00:09,350 --> 00:00:12,840
Everyone in this video, we're going to talk about conditionals.

2
00:00:12,890 --> 00:00:15,130
I already talked a little bit about it.

3
00:00:15,230 --> 00:00:21,170
I showed you a simple example and now we're going to see some other use cases for conditionals.

4
00:00:21,470 --> 00:00:29,180
So conditionals are used to determine if a block of code should or should not be executed depending

5
00:00:29,180 --> 00:00:30,910
on a given set of conditions.

6
00:00:31,280 --> 00:00:33,170
So let's see what this means.

7
00:00:33,440 --> 00:00:38,370
Let's just copy all this and then we are going to make some changes to this code.

8
00:00:38,750 --> 00:00:40,400
So here this is very simple.

9
00:00:40,400 --> 00:00:43,210
We just have a variable called age.

10
00:00:43,400 --> 00:00:47,600
Now we are just testing if this is less than 18.

11
00:00:47,780 --> 00:00:50,750
If it is, we are going to show under age.

12
00:00:51,110 --> 00:00:54,790
If it's not, we are going to show over age.

13
00:00:55,370 --> 00:00:58,940
But now let's say we want to add more conditions.

14
00:00:59,130 --> 00:01:02,600
So I'm just going to change the age to 70.

15
00:01:03,320 --> 00:01:08,960
So now I just want to create another group, which will be the senior group.

16
00:01:09,590 --> 00:01:14,330
So people above 65 years old will be part of the senior group.

17
00:01:15,330 --> 00:01:24,570
People above 18 will be adults and people below 18 is going to be under age, so let's get started with

18
00:01:24,570 --> 00:01:26,910
under 18 under age.

19
00:01:27,090 --> 00:01:28,020
This is good.

20
00:01:28,860 --> 00:01:32,010
And now we can include an else if.

21
00:01:34,260 --> 00:01:41,270
So by doing this, this is just like starting another if, but this is part of the same structure.

22
00:01:41,820 --> 00:01:45,270
So now the second condition is going to be.

23
00:01:47,360 --> 00:01:48,320
If age.

24
00:01:49,530 --> 00:01:51,750
Is greater than or equal to.

25
00:01:54,460 --> 00:01:58,180
Sixty five in this case, we are going to show.

26
00:01:59,250 --> 00:01:59,820
Senior.

27
00:02:01,040 --> 00:02:09,290
So now we just covered these two cases, so any other thing that happens will mean that the person is

28
00:02:09,290 --> 00:02:17,400
between 18 and 65 years old because we already covered below 18 and above 65.

29
00:02:17,420 --> 00:02:19,310
So now any other age?

30
00:02:20,280 --> 00:02:22,200
We can use it inside the house.

31
00:02:27,430 --> 00:02:32,290
So now we are going to do console dialogue and just show adult.

32
00:02:34,260 --> 00:02:42,750
So by doing this in this case, our age variable is 70, so if we refresh the page now it's showing

33
00:02:42,750 --> 00:02:43,350
senior.

34
00:02:44,690 --> 00:02:47,720
Let's try to change it to 14.

35
00:02:50,700 --> 00:02:55,890
Now it's showing on the rage and let's change it to 18.

36
00:02:58,640 --> 00:03:00,350
And now it's showing adult.

37
00:03:02,290 --> 00:03:10,660
So now we've just added one more condition to our structure, so one important thing is that no matter

38
00:03:10,660 --> 00:03:18,670
how many conditions we have, so we could have the first if statement and then 10 different else if

39
00:03:18,670 --> 00:03:19,500
statement.

40
00:03:19,720 --> 00:03:25,760
So no matter how many we have the moment when JavaScript finds a true value.

41
00:03:25,780 --> 00:03:33,560
So let's say it finds it here, it's going to execute this code and then just ignore all the rest.

42
00:03:33,880 --> 00:03:42,520
So when JavaScript finds a true value, it just stop testing all the rest of the conditions.

43
00:03:42,970 --> 00:03:50,470
If there's no true value in any of the conditions, then the code inside the statement will be executed.

44
00:03:51,070 --> 00:03:54,430
However, the statement, it's not mandatory.

45
00:03:54,440 --> 00:04:02,320
So in some cases we just don't want and we just need to execute something if some condition is true.

46
00:04:02,440 --> 00:04:05,970
If not, we just don't want to do anything.

47
00:04:05,980 --> 00:04:07,870
So this is possible as well.

48
00:04:08,020 --> 00:04:16,120
Now let's see how we can test multiple conditions at the same time using the end and or operators.

49
00:04:16,270 --> 00:04:25,180
So when we use and which can be represented by these two and symbols when we use it, all the conditions

50
00:04:25,390 --> 00:04:26,500
need to be true.

51
00:04:26,830 --> 00:04:28,750
So the code is executed.

52
00:04:29,850 --> 00:04:32,040
On the other hand, when using your.

53
00:04:33,440 --> 00:04:41,960
We just need one condition to be true, to execute the code, so let's do an example so we can understand

54
00:04:41,960 --> 00:04:43,280
better how it works.

55
00:04:43,640 --> 00:04:51,140
So let's analyze the grade and absences of a student and determine if he's been improved in the course

56
00:04:51,560 --> 00:04:52,580
to be improved.

57
00:04:52,760 --> 00:05:01,010
The student needs the grade to be seven or higher and the number of absences, five or lower.

58
00:05:01,400 --> 00:05:03,650
So let's just copy this.

59
00:05:04,830 --> 00:05:06,550
And this one as well.

60
00:05:06,720 --> 00:05:12,900
Let's go to Visual Studio Code, we can just comment all this shift at a.

61
00:05:17,390 --> 00:05:24,770
All right, so now first, let's solve the problem using end so we know that in order to be approved,

62
00:05:25,130 --> 00:05:30,910
the student needs the grade to be seven or higher and the number of absences, five are lower.

63
00:05:30,920 --> 00:05:34,070
So we need to test two conditions at the same time.

64
00:05:34,220 --> 00:05:35,240
How do we do this?

65
00:05:35,480 --> 00:05:37,850
We just start an if statement.

66
00:05:39,520 --> 00:05:44,500
Then the first condition is going to be that the grade is.

67
00:05:46,550 --> 00:05:49,790
Greater than or equal to seven.

68
00:05:51,010 --> 00:05:57,640
And now we are testing multiple conditions that should happen at the same time, so we are going to

69
00:05:57,640 --> 00:06:04,450
use the end operator here and we also need the absences to be.

70
00:06:07,110 --> 00:06:10,740
Less than or equal to five.

71
00:06:12,410 --> 00:06:19,190
If those things happen at the same time, so like I said, when we use the end operator, we need all

72
00:06:19,190 --> 00:06:24,130
the conditions to be true and we don't have to use only two conditions.

73
00:06:24,140 --> 00:06:26,630
We could add more here if we wanted.

74
00:06:27,350 --> 00:06:29,480
For this, we only need two conditions.

75
00:06:29,480 --> 00:06:31,230
So the grade and the absences.

76
00:06:31,610 --> 00:06:37,100
So if this is true and this is also true, then we are going to go inside here.

77
00:06:38,530 --> 00:06:46,190
So then we can do cancel that log and we can write the student has been approved otherwise.

78
00:06:46,420 --> 00:06:50,110
So inside the else statement, we can print.

79
00:06:51,270 --> 00:06:58,250
This student has failed, so in this case, we have seven and three, so the student should be approved.

80
00:06:58,620 --> 00:07:00,060
Let's test this out.

81
00:07:00,340 --> 00:07:01,950
I'm just going to save this.

82
00:07:03,340 --> 00:07:04,660
Refresh the page.

83
00:07:05,600 --> 00:07:08,600
And now we can see that the student has been approved.

84
00:07:09,870 --> 00:07:17,760
So now let me change the number of absences to six, and now the grade is the same, but now we have

85
00:07:17,760 --> 00:07:19,710
absences above five.

86
00:07:20,040 --> 00:07:20,940
So if we.

87
00:07:22,040 --> 00:07:23,090
Test this now.

88
00:07:23,900 --> 00:07:28,220
Now the student has failed so we can see that this is working.

89
00:07:28,520 --> 00:07:30,220
Now let's do the same thing.

90
00:07:31,350 --> 00:07:34,860
Using the OR operator.

91
00:07:47,040 --> 00:07:53,070
So this time, what we need to do is a bit different because we only need one of the two conditions

92
00:07:53,070 --> 00:07:53,790
to return.

93
00:07:53,790 --> 00:07:54,310
True.

94
00:07:54,450 --> 00:08:01,220
So in this case, instead of testing, if the student went well, we're just going to do the opposite.

95
00:08:01,500 --> 00:08:03,960
So if the grade is below seven.

96
00:08:05,450 --> 00:08:07,970
Or the absences are.

97
00:08:09,960 --> 00:08:18,120
Above five, then we know that the student has failed, so if only one is true, if this one is true,

98
00:08:18,390 --> 00:08:23,870
no matter if the absences are good, because this student will fail.

99
00:08:24,480 --> 00:08:27,690
So in this case, we just have to change this.

100
00:08:29,560 --> 00:08:36,850
So in the first statement, the student will fail and in the second one, the student will be approved.

101
00:08:38,890 --> 00:08:46,240
Now, before saving this, I just forgot to change the operator, so now we are using or so if this

102
00:08:46,240 --> 00:08:54,700
happens or this happens, then the student is going to fail, save this, refresh the page, and now

103
00:08:54,700 --> 00:08:55,960
the student has failed.

104
00:08:56,500 --> 00:08:57,880
Let's try something else.

105
00:08:57,880 --> 00:09:03,250
Like let's try to grade five and the absences three.

106
00:09:03,490 --> 00:09:06,100
The student should fail again because of the grade.

107
00:09:07,730 --> 00:09:13,040
Refresh the page now we can see that this is working and now let's try.

108
00:09:14,110 --> 00:09:20,920
Eight and three, and if we do this now, the student has been approved.

109
00:09:21,130 --> 00:09:21,570
All right.

110
00:09:21,580 --> 00:09:29,710
So these were the examples I wanted to show for now, before we finish this subject, I still need to

111
00:09:29,710 --> 00:09:31,860
talk about two important things.

112
00:09:31,870 --> 00:09:35,620
So the first one is evaluating values as conditions.

113
00:09:36,600 --> 00:09:43,200
And I also want to talk about another way of writing conditionals, which is by using the ternary IF.

114
00:09:44,180 --> 00:09:49,970
So let's do this in a separate video so this one doesn't get too long, so I'll wait for you in the

115
00:09:49,970 --> 00:09:52,310
next video so we can continue with this.
