1 00:00:00,000 --> 00:00:07,600 [No Audio] 2 00:00:07,601 --> 00:00:09,833 Welcome back again in the course. We will 3 00:00:09,834 --> 00:00:12,000 complete our discussion of the if statement 4 00:00:12,001 --> 00:00:14,400 in this tutorial, by learning the remainder of 5 00:00:14,401 --> 00:00:16,633 its variants. So, let's get started. 6 00:00:17,533 --> 00:00:20,300 The first variant of the basic if statement we 7 00:00:20,301 --> 00:00:23,033 will learn is, the nested if statement. By 8 00:00:23,034 --> 00:00:25,600 nested, we mean an if statement which contains 9 00:00:25,633 --> 00:00:28,700 another if statement inside it. Let us see 10 00:00:28,701 --> 00:00:31,733 the general syntax for the nested if variant. 11 00:00:31,734 --> 00:00:48,733 [No Audio] 12 00:00:48,734 --> 00:00:51,233 The outer conditions are being checked first, 13 00:00:51,533 --> 00:00:53,700 if it happens to be true, then the statement 14 00:00:53,701 --> 00:00:56,400 included inside it's respective body will get 15 00:00:56,401 --> 00:00:59,533 a chance to execute. When both the outer and 16 00:00:59,534 --> 00:01:02,100 inner conditions are true, then the statement 17 00:01:02,101 --> 00:01:04,500 inside the inner if statement will also get a 18 00:01:04,501 --> 00:01:07,733 chance to execute. Please note that the outer 19 00:01:07,734 --> 00:01:09,700 and inner if statements may have the 20 00:01:09,701 --> 00:01:11,700 respective else part also. 21 00:01:13,233 --> 00:01:14,566 Let us do a quick example 22 00:01:14,567 --> 00:01:17,233 on this, where we will take an input 23 00:01:17,234 --> 00:01:19,700 from the user, which will be checked for being 24 00:01:19,701 --> 00:01:22,900 zero or not. If it is not zero, then we will 25 00:01:22,901 --> 00:01:25,633 check if it is being odd or even. We will 26 00:01:25,634 --> 00:01:27,700 assume that the number zero is neither odd 27 00:01:27,733 --> 00:01:30,500 nor even. Let us code this problem. 28 00:01:31,700 --> 00:01:33,900 Next I will add a suitable display message, so 29 00:01:33,901 --> 00:01:36,000 that the user knows what the program is 30 00:01:36,001 --> 00:01:37,466 expecting as an input. 31 00:01:37,467 --> 00:01:44,500 [No Audio] 32 00:01:44,501 --> 00:01:47,233 First we will take an integer input from the user. 33 00:01:47,234 --> 00:02:01,533 [No Audio] 34 00:02:01,534 --> 00:02:03,700 Next we will check if the value is zero or 35 00:02:03,701 --> 00:02:05,466 not, using an if statement. 36 00:02:05,467 --> 00:02:11,000 [No Audio] 37 00:02:11,001 --> 00:02:13,333 The same statement may be written as not, 38 00:02:13,334 --> 00:02:16,266 and then some_num == 0. 39 00:02:16,267 --> 00:02:19,900 [No Audio] 40 00:02:19,901 --> 00:02:21,800 Inside the body of the if statement, I 41 00:02:21,801 --> 00:02:24,400 will add additional conditions 42 00:02:24,401 --> 00:02:26,533 to check if the number is even or odd. 43 00:02:26,534 --> 00:02:34,433 [No Audio] 44 00:02:34,434 --> 00:02:37,300 The percent operator is also called as mod 45 00:02:37,301 --> 00:02:40,800 operator, and it computes the remainder 46 00:02:40,801 --> 00:02:42,700 after dividing the number mentioned to the 47 00:02:42,701 --> 00:02:44,433 left of it, by the number mentioned to the 48 00:02:44,434 --> 00:02:47,100 right of it. In this case, it will divide the 49 00:02:47,101 --> 00:02:49,500 value of some_num by 2, and will 50 00:02:49,501 --> 00:02:51,900 compute the remainder. If the remainder 51 00:02:51,901 --> 00:02:54,500 happens to have a value of 0, then it will 52 00:02:54,501 --> 00:02:56,600 be an indication that the number is even, and 53 00:02:56,601 --> 00:02:58,600 if the remainder is not zero then the number 54 00:02:58,601 --> 00:03:01,900 will be odd. If the outer if condition is 55 00:03:01,901 --> 00:03:04,000 true, the inner if condition will be checked. 56 00:03:04,333 --> 00:03:06,600 Based on whether the associated inner if 57 00:03:06,601 --> 00:03:08,733 condition is true or not, we will either 58 00:03:08,734 --> 00:03:12,966 execute the inner if or, or its associated else parts. 59 00:03:13,200 --> 00:03:15,300 For the outer if statement, we may have 60 00:03:15,301 --> 00:03:18,800 an optional else part also. We may add a 61 00:03:18,801 --> 00:03:20,700 suitable print statement to the else part to 62 00:03:20,701 --> 00:03:23,333 indicate that the number is 0, and it is 63 00:03:23,334 --> 00:03:24,866 therefore neither even or odd. 64 00:03:24,867 --> 00:03:27,400 [No Audio] 65 00:03:27,401 --> 00:03:28,900 Let us cargo run this. 66 00:03:28,901 --> 00:03:31,666 [No Audio] 67 00:03:31,667 --> 00:03:34,000 I will provide a value of 13, it 68 00:03:34,001 --> 00:03:36,400 displays that the number is odd, okay great. 69 00:03:36,933 --> 00:03:38,733 It is a good programming practice to 70 00:03:38,800 --> 00:03:41,500 properly indent the inner if statement 71 00:03:41,501 --> 00:03:43,700 and its respective body, so that the code is 72 00:03:43,733 --> 00:03:47,300 easily readable. Let us now learn the if let 73 00:03:47,301 --> 00:03:49,533 syntax variant of the if statement. 74 00:03:49,534 --> 00:03:54,400 [No Audio] 75 00:03:54,401 --> 00:03:56,733 With the help of this variant, we are able to assign a 76 00:03:56,734 --> 00:03:58,933 value to some variable based on the body of 77 00:03:58,934 --> 00:04:00,966 the if statement. Let us look at the 78 00:04:00,967 --> 00:04:02,300 general syntax first. 79 00:04:02,301 --> 00:04:12,400 [No Audio] 80 00:04:12,401 --> 00:04:15,233 Inside the body of the if and the else parts, 81 00:04:15,500 --> 00:04:17,700 we need to have a single value or statement, 82 00:04:17,701 --> 00:04:20,100 after which there is no semicolon, which will 83 00:04:20,101 --> 00:04:22,600 be the value that will be assigned to the 84 00:04:22,601 --> 00:04:25,300 variable_name variable. Finally, 85 00:04:25,301 --> 00:04:27,500 we will need to put a semicolon at the end of 86 00:04:27,501 --> 00:04:30,500 the complete block of the if else statements. 87 00:04:31,600 --> 00:04:33,200 Let us look at this variant in action. 88 00:04:34,600 --> 00:04:37,533 I will declare a variable, a value, which will store 89 00:04:37,534 --> 00:04:39,366 the result of the if else statement. 90 00:04:39,367 --> 00:04:43,933 [No Audio] 91 00:04:43,934 --> 00:04:47,133 The condition associated with the if statement, in this case 92 00:04:47,134 --> 00:04:49,500 is true, which means that the body of the if 93 00:04:49,501 --> 00:04:51,900 statement will be executed. And therefore the 94 00:04:51,901 --> 00:04:54,400 value 1 will be assigned to the variable value, 95 00:04:54,401 --> 00:04:56,033 let us execute this. 96 00:04:56,034 --> 00:05:02,533 [No Audio] 97 00:05:02,534 --> 00:05:03,800 The value is being displayed 98 00:05:03,801 --> 00:05:05,300 for the variable. Please note 99 00:05:05,301 --> 00:05:07,600 that the return value from the if part is 100 00:05:07,601 --> 00:05:10,200 without a semicolon, and the value returned 101 00:05:10,201 --> 00:05:12,733 from the else part is also without a 102 00:05:12,734 --> 00:05:15,966 semicolon, and both of these values are of the same type. 103 00:05:16,200 --> 00:05:17,600 One of these values will be 104 00:05:17,601 --> 00:05:19,900 assigned to the variable value, and we know 105 00:05:19,901 --> 00:05:22,500 that a variable in Rust should have a defined 106 00:05:22,501 --> 00:05:24,733 type at compile time, otherwise the compiler 107 00:05:24,734 --> 00:05:27,600 will complain. If for instance, we change the 108 00:05:27,601 --> 00:05:30,233 value of 2 in the else part to that of 2.0 109 00:05:30,533 --> 00:05:32,700 and save. So the compiler will rightfully 110 00:05:32,701 --> 00:05:35,100 complain, because it will be confused 111 00:05:35,101 --> 00:05:38,266 regarding the data type of the variable value. 112 00:05:38,267 --> 00:05:40,433 Now, let us look at another scenario, 113 00:05:40,500 --> 00:05:44,100 or variant of the if let syntax. We can use the 114 00:05:44,101 --> 00:05:47,733 same syntax in the if-else ladder, that we 115 00:05:47,734 --> 00:05:49,666 have seen in the previous tutorial. 116 00:05:49,667 --> 00:05:52,733 Let us see how to use if let variant in the 117 00:05:52,734 --> 00:05:54,633 context of if-else ladder. 118 00:05:56,300 --> 00:05:57,566 We will use the same program 119 00:05:57,567 --> 00:05:59,100 that we saw in the previous tutorial, 120 00:05:59,101 --> 00:06:00,800 and we will modify it. 121 00:06:00,966 --> 00:06:03,066 Instead of rewriting it, I will paste it. 122 00:06:03,067 --> 00:06:10,800 [No Audio] 123 00:06:10,801 --> 00:06:12,600 Now instead of assigning and setting the 124 00:06:12,601 --> 00:06:14,933 value of the variable grade, inside each of 125 00:06:14,934 --> 00:06:17,600 the blocks, I will assign it using the 126 00:06:17,633 --> 00:06:19,500 if let syntax by declaring the grade 127 00:06:19,501 --> 00:06:21,700 variable at the start and then I will mention 128 00:06:21,701 --> 00:06:22,866 all the conditions. 129 00:06:22,867 --> 00:06:25,300 [No Audio] 130 00:06:25,301 --> 00:06:27,300 Inside each of the block, I 131 00:06:27,301 --> 00:06:29,500 will remove the assignment and we'll just 132 00:06:29,501 --> 00:06:31,133 keep the character letters. 133 00:06:31,134 --> 00:06:33,733 Let us do the remaining necessary changes. 134 00:06:33,734 --> 00:06:44,400 [No Audio] 135 00:06:44,401 --> 00:06:46,633 You may note, let the code is now more 136 00:06:46,634 --> 00:06:49,600 reduced and therefore more readable and compact. 137 00:06:49,601 --> 00:06:51,400 Please note that, since we need to 138 00:06:51,401 --> 00:06:53,433 make an assignment to the variable, so we 139 00:06:53,434 --> 00:06:55,333 cannot skip the else part in this case, 140 00:06:55,334 --> 00:06:58,400 because, that will leave the variable empty. 141 00:06:58,600 --> 00:07:00,833 The point to remember is that, with the 142 00:07:00,834 --> 00:07:04,100 if let syntax, we cannot leave a variable empty. 143 00:07:04,233 --> 00:07:05,933 Finally we also need to put the 144 00:07:05,934 --> 00:07:07,566 mandatory semicolon. 145 00:07:07,567 --> 00:07:10,033 [No Audio] 146 00:07:10,034 --> 00:07:11,566 Okay let us execute. 147 00:07:11,567 --> 00:07:18,300 [No Audio] 148 00:07:18,301 --> 00:07:20,100 You may note, that the variable has been 149 00:07:20,101 --> 00:07:22,800 assigned a value, based on the marks of the student. 150 00:07:23,100 --> 00:07:25,200 The important point to note is that, 151 00:07:25,400 --> 00:07:27,400 we should have at least one block which 152 00:07:27,401 --> 00:07:29,200 returns some value for the variable. 153 00:07:29,700 --> 00:07:32,200 With this we end this tutorial, we have learned 154 00:07:32,201 --> 00:07:34,600 additional two ways in which the if statement 155 00:07:34,601 --> 00:07:38,533 can be used, that is the nested if and the if let syntax. 156 00:07:38,534 --> 00:07:40,200 In the upcoming tutorial, we will 157 00:07:40,201 --> 00:07:42,233 be looking at a very similar but different 158 00:07:42,234 --> 00:07:45,000 construct called match. Which will also work 159 00:07:45,001 --> 00:07:47,900 very similarly, but has some essential differences also. 160 00:07:48,333 --> 00:07:49,533 Do come back for covering that 161 00:07:49,534 --> 00:07:52,466 and until next tutorial, enjoy Rust programming. 162 00:07:52,467 --> 00:07:57,633 [No Audio]