1 00:00:00,000 --> 00:00:03,400 In this video, you will learn about conditions and branching. 2 00:00:03,400 --> 00:00:07,150 Comparison operations compares some value or operand. 3 00:00:07,150 --> 00:00:09,330 Then based on some condition, 4 00:00:09,330 --> 00:00:10,830 they produce a Boolean. 5 00:00:10,830 --> 00:00:14,830 Let's say we assign a value of a to six. 6 00:00:14,830 --> 00:00:17,420 We can use the equality operator denoted with 7 00:00:17,420 --> 00:00:21,370 two equal signs to determine if two values are equal. 8 00:00:21,370 --> 00:00:24,660 In this case, if seven is equal to six. 9 00:00:24,660 --> 00:00:27,680 In this case, as six is not equal to seven, 10 00:00:27,680 --> 00:00:29,480 the result is false. 11 00:00:29,480 --> 00:00:32,890 If we performed an equality test for the value six, 12 00:00:32,890 --> 00:00:34,920 the two values would be equal. 13 00:00:34,920 --> 00:00:37,110 As a result, we would get a true. 14 00:00:37,110 --> 00:00:40,480 Consider the following equality comparison operator: 15 00:00:40,480 --> 00:00:43,490 If the value of the left operand, in this case, 16 00:00:43,490 --> 00:00:47,540 the variable i is greater than the value of the right operand, 17 00:00:47,540 --> 00:00:49,150 in this case five, 18 00:00:49,150 --> 00:00:52,890 the condition becomes true or else we get a false. 19 00:00:52,890 --> 00:00:55,770 Let's display some values for i on the left. 20 00:00:55,770 --> 00:01:00,340 Let's see the value is greater than five in green and the rest in red. 21 00:01:00,340 --> 00:01:02,810 If we set i equal to six, 22 00:01:02,810 --> 00:01:07,520 we see that six is larger than five and as a result, we get a true. 23 00:01:07,520 --> 00:01:10,860 We can also apply the same operations to floats. 24 00:01:10,860 --> 00:01:13,620 If we modify the operator as follows, 25 00:01:13,620 --> 00:01:19,210 if the left operand i is greater than or equal to the value of the right operand, 26 00:01:19,210 --> 00:01:20,720 in this case five, 27 00:01:20,720 --> 00:01:22,780 then the condition becomes true. 28 00:01:22,780 --> 00:01:24,960 In this case, we include the value of 29 00:01:24,960 --> 00:01:28,960 five in the number line and the color changes to green accordingly. 30 00:01:28,960 --> 00:01:31,900 If we set the value of i equal to five, 31 00:01:31,900 --> 00:01:34,180 the operand will produce a true. 32 00:01:34,180 --> 00:01:36,750 If we set the value of i to two, 33 00:01:36,750 --> 00:01:39,900 we would get a false because two is less than five. 34 00:01:39,900 --> 00:01:44,470 We can change the inequality if the value of the left operand, in this case, 35 00:01:44,470 --> 00:01:47,590 i is less than the value of the right operand, 36 00:01:47,590 --> 00:01:49,480 in this case, six. 37 00:01:49,480 --> 00:01:51,500 Then condition becomes true. 38 00:01:51,500 --> 00:01:54,870 Again, we can represent this with a colored number line. 39 00:01:54,870 --> 00:01:57,910 The areas where the inequality is true are marked in 40 00:01:57,910 --> 00:02:01,250 green and red where the inequality is false. 41 00:02:01,250 --> 00:02:03,810 If the value for i is set to two, 42 00:02:03,810 --> 00:02:05,390 the result is a true. 43 00:02:05,390 --> 00:02:07,770 As two is less than six. 44 00:02:07,770 --> 00:02:12,630 The inequality test uses an explanation mark preceding the equal sign. 45 00:02:12,630 --> 00:02:15,030 If two operands are not equal, 46 00:02:15,030 --> 00:02:17,140 then the condition becomes true. 47 00:02:17,140 --> 00:02:18,940 We can use a number line. 48 00:02:18,940 --> 00:02:20,850 When the condition is true, 49 00:02:20,850 --> 00:02:26,390 the corresponding numbers are marked in green and red for where the condition is false. 50 00:02:26,390 --> 00:02:28,840 If we set i equal to two, 51 00:02:28,840 --> 00:02:32,900 the operator is true as two is not equal to six. 52 00:02:32,900 --> 00:02:34,960 We compare strings as well. 53 00:02:34,960 --> 00:02:40,170 Comparing ACDC and Michael Jackson using the equality test, 54 00:02:40,170 --> 00:02:43,460 we get a false, as the strings are not the same. 55 00:02:43,460 --> 00:02:45,520 Using the inequality test, 56 00:02:45,520 --> 00:02:48,440 we get a true, as the strings are different. 57 00:02:48,440 --> 00:02:50,880 See the Lapps for more examples. 58 00:02:50,880 --> 00:02:55,010 Branching allows us to run different statements for a different input. 59 00:02:55,010 --> 00:02:58,920 It's helpful to think of an if statement as a locked room. 60 00:02:58,920 --> 00:03:00,670 If this statement is true, 61 00:03:00,670 --> 00:03:05,360 you can enter the room and your program can run some predefined task. 62 00:03:05,360 --> 00:03:07,070 If the statement is false, 63 00:03:07,070 --> 00:03:09,230 your program will skip the task. 64 00:03:09,230 --> 00:03:15,200 For example, consider the blue rectangle representing an ACDC concert. 65 00:03:15,200 --> 00:03:17,780 If the individual is 18 or older, 66 00:03:17,780 --> 00:03:20,240 they can enter the ACDC concert. 67 00:03:20,240 --> 00:03:22,230 If they are under the age of 18, 68 00:03:22,230 --> 00:03:24,040 they cannot enter the concert. 69 00:03:24,040 --> 00:03:28,100 Individual proceeds to the concert their age is 17, 70 00:03:28,100 --> 00:03:33,300 therefore, they are not granted access to the concert and they must move on. 71 00:03:33,300 --> 00:03:35,420 If the individual is 19, 72 00:03:35,420 --> 00:03:36,790 the condition is true. 73 00:03:36,790 --> 00:03:39,970 They can enter the concert then they can move on. 74 00:03:39,970 --> 00:03:44,070 This is the syntax of the if statement from our previous example. 75 00:03:44,070 --> 00:03:45,930 We have the if statement. 76 00:03:45,930 --> 00:03:49,080 We have the expression that can be true or false. 77 00:03:49,080 --> 00:03:52,750 The brackets are not necessary. We have a colon. 78 00:03:52,750 --> 00:03:57,480 Within an indent, we have the expression that is run if the condition is true. 79 00:03:57,480 --> 00:04:00,190 The statements after the if statement will run 80 00:04:00,190 --> 00:04:03,100 regardless if the condition is true or false. 81 00:04:03,100 --> 00:04:05,440 For the case where the age is 17, 82 00:04:05,440 --> 00:04:08,750 we set the value of the variable age to 17. 83 00:04:08,750 --> 00:04:10,570 We check the if statement, 84 00:04:10,570 --> 00:04:12,270 the statement is false. 85 00:04:12,270 --> 00:04:17,140 Therefore the program will not execute the statement to print, "you will enter". 86 00:04:17,140 --> 00:04:20,420 In this case, it will just print "move on". 87 00:04:20,420 --> 00:04:22,530 For the case where the age is 19, 88 00:04:22,530 --> 00:04:25,480 we set the value of the variable age to 19. 89 00:04:25,480 --> 00:04:27,290 We check the if statement. 90 00:04:27,290 --> 00:04:28,810 The statement is true. 91 00:04:28,810 --> 00:04:33,060 Therefore, the program will execute the statement to print "you will enter". 92 00:04:33,060 --> 00:04:35,660 Then it will just print "move on". 93 00:04:35,660 --> 00:04:40,830 The else statement will run a different block of code if the same condition is false. 94 00:04:40,830 --> 00:04:44,320 Let's use the ACDC concert analogy again. 95 00:04:44,320 --> 00:04:46,060 If the user is 17, 96 00:04:46,060 --> 00:04:48,930 they cannot go to the ACDC concert but they 97 00:04:48,930 --> 00:04:52,670 can go to the Meat Loaf concert represented by the purple square. 98 00:04:52,670 --> 00:04:54,710 If the individual is 19, 99 00:04:54,710 --> 00:04:55,980 the condition is true, 100 00:04:55,980 --> 00:05:00,640 they can enter the ACDC concert then they can move on as before. 101 00:05:00,640 --> 00:05:03,470 The syntax of the else statement is similar. 102 00:05:03,470 --> 00:05:05,820 We simply append the statement else. 103 00:05:05,820 --> 00:05:09,520 We then add the expression we would like to execute with an indent. 104 00:05:09,520 --> 00:05:11,510 For the case where the age is 17, 105 00:05:11,510 --> 00:05:14,460 we set the value of the variable age to 17. 106 00:05:14,460 --> 00:05:16,310 We check the if statement, 107 00:05:16,310 --> 00:05:18,080 the statement is false. 108 00:05:18,080 --> 00:05:21,030 Therefore, we progress to the else statement. 109 00:05:21,030 --> 00:05:23,480 We run the statement in the indent. 110 00:05:23,480 --> 00:05:27,360 This corresponds to the individual attending the Meat Loaf concert. 111 00:05:27,360 --> 00:05:30,390 The program will then continue running. 112 00:05:30,390 --> 00:05:33,300 For the case where the age is 19, 113 00:05:33,300 --> 00:05:36,290 we set the value of the variable age to 19. 114 00:05:36,290 --> 00:05:38,190 We check the if statement, 115 00:05:38,190 --> 00:05:39,710 the statement is true. 116 00:05:39,710 --> 00:05:43,730 Therefore, the program will execute the statement to print "you will enter". 117 00:05:43,730 --> 00:05:45,870 The program skips the expressions in 118 00:05:45,870 --> 00:05:49,870 the else statement and continues to run the rest of the expressions. 119 00:05:49,870 --> 00:05:53,910 The elif statement, short for else if, 120 00:05:53,910 --> 00:05:58,440 allows us to check additional conditions if the preceding condition is false. 121 00:05:58,440 --> 00:05:59,950 If the condition is true, 122 00:05:59,950 --> 00:06:02,030 the alternate expressions will be run. 123 00:06:02,030 --> 00:06:04,010 Consider the concert example, 124 00:06:04,010 --> 00:06:05,830 if the individual is 18, 125 00:06:05,830 --> 00:06:08,430 they will go to the Pink Floyd concert instead of 126 00:06:08,430 --> 00:06:11,390 attending the ACDC or Meat Loaf concerts. 127 00:06:11,390 --> 00:06:17,010 The person of 18 years of age enters the area as they are not over 19 years of age. 128 00:06:17,010 --> 00:06:20,670 They cannot see ACDC but as their 18 years, 129 00:06:20,670 --> 00:06:22,160 they attend Pink Floyd. 130 00:06:22,160 --> 00:06:26,240 After seeing Pink Floyd, they move on. 131 00:06:26,240 --> 00:06:30,870 The syntax of the elif statement is similar. 132 00:06:30,870 --> 00:06:34,320 We simply add the statement elif with the condition. 133 00:06:34,320 --> 00:06:36,580 We, then add the expression we would like to 134 00:06:36,580 --> 00:06:39,860 execute if the statement is true with an indent. 135 00:06:39,860 --> 00:06:42,400 Let's illustrate the code on the left. 136 00:06:42,400 --> 00:06:44,570 An 18 year old enters. 137 00:06:44,570 --> 00:06:46,800 They are not older than 18 years of age. 138 00:06:46,800 --> 00:06:48,720 Therefore, the condition is false. 139 00:06:48,720 --> 00:06:51,620 So the condition of the elif statement is checked. 140 00:06:51,620 --> 00:06:53,310 The condition is true. 141 00:06:53,310 --> 00:06:56,390 So then we would print "go see Pink Floyd". 142 00:06:56,390 --> 00:06:59,080 Then we would move on as before. 143 00:06:59,080 --> 00:07:01,420 If the variable age was 17, 144 00:07:01,420 --> 00:07:04,030 the statement "go see Meat Loaf" would print. 145 00:07:04,030 --> 00:07:07,060 Similarly, if the age was greater than 18, 146 00:07:07,060 --> 00:07:09,320 the statement "you can enter" would print. 147 00:07:09,320 --> 00:07:11,760 Check the Lapps for more examples. 148 00:07:11,760 --> 00:07:14,710 Now let's take a look at logic operators. 149 00:07:14,710 --> 00:07:19,710 Logic operations take Boolean values and produce different Boolean values. 150 00:07:19,710 --> 00:07:22,760 The first operation is the not operator. 151 00:07:22,760 --> 00:07:24,430 If the input is true, 152 00:07:24,430 --> 00:07:26,200 the result is a false. 153 00:07:26,200 --> 00:07:29,130 Similarly, if the input is false, 154 00:07:29,130 --> 00:07:31,130 the result is a true. 155 00:07:31,130 --> 00:07:34,890 Let A and B represent Boolean variables. 156 00:07:34,890 --> 00:07:40,300 The OR operator takes in the two values and produces a new Boolean value. 157 00:07:40,300 --> 00:07:43,750 We can use this table to represent the different values. 158 00:07:43,750 --> 00:07:47,620 The first column represents the possible values of A. 159 00:07:47,620 --> 00:07:51,330 The second column represents the possible values of B. 160 00:07:51,330 --> 00:07:56,390 The final column represents the result of applying the OR operation. 161 00:07:56,390 --> 00:08:02,880 We see the OR operator only produces a false if all the Boolean values are false. 162 00:08:02,880 --> 00:08:08,780 The following lines of code will print out: "This album was made in the 70s' or 90's", 163 00:08:08,780 --> 00:08:12,210 if the variable album year does not fall in the 80s. 164 00:08:12,210 --> 00:08:16,130 Let's see what happens when we set the album year to 1990. 165 00:08:16,130 --> 00:08:18,780 The colored number line is green when the condition is 166 00:08:18,780 --> 00:08:21,540 true and red when the condition is false. 167 00:08:21,540 --> 00:08:23,690 In this case, the condition is false. 168 00:08:23,690 --> 00:08:25,730 Examining the second condition, 169 00:08:25,730 --> 00:08:29,370 we see that 1990 is greater than 1989. 170 00:08:29,370 --> 00:08:31,340 So the condition is true. 171 00:08:31,340 --> 00:08:35,210 We can verify by examining the corresponding second number line. 172 00:08:35,210 --> 00:08:36,800 In the final number line, 173 00:08:36,800 --> 00:08:39,710 the green region indicates, where the area is true. 174 00:08:39,710 --> 00:08:43,510 This region corresponds to where at least one statement is true. 175 00:08:43,510 --> 00:08:46,310 We see that 1990 falls in the area. 176 00:08:46,310 --> 00:08:48,800 Therefore, we execute the statement. 177 00:08:48,800 --> 00:08:51,900 Let A and B represent Boolean variables. 178 00:08:51,900 --> 00:08:57,290 The AND operator takes in the two values and produces a new Boolean value. 179 00:08:57,290 --> 00:09:00,590 We can use this table to represent the different values. 180 00:09:00,590 --> 00:09:04,170 The first column represents the possible values of A. 181 00:09:04,170 --> 00:09:08,000 The second column represents the possible values of B. 182 00:09:08,000 --> 00:09:12,810 The final column represents the result of applying the AND operation. 183 00:09:12,810 --> 00:09:18,630 We see the OR operator only produces a true if all the Boolean values are true. 184 00:09:18,630 --> 00:09:22,800 The following lines of code will print out "This album was made in the 185 00:09:22,800 --> 00:09:27,940 80's" if the variable album year is between 1980 and 1989. 186 00:09:27,940 --> 00:09:31,780 Let's see what happens when we set the album year to 1983. 187 00:09:31,780 --> 00:09:36,880 As before, we can use the colored number line to examine where the condition is true. 188 00:09:36,880 --> 00:09:40,640 In this case, 1983 is larger than 1980, 189 00:09:40,640 --> 00:09:42,310 so the condition is true. 190 00:09:42,310 --> 00:09:44,430 Examining the second condition, 191 00:09:44,430 --> 00:09:47,950 we see that 1990 is greater than 1983. 192 00:09:47,950 --> 00:09:50,240 So, this condition is also true. 193 00:09:50,240 --> 00:09:54,290 We can verify by examining the corresponding second number line. 194 00:09:54,290 --> 00:09:56,050 In the final number line, 195 00:09:56,050 --> 00:09:59,690 the green region indicates where the area is true. 196 00:09:59,690 --> 00:10:03,900 Similarly, this region corresponds to where both statements are true. 197 00:10:03,900 --> 00:10:06,960 We see that 1983 falls in the area. 198 00:10:06,960 --> 00:10:09,280 Therefore, we execute the statement. 199 00:10:09,280 --> 00:10:12,000 Branching allows us to run different statements for different inputs. 200 00:10:12,000 --> 00:10:17,000 (Music)