1 00:00:00,320 --> 00:00:03,740 An if statement is a control structure that causes a block of 2 00:00:03,740 --> 00:00:06,640 code to run under certain conditions. 3 00:00:06,640 --> 00:00:09,824 It consists of a condition followed by a block of code and 4 00:00:09,824 --> 00:00:13,090 the code in the block is run only if the condition is true. 5 00:00:13,090 --> 00:00:14,996 So here we have an if statement and 6 00:00:14,996 --> 00:00:20,050 the condition is the literal boolean value trick, which of course evaluates the true. 7 00:00:20,050 --> 00:00:24,109 So the code here in the block does get executed and 8 00:00:24,109 --> 00:00:26,690 it prints out the string true. 9 00:00:27,810 --> 00:00:29,970 You can see that down here in the output. 10 00:00:29,970 --> 00:00:33,720 Here's another if statement, where the condition is a simple boolean value, 11 00:00:33,720 --> 00:00:35,280 it's the value false. 12 00:00:35,280 --> 00:00:39,050 And, of course, false is not true, so the code in the block does not run. 13 00:00:39,050 --> 00:00:42,280 You don't see false down here in the program output. 14 00:00:42,280 --> 00:00:46,150 Here's an if statement with an actual comparison operation. 15 00:00:46,150 --> 00:00:51,760 The result of is 1 less than 2 is true, therefore the code in the block runs and 16 00:00:51,760 --> 00:00:54,990 it prints out the string, 1 is less than 2. 17 00:00:54,990 --> 00:00:57,921 You can see that here in the output. 18 00:00:57,921 --> 00:01:01,240 And here's another comparison, is 1 greater than 2? 19 00:01:01,240 --> 00:01:05,090 Well, the result of that value is false and so the block does not run. 20 00:01:05,090 --> 00:01:09,640 The go language has many of the same boolean operators that other languages do. 21 00:01:09,640 --> 00:01:12,930 For example, there's the not operator, the exclamation point, 22 00:01:12,930 --> 00:01:16,370 which takes whatever boolean value it's given and negates it. 23 00:01:16,370 --> 00:01:20,270 So if it receives the value true, it will turn it into false. 24 00:01:20,270 --> 00:01:25,800 So, this if statement, the result is false and therefore, this code won't be run. 25 00:01:25,800 --> 00:01:29,160 But this one takes the value false and makes it true. 26 00:01:29,160 --> 00:01:31,440 With the result of true the code in the block does run, 27 00:01:31,440 --> 00:01:36,138 and you see that it prints not false down here. 28 00:01:36,138 --> 00:01:41,790 The operator looks to ensure that the value on both sides of it is true. 29 00:01:41,790 --> 00:01:47,190 So, here we have a value that's true on the left but a false value on the right, 30 00:01:47,190 --> 00:01:52,260 therefore this entire expression is false because both values are not true. 31 00:01:53,860 --> 00:01:58,340 However, down here, we have true on the left as well as true on the right 32 00:01:58,340 --> 00:02:03,000 therefore true double and true the result is true, and this code gets run. 33 00:02:03,000 --> 00:02:04,220 You can see it on the output. 34 00:02:09,105 --> 00:02:14,245 The operator with the two vertical bars here is read as or, it will return 35 00:02:14,245 --> 00:02:19,625 a true value if either the value on its left or the value on its right is true. 36 00:02:19,625 --> 00:02:23,252 So, since this first value here on the left is true, 37 00:02:23,252 --> 00:02:26,192 there's no need to even evaluate the value on the right. 38 00:02:26,192 --> 00:02:28,922 The results of this expression is true. 39 00:02:28,922 --> 00:02:32,794 And it prints this line down here in the output. 40 00:02:34,190 --> 00:02:36,510 The case is the same here. 41 00:02:36,510 --> 00:02:40,320 The value on the left is true and the value on the right is true, but 42 00:02:40,320 --> 00:02:41,180 that doesn't matter. 43 00:02:41,180 --> 00:02:45,400 If either one of those had been true, the result would've been true, and 44 00:02:45,400 --> 00:02:47,650 the code here in the if block would have run. 45 00:02:47,650 --> 00:02:49,420 And again, you can see it in the output. 46 00:02:50,550 --> 00:02:55,710 The else block goes after an if block, and it runs only if the if block doesn't run. 47 00:02:56,720 --> 00:02:59,910 So here we have an if block, and its condition is true. 48 00:02:59,910 --> 00:03:01,340 So it does run. 49 00:03:01,340 --> 00:03:04,860 It prints the expression if down here in the output. 50 00:03:04,860 --> 00:03:09,240 But what if we changed this to false so that it doesn't run? 51 00:03:09,240 --> 00:03:10,920 Let's try saving that, run this. 52 00:03:11,980 --> 00:03:15,220 Now that we've changed this to false the if block doesn't run so 53 00:03:15,220 --> 00:03:16,760 the else block does. 54 00:03:16,760 --> 00:03:19,100 And you see else printed out here on the output. 55 00:03:20,220 --> 00:03:22,510 You can also use if else blocks. 56 00:03:22,510 --> 00:03:26,252 So in this code, the condition in the if statement is true, and so 57 00:03:26,252 --> 00:03:28,854 the format print line if statement gets run. 58 00:03:28,854 --> 00:03:33,153 But if we were to change that to false, and re-run it, 59 00:03:33,153 --> 00:03:36,335 then the if statement doesn't get run. 60 00:03:36,335 --> 00:03:40,765 So it checks the if-else clause, which it sees is true. 61 00:03:40,765 --> 00:03:45,365 And because that's true, it will go ahead and run this block, printing else-if. 62 00:03:45,365 --> 00:03:47,291 If we were to change that to false as well. 63 00:03:47,291 --> 00:03:51,590 Save that, re-run it. 64 00:03:51,590 --> 00:03:53,010 Now the if clause is false. 65 00:03:53,010 --> 00:03:55,780 The else if clause is also false. 66 00:03:55,780 --> 00:04:00,260 And, therefore, it runs the else clause and prints else. 67 00:04:00,260 --> 00:04:04,850 Now as before, this code within the curly braces following the condition of an if 68 00:04:04,850 --> 00:04:06,010 statement is a block. 69 00:04:06,010 --> 00:04:11,590 And that means all the same rules regarding the scope for variables applies. 70 00:04:11,590 --> 00:04:15,160 So down here where we access the beforeIf variable, 71 00:04:15,160 --> 00:04:20,390 that's fine because the beforeIf variable gets declared before the if block here, 72 00:04:20,390 --> 00:04:22,940 and therefore it's still within scope afterwards. 73 00:04:22,940 --> 00:04:27,230 But here where we try to print within the if variable, 74 00:04:27,230 --> 00:04:32,320 that's not okay because it gets defined within the block for the if statement. 75 00:04:32,320 --> 00:04:35,570 And, therefore, it's out of scope down here. 76 00:04:35,570 --> 00:04:38,910 So, if we try to run this, we'll see a compile error, 77 00:04:38,910 --> 00:04:42,834 undefined within if, here on line 12.