1 00:00:00,530 --> 00:00:02,550 When programming in any language. 2 00:00:02,660 --> 00:00:09,680 It's very common to create logic that performs different actions based on one or more conditions. 3 00:00:09,710 --> 00:00:17,010 JavaScript has a variety of conditional statements that will explore in this lesson we'll be exploring 4 00:00:17,010 --> 00:00:25,610 the if else and else if statements these statements allow us to execute an action contained within a 5 00:00:25,610 --> 00:00:26,940 block of code. 6 00:00:27,110 --> 00:00:36,330 If certain conditions are met let's take a look at how this works with a simple example will create 7 00:00:36,330 --> 00:00:44,660 a script that converts a student's numerical test score into a letter grade to keep it simple we'll 8 00:00:44,670 --> 00:00:48,190 assume the test was out of 100 marks. 9 00:00:48,350 --> 00:00:57,510 First we need to define the grading system let's say that a score of zero to forty nine equals an F 10 00:00:58,640 --> 00:01:08,210 fifty two sixty nine equals a d seventy to seventy nine equals a C eighteen to eighty nine equals A 11 00:01:08,210 --> 00:01:13,310 B and 90 to 100 one hundred inclusive equals and a 12 00:01:16,190 --> 00:01:17,210 in order to do this. 13 00:01:17,240 --> 00:01:22,250 We need to start by creating our test score variable and assigning a value 14 00:01:30,380 --> 00:01:39,000 called the variable score and we'll assign a value of sixty five for now keep in mind that we'll make 15 00:01:39,000 --> 00:01:46,010 changes to this score to test our script later on. 16 00:01:46,020 --> 00:01:52,690 Next we'll need to start with our first if statement if the score is less than 50. 17 00:01:52,720 --> 00:01:56,990 We want to output an F so start by typing. 18 00:01:57,000 --> 00:01:57,420 If 19 00:02:00,730 --> 00:02:03,580 score is less than 50 20 00:02:10,530 --> 00:02:25,700 you will output an F using the document write method. 21 00:02:26,050 --> 00:02:31,780 If the score is greater than 50 but less than 70 we want to output a D. 22 00:02:31,780 --> 00:02:38,240 Since we're adding a condition to our original if statement we must start this condition with the. 23 00:02:38,240 --> 00:02:39,380 Else if statement 24 00:03:09,960 --> 00:03:10,780 a copy this. 25 00:03:10,780 --> 00:03:19,880 Else if statement because when you create a few others so the next one we need is if the score is greater 26 00:03:19,880 --> 00:03:22,790 than 70 but less than 80. 27 00:03:22,790 --> 00:03:24,110 We want to output a C 28 00:03:46,530 --> 00:03:51,750 this is for if the score is greater than 80 but less than 90 will output a B 29 00:03:55,940 --> 00:04:01,640 and if the score is greater than 90 or equal to 100 we want to output on a 30 00:04:13,740 --> 00:04:20,100 the last thing we want to do is if this score is anything above one hundred we need to display an error 31 00:04:20,100 --> 00:04:26,820 message indicating that the score must be less than 100 because the exam was out of one hundred marks 32 00:04:28,170 --> 00:04:34,030 for this we'll use the L statement the L statement instructs the script on what to do. 33 00:04:34,110 --> 00:04:36,750 If none of the preceding conditions have been met 34 00:04:57,150 --> 00:05:02,150 in I'll go ahead and save this file and previewed in our web browser. 35 00:05:08,280 --> 00:05:09,180 So here we have 36 00:05:12,160 --> 00:05:13,820 d that has been output. 37 00:05:15,170 --> 00:05:20,950 Because our current score we've set it to sixty five. 38 00:05:21,140 --> 00:05:24,590 Now that is greater than 50 in less than 70. 39 00:05:24,590 --> 00:05:32,040 So if we were to change our score let's go ahead and change the score to 81 40 00:05:38,490 --> 00:05:48,770 and in this case it correctly outputs a B because that's over 80 but less and less than 90. 41 00:05:48,780 --> 00:05:53,580 Now let's see what will happen if we create a score above 100 42 00:05:59,410 --> 00:06:02,470 is where our L statement is working. 43 00:06:02,470 --> 00:06:08,630 The score must be less than 100 and so you can go ahead and play with that. 44 00:06:09,370 --> 00:06:16,840 But that's basically how the if else if and l statements were also known as conditional statements.