1 00:00:01,050 --> 00:00:02,010 ‫Instructor: Hi. 2 00:00:02,010 --> 00:00:04,650 ‫Within this lecture, we're going to learn about 3 00:00:04,650 --> 00:00:07,140 ‫statements and operators. 4 00:00:07,140 --> 00:00:11,250 ‫In fact, it's time to learn about if controls. 5 00:00:11,250 --> 00:00:15,150 ‫That is to say we can actually determine 6 00:00:15,150 --> 00:00:19,830 ‫what will happen if some condition holds in our code. 7 00:00:19,830 --> 00:00:22,230 ‫But before we learn that, 8 00:00:22,230 --> 00:00:25,200 ‫we have to understand some basic operations, 9 00:00:25,200 --> 00:00:30,200 ‫like greater than, less than, equal to, if not equal to, 10 00:00:31,200 --> 00:00:34,320 ‫some operations like this. 11 00:00:34,320 --> 00:00:38,010 ‫So within this lecture, we're going to learn about those. 12 00:00:38,010 --> 00:00:39,480 ‫And following, the next lecture, 13 00:00:39,480 --> 00:00:42,330 ‫we're going to deep dive into the if statements 14 00:00:42,330 --> 00:00:44,910 ‫and then loops. 15 00:00:44,910 --> 00:00:47,490 ‫So if you don't know what those terms are, 16 00:00:47,490 --> 00:00:50,193 ‫it's perfectly okay, you are about to learn it. 17 00:00:51,600 --> 00:00:56,600 ‫So I believe it's time to create a new Java class for us. 18 00:00:57,660 --> 00:01:00,180 ‫So if you're doing this with an activity 19 00:01:00,180 --> 00:01:04,650 ‫because of the errors that we have observed 20 00:01:04,650 --> 00:01:06,330 ‫in the previous lectures, 21 00:01:06,330 --> 00:01:09,000 ‫then you should create a new project. 22 00:01:09,000 --> 00:01:12,930 ‫But I'm going to add a new Java class from here 23 00:01:12,930 --> 00:01:17,820 ‫and I'm gonna call this Statements, okay? 24 00:01:17,820 --> 00:01:20,520 ‫So this is basically what we are going to do. 25 00:01:20,520 --> 00:01:24,960 ‫We're going to add our new main method over here 26 00:01:24,960 --> 00:01:27,120 ‫because, remember, this is our entry point 27 00:01:27,120 --> 00:01:31,080 ‫and this should be in every Java code 28 00:01:31,080 --> 00:01:33,780 ‫that we are trying to run over here. 29 00:01:33,780 --> 00:01:35,700 ‫So maybe you may want to pause the video 30 00:01:35,700 --> 00:01:38,190 ‫and try to write it on your own, 31 00:01:38,190 --> 00:01:39,453 ‫see if you remember. 32 00:01:40,320 --> 00:01:43,950 ‫So it says public static void main 33 00:01:43,950 --> 00:01:48,240 ‫and it takes some parameters called string array, 34 00:01:48,240 --> 00:01:52,080 ‫and we name that string array arguments, 35 00:01:52,080 --> 00:01:54,600 ‫and open curly braces. 36 00:01:54,600 --> 00:01:56,160 ‫Here we go. 37 00:01:56,160 --> 00:01:58,860 ‫Now, I believe it's good to start 38 00:01:58,860 --> 00:02:03,090 ‫with some mathematical concepts like adding or subtracting. 39 00:02:03,090 --> 00:02:04,260 ‫So in order to do that, 40 00:02:04,260 --> 00:02:08,160 ‫I'm gonna create a new integer and we will work on that. 41 00:02:08,160 --> 00:02:12,990 ‫So I'm gonna call this x and x will be five. 42 00:02:12,990 --> 00:02:17,340 ‫So what if I want to add something to this x? 43 00:02:17,340 --> 00:02:20,820 ‫Let me print this out so that we can see it, 44 00:02:20,820 --> 00:02:23,460 ‫it's current value over here. 45 00:02:23,460 --> 00:02:24,450 ‫Let me run this 46 00:02:24,450 --> 00:02:28,470 ‫so we can get rid of this old Logcat over here. 47 00:02:28,470 --> 00:02:30,240 ‫Yep, this is five. 48 00:02:30,240 --> 00:02:34,260 ‫So let me add something to this x, 49 00:02:34,260 --> 00:02:36,750 ‫like let me add one. 50 00:02:36,750 --> 00:02:41,750 ‫So we can do it this way, right, x is x + 1. 51 00:02:41,760 --> 00:02:46,760 ‫So if I print out x right now, I will get six. 52 00:02:47,490 --> 00:02:51,600 ‫First I get five, then I get six because I added one. 53 00:02:51,600 --> 00:02:54,000 ‫So that's basic, right? 54 00:02:54,000 --> 00:02:57,330 ‫But there is another annotation for this. 55 00:02:57,330 --> 00:03:02,070 ‫So if you come over here, you can just say x++ 56 00:03:02,070 --> 00:03:04,860 ‫and this will do the same thing, basically. 57 00:03:04,860 --> 00:03:08,160 ‫So I don't really actually use this a lot 58 00:03:08,160 --> 00:03:11,670 ‫but if you see something like this, x++, 59 00:03:11,670 --> 00:03:15,990 ‫then just know that it's adding one, like this. 60 00:03:15,990 --> 00:03:18,753 ‫As you can see, x is now seven. 61 00:03:19,590 --> 00:03:22,710 ‫So, it's basically the same thing by saying that 62 00:03:22,710 --> 00:03:25,443 ‫with saying that x is x + 1. 63 00:03:26,400 --> 00:03:29,490 ‫In fact, we can do the same thing for subtraction as well, 64 00:03:29,490 --> 00:03:30,890 ‫so you can say x--, 65 00:03:32,010 --> 00:03:35,250 ‫so it will just subtract one out of x. 66 00:03:35,250 --> 00:03:38,040 ‫So if I run this, we will get six 67 00:03:38,040 --> 00:03:41,340 ‫at the end of the Logcat, like this. 68 00:03:41,340 --> 00:03:42,450 ‫So here you go. 69 00:03:42,450 --> 00:03:46,620 ‫Now you see how to add one and subtract one. 70 00:03:46,620 --> 00:03:49,170 ‫So let me do something like this, 71 00:03:49,170 --> 00:03:51,620 ‫x is x multiplied by five. 72 00:03:51,620 --> 00:03:53,280 ‫Do we get the result? 73 00:03:53,280 --> 00:03:55,620 ‫Yep, we get the result. 74 00:03:55,620 --> 00:03:58,770 ‫So it's multiplied six by five 75 00:03:58,770 --> 00:04:01,653 ‫and the current value of x is 30. 76 00:04:02,700 --> 00:04:07,700 ‫So, these are some basic operands of mathematics in Java. 77 00:04:09,540 --> 00:04:13,770 ‫And most of the time we get more than value, right? 78 00:04:13,770 --> 00:04:18,540 ‫For example, we can have this y equal to four. 79 00:04:18,540 --> 00:04:22,080 ‫Now I have an x and I have a y, 80 00:04:22,080 --> 00:04:25,650 ‫so I can compare them if I want, right? 81 00:04:25,650 --> 00:04:29,880 ‫For example, I can just say x is greater than, 82 00:04:29,880 --> 00:04:31,620 ‫so this is greater than sign, 83 00:04:31,620 --> 00:04:34,623 ‫as you might already know, y. 84 00:04:35,700 --> 00:04:39,120 ‫So what if I say that, what will happen? 85 00:04:39,120 --> 00:04:40,590 ‫What is x right now? 86 00:04:40,590 --> 00:04:42,450 ‫X is 30 at this point, right? 87 00:04:42,450 --> 00:04:44,880 ‫So it's definitely greater than y 88 00:04:44,880 --> 00:04:48,030 ‫and we get a Boolean out of that. 89 00:04:48,030 --> 00:04:52,080 ‫Remember Booleans? Now we get a Boolean. 90 00:04:52,080 --> 00:04:57,080 ‫So we can do the same thing with this 91 00:04:57,240 --> 00:04:59,490 ‫and we will get a Boolean again, 92 00:04:59,490 --> 00:05:02,640 ‫but this time it will be false. 93 00:05:02,640 --> 00:05:06,180 ‫And this is kind of basis of the if controls 94 00:05:06,180 --> 00:05:09,060 ‫that we're going to see in the next lecture. 95 00:05:09,060 --> 00:05:13,110 ‫As you can see, when we compare things, we get Booleans. 96 00:05:13,110 --> 00:05:16,680 ‫For example, let me change y to 30, okay? 97 00:05:16,680 --> 00:05:20,430 ‫The current value of the x is 30 as well. 98 00:05:20,430 --> 00:05:24,390 ‫So if I do this, x is greater than y, 99 00:05:24,390 --> 00:05:27,480 ‫then we will get false. 100 00:05:27,480 --> 00:05:32,460 ‫Now if I do something like greater than or equal to, 101 00:05:32,460 --> 00:05:33,293 ‫like this, 102 00:05:33,293 --> 00:05:38,250 ‫so x is greater than or equal to y, 103 00:05:38,250 --> 00:05:40,830 ‫so greater than sign and equal sign. 104 00:05:40,830 --> 00:05:43,860 ‫Now if I run this, I will get true 105 00:05:43,860 --> 00:05:46,623 ‫because now they are both 30, 106 00:05:47,490 --> 00:05:49,860 ‫so that's good. 107 00:05:49,860 --> 00:05:54,090 ‫So how do we compare if they're actually equal or not? 108 00:05:54,090 --> 00:05:58,020 ‫So we do this, x equal to y 109 00:05:58,020 --> 00:06:01,770 ‫but with a double equal sign. 110 00:06:01,770 --> 00:06:04,620 ‫So if we make one equal sign, 111 00:06:04,620 --> 00:06:09,620 ‫it means that just make the current value of x equal to y, 112 00:06:10,320 --> 00:06:14,160 ‫but if we do it like this, ==, 113 00:06:14,160 --> 00:06:17,400 ‫it means that if they're equal or not. 114 00:06:17,400 --> 00:06:19,200 ‫And as you can see in this case, 115 00:06:19,200 --> 00:06:21,930 ‫they're actually equal to each other 116 00:06:21,930 --> 00:06:24,633 ‫so we get true out of this. 117 00:06:25,560 --> 00:06:26,970 ‫So that's cool. 118 00:06:26,970 --> 00:06:30,780 ‫So how do we define if they're not equal? 119 00:06:30,780 --> 00:06:35,460 ‫So we do it like this, exclamation point, 120 00:06:35,460 --> 00:06:38,236 ‫it means negative, okay? 121 00:06:38,236 --> 00:06:42,540 ‫!= means it's not equal. 122 00:06:42,540 --> 00:06:46,530 ‫So, since they are actually equal to each other, 123 00:06:46,530 --> 00:06:49,863 ‫we get a false out of this one, as you can see. 124 00:06:51,450 --> 00:06:54,060 ‫There're couple of other operands 125 00:06:54,060 --> 00:06:57,870 ‫or couple of other operations that you should know 126 00:06:57,870 --> 00:07:02,460 ‫and these are called AND and OR operations. 127 00:07:02,460 --> 00:07:06,870 ‫So maybe you have taken some kind of basic philosophy 128 00:07:06,870 --> 00:07:10,350 ‫or mathematical courses in high school 129 00:07:10,350 --> 00:07:13,650 ‫and maybe you have learned about this. 130 00:07:13,650 --> 00:07:16,020 ‫So I'm taking notes over here 131 00:07:16,020 --> 00:07:19,590 ‫so that you can see how it's written as well. 132 00:07:19,590 --> 00:07:23,010 ‫So this is AND and this is OR. 133 00:07:23,010 --> 00:07:26,040 ‫And this sign is a pipe sign. 134 00:07:26,040 --> 00:07:31,040 ‫You can do it by Alt and something like question mark 135 00:07:31,110 --> 00:07:33,660 ‫or the right next to it on your keyboard. 136 00:07:33,660 --> 00:07:34,830 ‫You're gonna have to try it 137 00:07:34,830 --> 00:07:38,370 ‫because it differs with international keyboards. 138 00:07:38,370 --> 00:07:42,330 ‫However, it's there in your keyboard, okay? 139 00:07:42,330 --> 00:07:46,200 ‫So this is pipe sign and it means OR 140 00:07:46,200 --> 00:07:49,470 ‫and this is and sign and it means AND. 141 00:07:49,470 --> 00:07:51,630 ‫So, how do we use them? 142 00:07:51,630 --> 00:07:56,220 ‫For example, if I say x is three AND y is four, 143 00:07:56,220 --> 00:08:01,220 ‫now if I create a new z over here with five, 144 00:08:01,380 --> 00:08:04,380 ‫then I can compare all three of them. 145 00:08:04,380 --> 00:08:09,380 ‫For example, what if I say x is less than y, is it true? 146 00:08:11,190 --> 00:08:13,140 ‫Yep, is it true, right? 147 00:08:13,140 --> 00:08:16,740 ‫It's true because x is three, y is four right now. 148 00:08:16,740 --> 00:08:18,360 ‫It gives me true. 149 00:08:18,360 --> 00:08:21,660 ‫And what if I say something like this, 150 00:08:21,660 --> 00:08:26,660 ‫x is less than y and I can also add 151 00:08:27,690 --> 00:08:31,530 ‫another condition over here with AND operand 152 00:08:31,530 --> 00:08:35,250 ‫and I can say y is less than z. 153 00:08:35,250 --> 00:08:39,030 ‫So is y is less than z? 154 00:08:39,030 --> 00:08:43,860 ‫Yep, it's less than z and x is less than z, 155 00:08:43,860 --> 00:08:46,650 ‫so it will give me true. 156 00:08:46,650 --> 00:08:50,250 ‫And since I have used this and sign, 157 00:08:50,250 --> 00:08:55,250 ‫it means that both side of this AND operand should be true 158 00:08:55,470 --> 00:08:58,440 ‫in order to be getting true. 159 00:08:58,440 --> 00:09:02,490 ‫So if either of these conditions were not true, 160 00:09:02,490 --> 00:09:07,490 ‫then we wouldn't get true out of this equation, okay? 161 00:09:07,950 --> 00:09:12,510 ‫Both sides should be true in order to get true. 162 00:09:12,510 --> 00:09:14,460 ‫For example, let me do it this way 163 00:09:14,460 --> 00:09:16,980 ‫and you will see we will get false, 164 00:09:16,980 --> 00:09:20,220 ‫even though x is actually less than y, 165 00:09:20,220 --> 00:09:23,550 ‫z is not less than y. 166 00:09:23,550 --> 00:09:28,320 ‫But if I do something like this with or sign, 167 00:09:28,320 --> 00:09:33,270 ‫then any of the condition should be true 168 00:09:33,270 --> 00:09:37,200 ‫and the other condition doesn't matter at all. 169 00:09:37,200 --> 00:09:40,770 ‫As you can see, since x is actually less than y, 170 00:09:40,770 --> 00:09:42,450 ‫I'm getting true. 171 00:09:42,450 --> 00:09:47,400 ‫So, even though it's not true, z is not less than y, 172 00:09:47,400 --> 00:09:51,480 ‫I'm getting true because I'm using OR. 173 00:09:51,480 --> 00:09:54,540 ‫So either one of these is true, 174 00:09:54,540 --> 00:09:57,750 ‫it's good enough for me, I'm gonna get true. 175 00:09:57,750 --> 00:10:02,130 ‫So this is the basic difference between AND and OR. 176 00:10:02,130 --> 00:10:05,820 ‫In the AND operand, you should have both sides true. 177 00:10:05,820 --> 00:10:08,760 ‫In the OR operand, either one of these sides 178 00:10:08,760 --> 00:10:11,070 ‫is very good for us. 179 00:10:11,070 --> 00:10:14,253 ‫So let's stop here and continue it in the next one.