1 00:00:07,090 --> 00:00:07,370 VIPUL SHEKHAWAT: Hi. 2 00:00:07,370 --> 00:00:09,410 In this video I'll be introducing you to one of most 3 00:00:09,410 --> 00:00:11,260 fundamental concepts in logic and 4 00:00:11,260 --> 00:00:13,880 programming, the Boolean value. 5 00:00:13,880 --> 00:00:16,210 If you're curious about the name, Boolean values and 6 00:00:16,210 --> 00:00:19,440 conditions are named after George Boole, a 19th century 7 00:00:19,440 --> 00:00:22,030 mathematician who pioneered what is now called Boolean 8 00:00:22,030 --> 00:00:23,980 logic, which is based by grouping and 9 00:00:23,980 --> 00:00:26,030 comparing Boolean values. 10 00:00:26,030 --> 00:00:28,250 >> So what is a Boolean value? 11 00:00:28,250 --> 00:00:30,920 A Boolean value is a variable that just has two possible 12 00:00:30,920 --> 00:00:33,710 conditions, true and false. 13 00:00:33,710 --> 00:00:35,380 You can think of it as a light switch. 14 00:00:35,380 --> 00:00:38,530 It can be either on or off, true or false. 15 00:00:38,530 --> 00:00:41,880 Similarly, binary numbers can be either one or zero, which 16 00:00:41,880 --> 00:00:45,680 is analogous to the same thing, true or false. 17 00:00:45,680 --> 00:00:46,840 Simple, right? 18 00:00:46,840 --> 00:00:48,750 The concept of a Boolean variable is easy to 19 00:00:48,750 --> 00:00:51,720 understand, but the ways in which you can manipulate and 20 00:00:51,720 --> 00:00:54,570 combine them allows for much of greater complexity. 21 00:00:54,570 --> 00:00:57,030 In addition to the two fundamental Boolean values, 22 00:00:57,030 --> 00:00:59,350 there are many Boolean operators that can combine two 23 00:00:59,350 --> 00:01:01,760 Boolean values into a single one. 24 00:01:01,760 --> 00:01:03,540 >> Two of the most basic, but most important 25 00:01:03,540 --> 00:01:06,570 operators, are AND and OR. 26 00:01:06,570 --> 00:01:09,950 The AND operator results in a value of true only if both of 27 00:01:09,950 --> 00:01:14,630 the values it takes are true, so false AND true is false. 28 00:01:14,630 --> 00:01:17,540 Likewise, false AND false is false. 29 00:01:17,540 --> 00:01:21,080 Only true AND true equals true. 30 00:01:21,080 --> 00:01:24,050 The OR operator results in the value of true if either of the 31 00:01:24,050 --> 00:01:25,620 values it takes are true. 32 00:01:25,620 --> 00:01:30,460 So false OR false is false, but true OR false is true, and 33 00:01:30,460 --> 00:01:33,710 true OR true is also true. 34 00:01:33,710 --> 00:01:36,560 The NOT operator simply takes a Boolean variable and gives 35 00:01:36,560 --> 00:01:37,830 you the opposite of it. 36 00:01:37,830 --> 00:01:40,950 So true becomes false and false becomes true. 37 00:01:40,950 --> 00:01:43,130 If you put the whole thing together, variables with 38 00:01:43,130 --> 00:01:46,320 operators, you create a Boolean expression. 39 00:01:46,320 --> 00:01:48,550 >> Now let's look at an example of nesting these Boolean 40 00:01:48,550 --> 00:01:49,950 expressions. 41 00:01:49,950 --> 00:01:51,840 Do you remember the order of operations? 42 00:01:51,840 --> 00:01:54,700 As with numbers, Boolean expressions can be grouped by 43 00:01:54,700 --> 00:01:56,270 using parentheses. 44 00:01:56,270 --> 00:02:00,150 So there are basically three expressions here, NOT z, y OR 45 00:02:00,150 --> 00:02:04,740 NOT z, and x AND y OR NOT z. 46 00:02:04,740 --> 00:02:07,080 We can figure out the values of these by looking at the 47 00:02:07,080 --> 00:02:09,020 inside and working our way out. 48 00:02:09,020 --> 00:02:12,850 So let's suppose x is true, y is true, and z is also true. 49 00:02:12,850 --> 00:02:15,270 What would NOT z evaluate to? 50 00:02:15,270 --> 00:02:20,970 Since we start with true, NOT z would simply be false. 51 00:02:20,970 --> 00:02:26,230 So now we have false OR y. 52 00:02:26,230 --> 00:02:29,740 If you look on top, you can see that y is true, y OR false 53 00:02:29,740 --> 00:02:32,870 would still just be true. 54 00:02:32,870 --> 00:02:37,580 Lastly we, have x AND true. 55 00:02:37,580 --> 00:02:39,300 So what's x AND true? 56 00:02:39,300 --> 00:02:42,590 x is true and true is also true, so this whole thing 57 00:02:42,590 --> 00:02:45,070 evaluates to true. 58 00:02:45,070 --> 00:02:47,270 >> Next, let's look at how these Boolean expressions can 59 00:02:47,270 --> 00:02:49,890 actually be used in a programming language. 60 00:02:49,890 --> 00:02:52,900 In C, the syntax for Boolean operations is a bit different 61 00:02:52,900 --> 00:02:55,520 from the words and, or, and not. 62 00:02:55,520 --> 00:02:57,210 Let's cover the syntax. 63 00:02:57,210 --> 00:03:00,510 To use the AND operator, we write a double ampersand. 64 00:03:00,510 --> 00:03:03,620 The OR operator is a double pipe line character. 65 00:03:03,620 --> 00:03:05,780 This is the straight vertical line, which you can probably 66 00:03:05,780 --> 00:03:09,070 find above the Enter or Return keys on your keyboard. 67 00:03:09,070 --> 00:03:12,550 And the NOT operator is simply an exclamation mark. 68 00:03:12,550 --> 00:03:15,550 So to rewrite the expression we had before, we would just 69 00:03:15,550 --> 00:03:27,010 write this x && y || !z. 70 00:03:27,010 --> 00:03:29,250 That's just taking exactly what we had before and turning 71 00:03:29,250 --> 00:03:31,870 it into C syntax. 72 00:03:31,870 --> 00:03:34,370 >> Now that we've translated our Boolean expression into code, 73 00:03:34,370 --> 00:03:36,160 how do we actually use it? 74 00:03:36,160 --> 00:03:38,170 Let's say we have some code that should only execute if a 75 00:03:38,170 --> 00:03:40,330 certain expression is true. 76 00:03:40,330 --> 00:03:42,750 For this purpose, pretty much all programming languages 77 00:03:42,750 --> 00:03:45,190 support the if condition. 78 00:03:45,190 --> 00:03:47,870 Let's say we have a Boolean variable, x, and we want some 79 00:03:47,870 --> 00:03:50,850 code to execute only if x is true. 80 00:03:50,850 --> 00:03:54,900 We would simply write the word if, put parentheses, and put 81 00:03:54,900 --> 00:03:57,800 the Boolean expression within those parentheses. 82 00:03:57,800 --> 00:03:59,680 After that, we wrap the code we want to 83 00:03:59,680 --> 00:04:01,080 execute in curly braces. 84 00:04:07,160 --> 00:04:08,150 What if there's some code you'd like to 85 00:04:08,150 --> 00:04:10,260 execute if x is not true? 86 00:04:10,260 --> 00:04:13,310 Simply write the word else after the if statement, wrap 87 00:04:13,310 --> 00:04:16,930 the other code in curly braces, and then that code will 88 00:04:16,930 --> 00:04:18,399 execute if x is not true. 89 00:04:26,640 --> 00:04:29,840 >> Another useful language construct is else if. 90 00:04:29,840 --> 00:04:32,210 Suppose there are two Booleans you would like to consider, 91 00:04:32,210 --> 00:04:34,330 let's call them x and y. 92 00:04:34,330 --> 00:04:37,340 We declare these variables to be true and false. 93 00:04:37,340 --> 00:04:40,540 If x and y are true, you execute the first block of 94 00:04:40,540 --> 00:04:42,630 code within those curly braces. 95 00:04:42,630 --> 00:04:46,470 Else if x or y are true, you execute the next block of 96 00:04:46,470 --> 00:04:50,590 code, and else you execute the last block of code. 97 00:04:50,590 --> 00:04:52,650 Working with Boolean values like this is useful, but 98 00:04:52,650 --> 00:04:55,750 you're really only limited to a few conditions. 99 00:04:55,750 --> 00:04:58,400 Booleans can become much more powerful when you introduce 100 00:04:58,400 --> 00:04:59,900 comparisons. 101 00:04:59,900 --> 00:05:01,280 These are ways to compare values that are 102 00:05:01,280 --> 00:05:03,300 not originally Boolean. 103 00:05:03,300 --> 00:05:06,060 To see if two values are the same, you can use equals 104 00:05:06,060 --> 00:05:09,340 equals, which is true if they're equal and false if 105 00:05:09,340 --> 00:05:10,530 they are not. 106 00:05:10,530 --> 00:05:15,360 Other common comparisons are less than, greater than, less 107 00:05:15,360 --> 00:05:19,740 than or equal to, and greater than or equal to. 108 00:05:19,740 --> 00:05:22,220 >> Everything I've covered so far has been pretty abstract, so 109 00:05:22,220 --> 00:05:24,320 let's introduce these comparisons in one last 110 00:05:24,320 --> 00:05:25,850 concrete example. 111 00:05:25,850 --> 00:05:27,130 Suppose there are two variables, 112 00:05:27,130 --> 00:05:29,430 temperature and isHungry. 113 00:05:29,430 --> 00:05:31,560 Temperature is a floating point number, so it can have 114 00:05:31,560 --> 00:05:33,090 decimal places. 115 00:05:33,090 --> 00:05:35,440 You're programming a very simple application which tells 116 00:05:35,440 --> 00:05:38,270 someone what to eat depending on the temperature. 117 00:05:38,270 --> 00:05:41,010 If you're hungry, AND AND the temperature is greater than or 118 00:05:41,010 --> 00:05:45,060 equal to 100, you can print eat ice cream. 119 00:05:45,060 --> 00:05:48,370 Else if you're hungry AND AND the temperature is less than 120 00:05:48,370 --> 00:05:52,420 or equal to zero, you can printf("eat spicy food"). 121 00:05:52,420 --> 00:05:55,200 Lastly, if you're not hungry at all, you can print "don't 122 00:05:55,200 --> 00:05:56,710 eat anything." 123 00:05:56,710 --> 00:06:00,130 >> I am Vipul Shekhawat, and this is CS50.