1 00:00:01,080 --> 00:00:04,350 ‫-: Hi. Within this lecture we are going to dive 2 00:00:04,350 --> 00:00:06,660 ‫into the If statements, 3 00:00:06,660 --> 00:00:10,500 ‫because that's why we have spent so much time 4 00:00:10,500 --> 00:00:13,860 ‫on these operands, like greater than, less than or equal to, 5 00:00:13,860 --> 00:00:16,470 ‫not equal to, and or. 6 00:00:16,470 --> 00:00:20,280 ‫Because most of the time in programming we we're gonna go 7 00:00:20,280 --> 00:00:22,590 ‫for if statements. 8 00:00:22,590 --> 00:00:26,370 ‫Because most of the time we want to tell 9 00:00:26,370 --> 00:00:30,030 ‫to software to do this if something happens, 10 00:00:30,030 --> 00:00:33,900 ‫like if X is actually less than Y, 11 00:00:33,900 --> 00:00:36,090 ‫then you're gonna do this. 12 00:00:36,090 --> 00:00:39,810 ‫But else, if it's not, then you're gonna do that. 13 00:00:39,810 --> 00:00:42,810 ‫For example, over here we are gonna check to see 14 00:00:42,810 --> 00:00:46,500 ‫if X is actually less than Y. 15 00:00:46,500 --> 00:00:49,500 ‫And we have some complicated codes over here 16 00:00:49,500 --> 00:00:52,798 ‫but the current value of X is three, Y is four 17 00:00:52,798 --> 00:00:56,250 ‫and Zed is five. 18 00:00:56,250 --> 00:01:00,330 ‫And over here, beware of the curly braces. 19 00:01:00,330 --> 00:01:04,200 ‫Like we have a lot of curly braces right now. 20 00:01:04,200 --> 00:01:09,180 ‫And in the If block, I opened a new curly brace 21 00:01:09,180 --> 00:01:13,200 ‫and it means it has its own coding block. 22 00:01:13,200 --> 00:01:18,200 ‫Now if I write something over here, like Y is bigger, okay, 23 00:01:19,590 --> 00:01:24,590 ‫it will run because X is actually less than Y. 24 00:01:25,020 --> 00:01:30,020 ‫But this will run only, and only if, X is less than Y. 25 00:01:30,480 --> 00:01:35,480 ‫Now, if I change X to be 30, it won't be run as you can see 26 00:01:35,640 --> 00:01:39,900 ‫because X is not less than Y anymore. 27 00:01:39,900 --> 00:01:43,320 ‫So this will get run, this execution block, 28 00:01:43,320 --> 00:01:47,400 ‫this coding block, inside of the curly braces, 29 00:01:47,400 --> 00:01:52,400 ‫will only get run if, and only if, the condition holds. 30 00:01:53,610 --> 00:01:56,220 ‫That's how we leverage If statements. 31 00:01:56,220 --> 00:01:59,490 ‫We write If, and inside of the parenthesis 32 00:01:59,490 --> 00:02:03,237 ‫we write the condition, and then we open a coding block 33 00:02:03,237 --> 00:02:06,000 ‫and inside of that coding block, we write 34 00:02:06,000 --> 00:02:09,810 ‫whatever we want to do, if that condition holds. 35 00:02:09,810 --> 00:02:13,260 ‫But if it doesn't hold, it won't get run 36 00:02:13,260 --> 00:02:16,590 ‫or it won't be executed no matter what. 37 00:02:16,590 --> 00:02:19,410 ‫For example, we could have downloaded something 38 00:02:19,410 --> 00:02:21,027 ‫from the internet. 39 00:02:21,027 --> 00:02:24,840 ‫We could have added a new animation or something. 40 00:02:24,840 --> 00:02:26,160 ‫It really doesn't matter. 41 00:02:26,160 --> 00:02:29,100 ‫You don't always have to print, find something out 42 00:02:29,100 --> 00:02:30,360 ‫of the log cat. 43 00:02:30,360 --> 00:02:33,780 ‫In real world, you control for a statement 44 00:02:33,780 --> 00:02:37,500 ‫and if that holds, you do whatever you want to do. 45 00:02:37,500 --> 00:02:42,500 ‫So if X is less than Y, we are gonna write, Y is bigger. 46 00:02:43,500 --> 00:02:45,330 ‫But what if it doesn't? 47 00:02:45,330 --> 00:02:50,250 ‫Maybe I want to have another condition like this, 48 00:02:50,250 --> 00:02:55,250 ‫else if maybe Y is less than X. Okay? 49 00:02:58,350 --> 00:03:01,740 ‫So if X is less than Y, 50 00:03:01,740 --> 00:03:04,860 ‫then I'm gonna print out Y is bigger. 51 00:03:04,860 --> 00:03:07,830 ‫So I'm closing the curly braces, 52 00:03:07,830 --> 00:03:10,680 ‫after I write system.out.println. 53 00:03:10,680 --> 00:03:14,400 ‫And I say else if Y is less than X 54 00:03:14,400 --> 00:03:17,910 ‫and I'm going to write this, X is bigger over here. 55 00:03:17,910 --> 00:03:22,833 ‫Now if I run this, then I'm gonna see X is bigger. 56 00:03:23,700 --> 00:03:24,930 ‫So this is cool. 57 00:03:24,930 --> 00:03:29,280 ‫I get to propose a statement and then afterwards 58 00:03:29,280 --> 00:03:33,903 ‫I get to say what if it doesn't hold as well? 59 00:03:34,830 --> 00:03:37,267 ‫But over here, as you can see 60 00:03:37,267 --> 00:03:39,480 ‫maybe we can come up with a point 61 00:03:39,480 --> 00:03:44,130 ‫that X is greater than Y and Y is greater than X. 62 00:03:44,130 --> 00:03:46,740 ‫But what if they're equal, actually? 63 00:03:46,740 --> 00:03:51,336 ‫Maybe we can add something for that as well, like this: 64 00:03:51,336 --> 00:03:54,510 ‫X is equal Y. 65 00:03:54,510 --> 00:03:58,020 ‫So as you can see, we can add as many 66 00:03:58,020 --> 00:04:01,860 ‫else if blocks as we want like this. 67 00:04:01,860 --> 00:04:03,420 ‫So let's make them equal. 68 00:04:03,420 --> 00:04:04,620 ‫I run this. 69 00:04:04,620 --> 00:04:05,453 ‫Here you go. 70 00:04:05,453 --> 00:04:07,680 ‫X is equal to Y. 71 00:04:07,680 --> 00:04:10,983 ‫Since they're all 30, now they're equal. 72 00:04:12,030 --> 00:04:17,030 ‫And we can actually propose something like this as well. 73 00:04:17,790 --> 00:04:20,700 ‫We can do an else. 74 00:04:20,700 --> 00:04:23,910 ‫So else means if this doesn't hold 75 00:04:23,910 --> 00:04:28,800 ‫and if neither of the as ifs do not hold as well, 76 00:04:28,800 --> 00:04:30,570 ‫then what's gonna happen? 77 00:04:30,570 --> 00:04:33,180 ‫So maybe you have like tens of thousands 78 00:04:33,180 --> 00:04:34,890 ‫of conditions over here. 79 00:04:34,890 --> 00:04:38,010 ‫Of course it wouldn't be effective, but just imagine 80 00:04:38,010 --> 00:04:41,370 ‫that you have a hundred else ifs over here. 81 00:04:41,370 --> 00:04:44,550 ‫So if none of them holds 82 00:04:44,550 --> 00:04:48,480 ‫then maybe you may want to do something as well. 83 00:04:48,480 --> 00:04:50,340 ‫Then you can use else. 84 00:04:50,340 --> 00:04:54,330 ‫If nothing holds in this if statement, if control 85 00:04:54,330 --> 00:04:57,450 ‫then we are going to print out this. 86 00:04:57,450 --> 00:05:00,930 ‫But if something holds, then it will print 87 00:05:00,930 --> 00:05:05,930 ‫out only the code inside of the related block. Okay? 88 00:05:07,200 --> 00:05:11,220 ‫So in those examples, since X is not less than Y 89 00:05:11,220 --> 00:05:16,220 ‫and Y is not less than X, it's printing out X equals Y. 90 00:05:16,903 --> 00:05:19,710 ‫So that's it for if statements. 91 00:05:19,710 --> 00:05:22,230 ‫It's really looks like we have basic one 92 00:05:22,230 --> 00:05:26,610 ‫but we are gonna use this a lot in the Android development. 93 00:05:26,610 --> 00:05:29,070 ‫So if you come to this point, very good. 94 00:05:29,070 --> 00:05:32,313 ‫Let's stop here and continue within the next lecture.