1 00:00:00,360 --> 00:00:04,200 Logical operators can join many comparisons into one condition. 2 00:00:05,840 --> 00:00:12,380 In the last lesson, you set up a series of if elsea final statements, notice that each condition only 3 00:00:12,380 --> 00:00:13,670 has one comparison. 4 00:00:15,450 --> 00:00:19,170 In this lesson, you're going to connect many comparisons into one condition. 5 00:00:21,130 --> 00:00:25,690 The first thing I'll need you to do is create a new class by yourself inside the Section three project, 6 00:00:25,690 --> 00:00:31,150 create a new file named Logical Operators Java, and make sure the class has a main method. 7 00:00:35,930 --> 00:00:39,260 With a logical operator, we can compare many things at once. 8 00:00:40,790 --> 00:00:44,240 And the most common logical operators are or and end. 9 00:00:45,490 --> 00:00:50,140 We're going to start with the owner operator and this one returns true if either comparison is true. 10 00:00:51,960 --> 00:00:54,250 And there are three parts to the owner operator. 11 00:00:54,630 --> 00:01:00,210 There's the first comparison, and then there's the owner operator represented by two vertical lines 12 00:01:00,630 --> 00:01:04,610 such that if any of the comparisons are true, the entire condition becomes true. 13 00:01:05,250 --> 00:01:07,020 And then there's the second comparison. 14 00:01:08,170 --> 00:01:11,020 So what are some use cases for the or operator? 15 00:01:12,430 --> 00:01:17,860 Well, think about this, you're applying for a scholarship and to get the scholarship you need above 16 00:01:17,860 --> 00:01:21,120 a seventy five in chemistry or in English. 17 00:01:21,820 --> 00:01:23,920 So we're going to make two integer variables. 18 00:01:23,920 --> 00:01:27,220 Chemistry grade set that equal to seventy eight. 19 00:01:29,520 --> 00:01:32,520 An English grade return is set equal to sixty five. 20 00:01:39,040 --> 00:01:43,510 Now we're going to make if statement where they condition and inside the condition, we're going to 21 00:01:43,510 --> 00:01:50,920 check if chemistry great is greater than 75, did they get above a 75 in chemistry or we'll check if 22 00:01:50,920 --> 00:01:53,380 English grade is bigger than 75. 23 00:02:06,260 --> 00:02:10,970 And if either one of these comparisons is true, the entire condition is going to be true, in which 24 00:02:10,970 --> 00:02:12,350 case we're going to print. 25 00:02:16,300 --> 00:02:17,500 Congratulations. 26 00:02:21,670 --> 00:02:23,290 You got the scholarship. 27 00:02:26,780 --> 00:02:28,220 Otherwise, we're going to print. 28 00:02:32,210 --> 00:02:33,620 You didn't get the scholarship. 29 00:02:43,110 --> 00:02:44,910 All right, I think we're ready to run our coat. 30 00:02:55,900 --> 00:02:58,870 And the condition is true, you did get the scholarship. 31 00:03:00,930 --> 00:03:06,780 The or operator checks, if one of the comparisons is true, only one of them has to be true, and the 32 00:03:06,780 --> 00:03:11,970 first comparison is to the entire condition is true and the code runs. 33 00:03:15,270 --> 00:03:18,180 Now we're going to change the students chemistry grade to 67. 34 00:03:21,270 --> 00:03:22,590 We're going to rerun the code. 35 00:03:26,450 --> 00:03:29,150 And this time the code inside Elsa runs. 36 00:03:31,360 --> 00:03:36,790 None of the comparisons are true, so the condition is false, so Jarvis skips over and runs the code 37 00:03:36,790 --> 00:03:37,800 inside ELT's. 38 00:03:42,340 --> 00:03:44,560 You can combine more than two comparisons. 39 00:03:45,850 --> 00:03:52,000 You can combine three comparisons into one condition for comparisons, five comparisons, as many as 40 00:03:52,000 --> 00:03:56,650 you want, as long as one of them is true, the entire condition is going to be true. 41 00:03:57,750 --> 00:04:00,780 And so back in coat, let's say the school decided to be generous. 42 00:04:01,020 --> 00:04:03,600 They're also going to offer you a scholarship if you know Java. 43 00:04:04,170 --> 00:04:07,500 So we're going to make a string variable and set it equal to Java. 44 00:04:12,540 --> 00:04:17,130 And now we're going to add another comparison to the condition, it's going to check if the variable 45 00:04:17,130 --> 00:04:20,720 equals Java, does the student know the Java programming language? 46 00:04:23,910 --> 00:04:25,050 Rewriting our code. 47 00:04:31,740 --> 00:04:33,840 And we got our scholarship back. 48 00:04:35,990 --> 00:04:38,900 The or operator checks if one of the comparisons is true. 49 00:04:39,830 --> 00:04:41,100 First one is false. 50 00:04:41,150 --> 00:04:47,300 The second one is false, but the last one is true to the entire condition is true and the code inside 51 00:04:47,300 --> 00:04:47,960 runs. 52 00:04:52,860 --> 00:04:54,840 Now we're going to talk about the end operator. 53 00:04:58,980 --> 00:05:00,780 And there are three parts to it as well. 54 00:05:01,050 --> 00:05:06,960 There is the first comparison, there's the end operator, which is a pair of ampersands and the second 55 00:05:06,960 --> 00:05:07,590 comparison. 56 00:05:08,310 --> 00:05:13,130 And with the end operation, the condition is only true if every comparison is true. 57 00:05:13,920 --> 00:05:16,330 In this case, the first comparison is false. 58 00:05:16,350 --> 00:05:18,930 So the entire condition becomes false. 59 00:05:20,850 --> 00:05:23,160 So what are some use cases for the end operator? 60 00:05:23,700 --> 00:05:24,870 Well, think about this. 61 00:05:25,080 --> 00:05:31,590 To get your diploma, you need at least 40 credits and a minimum GPA of 2.0. 62 00:05:32,220 --> 00:05:33,890 Now, credits are whole numbers. 63 00:05:33,900 --> 00:05:38,820 So we're going to make it into variable credits and we're going to set that equal to fifty six. 64 00:05:39,360 --> 00:05:41,390 And GPA is a decimal measure. 65 00:05:41,400 --> 00:05:46,080 So we're going to make a double variable GPA and set it equal to three point two. 66 00:05:47,390 --> 00:05:49,050 Now we're going to make another statement. 67 00:05:49,190 --> 00:05:55,310 First, we're going to check if a student has at least 40 credits, credits greater than or equal to 68 00:05:55,310 --> 00:06:01,100 40 and will check if they have a GPA of at least two point zero. 69 00:06:07,180 --> 00:06:12,400 And if the student satisfies both requirements, if both of these comparisons are true, we're going 70 00:06:12,400 --> 00:06:14,710 to print you earned your diploma. 71 00:06:26,880 --> 00:06:31,920 We're sorry, you need at least 40 credits and a minimum GPA of 2.0. 72 00:06:41,460 --> 00:06:43,050 All right, we're ready to run our code. 73 00:06:46,200 --> 00:06:51,630 The condition is indeed true, they got their diploma, the end operator checks, if every comparison 74 00:06:51,630 --> 00:06:56,370 is true, the student has about 40 credits and has a high enough GPA. 75 00:06:56,790 --> 00:06:59,880 So the entire condition is true and the coda runs. 76 00:07:04,370 --> 00:07:07,430 What if we change the students GPA to one point for. 77 00:07:08,450 --> 00:07:09,620 Rewriting our code. 78 00:07:14,330 --> 00:07:15,940 This time, they don't get their diploma. 79 00:07:19,250 --> 00:07:22,050 The end operator needs both comparisons to be true. 80 00:07:22,550 --> 00:07:28,890 The student has more than 40 credits, but there is too low, so the condition is false in the code 81 00:07:28,930 --> 00:07:29,480 runs. 82 00:07:33,480 --> 00:07:36,600 In this lesson, you connected many comparisons to one condition. 83 00:07:38,280 --> 00:07:44,340 The owner operator returns true if either comparison is true now, a student had a seven eight in chemistry 84 00:07:44,340 --> 00:07:49,830 and a sixty five in English, and you made a condition that checks if chemistry grade is greater than 85 00:07:49,830 --> 00:07:54,650 75 or in English, and the first comparison is true. 86 00:07:54,930 --> 00:07:58,860 And whether or not the second one is false or true, it doesn't matter. 87 00:07:59,010 --> 00:08:03,210 The entire condition becomes true and the code inside runs. 88 00:08:05,600 --> 00:08:11,360 Now, the end operator is different, it only returns true if every comparison, if every expression 89 00:08:11,360 --> 00:08:11,780 is true. 90 00:08:13,250 --> 00:08:19,790 Now, a student had 56 credits, but a GPA of one point four and you made a condition that checks if 91 00:08:19,790 --> 00:08:24,650 a student scored above 40 credits and had a minimum GPA of 2.0. 92 00:08:25,810 --> 00:08:29,950 The end operator needs both comparisons to be true, but one of them is false. 93 00:08:30,490 --> 00:08:36,039 This means that the entire condition is false and Java skips over to the L statements.