1 00:00:00,000 --> 00:00:08,700 [No Audio] 2 00:00:08,701 --> 00:00:10,400 Hello and a warm welcome again 3 00:00:10,401 --> 00:00:11,866 in the new section of the course. 4 00:00:12,033 --> 00:00:13,860 This section intends to cover the 5 00:00:13,861 --> 00:00:15,660 basic programming constructs called the 6 00:00:15,661 --> 00:00:18,180 control structures, that are part of almost 7 00:00:18,210 --> 00:00:21,030 all programming languages. control structures 8 00:00:21,060 --> 00:00:23,760 are the constructs, that analyze variables and 9 00:00:23,761 --> 00:00:25,710 choose directions in which to execute the 10 00:00:25,711 --> 00:00:29,040 code, based on some parameters. The basic 11 00:00:29,041 --> 00:00:31,200 control structures in programming languages 12 00:00:31,201 --> 00:00:34,320 are conditionals. Conditional statements or 13 00:00:34,321 --> 00:00:37,170 conditional expressions are also sometimes 14 00:00:37,171 --> 00:00:39,210 referred to as conditional constructs or 15 00:00:39,211 --> 00:00:41,190 programming language commands for handling 16 00:00:41,191 --> 00:00:43,066 decisions inside the program. 17 00:00:44,266 --> 00:00:47,310 The first, and very basic type of conditional construct, 18 00:00:47,311 --> 00:00:49,530 which is used in almost all the programming 19 00:00:49,531 --> 00:00:52,050 languages, is the If conditional statement. 20 00:00:52,470 --> 00:00:54,810 This tutorial will discuss this in detail, so 21 00:00:54,811 --> 00:00:57,166 let's start with today's learning agenda. 22 00:00:57,921 --> 00:01:00,621 The first question is, when the if statement is 23 00:01:00,622 --> 00:01:03,570 being used? We use it when we need to execute 24 00:01:03,571 --> 00:01:06,420 a block of statements or code segment, when a 25 00:01:06,421 --> 00:01:09,360 certain condition is being true. Let us look 26 00:01:09,361 --> 00:01:11,700 at the general syntax for the if statement, 27 00:01:11,701 --> 00:01:13,500 which will make things more easy to 28 00:01:13,501 --> 00:01:15,160 understand and comprehend. 29 00:01:15,161 --> 00:01:23,099 [No Audio] 30 00:01:23,100 --> 00:01:25,199 The statements inside the curly brackets, are 31 00:01:25,200 --> 00:01:27,689 referred to as the body of the if statement, 32 00:01:28,049 --> 00:01:30,029 and they will only execute if the given 33 00:01:30,030 --> 00:01:32,339 condition in front of the if statement 34 00:01:32,369 --> 00:01:35,519 evaluates to a value of true. In case the 35 00:01:35,520 --> 00:01:37,889 condition is false, then the statement inside 36 00:01:37,890 --> 00:01:40,319 the if are skipped, and the remaining code 37 00:01:40,320 --> 00:01:42,629 after the ending curly brackets, are executed 38 00:01:42,630 --> 00:01:46,679 as normal. Let us have some examples. I will 39 00:01:46,680 --> 00:01:48,899 declare a variable some_number and 40 00:01:48,900 --> 00:01:50,487 will assign to it a value of 40. 41 00:01:50,488 --> 00:01:53,166 [No Audio] 42 00:01:53,167 --> 00:01:56,219 Next I will check, if the value of some_number 43 00:01:56,220 --> 00:01:57,533 is less than 50. 44 00:01:57,534 --> 00:02:02,730 [No Audio] 45 00:02:02,731 --> 00:02:04,500 If it is true, then I will display the 46 00:02:04,501 --> 00:02:06,688 message, the number is less than 50. 47 00:02:06,689 --> 00:02:13,170 [No Audio] 48 00:02:13,171 --> 00:02:15,960 Outside the body of the if statement, I will 49 00:02:15,961 --> 00:02:17,580 add another print statement which will 50 00:02:17,581 --> 00:02:19,666 execute, irrespective of the condition 51 00:02:19,667 --> 00:02:20,859 in the if statement. 52 00:02:20,860 --> 00:02:26,933 [No Audio] 53 00:02:26,940 --> 00:02:28,860 In this case, now since the condition in the 54 00:02:28,861 --> 00:02:30,930 if statement will be true, as the value of the 55 00:02:30,931 --> 00:02:33,660 variable is less than 50. Therefore, the 56 00:02:33,690 --> 00:02:35,820 print statement inside the if statement, will 57 00:02:35,821 --> 00:02:38,670 be executed. The statement outside the if 58 00:02:38,671 --> 00:02:41,460 statement, since it is not associated with any 59 00:02:41,461 --> 00:02:44,300 condition, so this statement will also execute. 60 00:02:44,933 --> 00:02:46,718 Let us Cargo run to confirm. 61 00:02:46,719 --> 00:02:54,780 [No Audio] 62 00:02:54,781 --> 00:02:57,090 if I change the value of the variable, to that 63 00:02:57,091 --> 00:02:59,633 of, let's say 55 and execute again. 64 00:03:00,100 --> 00:03:02,250 You may note that, the statement inside the if 65 00:03:02,251 --> 00:03:05,040 statement is not executed, but the statement 66 00:03:05,041 --> 00:03:07,493 outside the if statement has executed. 67 00:03:07,494 --> 00:03:09,600 [No Audio] 68 00:03:09,601 --> 00:03:12,933 This is because, there is no condition associated with it. 69 00:03:13,133 --> 00:03:15,180 Okay now, let us move ahead. The 70 00:03:15,181 --> 00:03:17,460 condition in the if statement, can be composed 71 00:03:17,461 --> 00:03:19,560 of multiple conditions that are joined 72 00:03:19,561 --> 00:03:21,930 together using the logic operators, such as 73 00:03:21,931 --> 00:03:25,170 the AND and OR operations. Let us see an 74 00:03:25,171 --> 00:03:27,840 example on this. Let me first declare a 75 00:03:27,841 --> 00:03:31,417 variable of marks, and assign it a value of 65. 76 00:03:31,418 --> 00:03:33,966 [No Audio] 77 00:03:33,967 --> 00:03:36,180 Let us now check, if the student marks are 78 00:03:36,181 --> 00:03:39,543 in a certain range, let's say in the range of 60 to 70. 79 00:03:39,544 --> 00:03:41,433 [No Audio] 80 00:03:41,434 --> 00:03:42,960 For the marks to be in this range, 81 00:03:42,961 --> 00:03:45,480 we will have conditions to check that they 82 00:03:45,481 --> 00:03:47,700 are greater than or equal to 60, and less than 83 00:03:47,701 --> 00:03:50,160 or equal to 70. The two conditions can be 84 00:03:50,161 --> 00:03:52,110 combined, with the help of logical AND 85 00:03:52,111 --> 00:03:54,630 operation. The logical AND operation will 86 00:03:54,631 --> 00:03:57,630 return a true, when both the conditions to the 87 00:03:57,631 --> 00:03:59,880 left of it, and to the right of it, are both 88 00:03:59,881 --> 00:04:02,430 true at the same time. The logical AND 89 00:04:02,431 --> 00:04:05,084 operation is indicated by the two && signs. 90 00:04:05,085 --> 00:04:07,350 [No Audio] 91 00:04:07,351 --> 00:04:09,330 If the marks are in the range, then we will 92 00:04:09,360 --> 00:04:10,893 display a suitable message. 93 00:04:10,894 --> 00:04:16,132 [No Audio] 94 00:04:16,133 --> 00:04:17,565 Let us execute this. 95 00:04:17,566 --> 00:04:21,733 [No Audio] 96 00:04:21,734 --> 00:04:22,830 This statement has been 97 00:04:22,831 --> 00:04:24,630 printed which indicates to us that the 98 00:04:24,631 --> 00:04:27,690 condition was overall true. Please note that, 99 00:04:27,691 --> 00:04:30,833 one & sign is used for a reference in Rust. 100 00:04:30,834 --> 00:04:33,840 And two & signs are used, for a logical AND 101 00:04:33,870 --> 00:04:36,510 operation. Similar to the logical AND 102 00:04:36,511 --> 00:04:38,790 operation, we can use the logical OR operation 103 00:04:38,791 --> 00:04:41,066 to construct the conditions for the if statement. 104 00:04:41,200 --> 00:04:42,966 Let us do an example on this. 105 00:04:44,100 --> 00:04:46,950 I will declare a couple of Boolean variable, 106 00:04:46,951 --> 00:04:48,710 called flag_1 and flag_2. 107 00:04:48,711 --> 00:04:55,566 [No Audio] 108 00:04:55,567 --> 00:04:58,400 Next I will set condition, if flag_1 == true 109 00:04:58,401 --> 00:05:01,230 or flag_2 == true, then the body of 110 00:05:01,231 --> 00:05:03,266 the if statement should be executed. 111 00:05:03,600 --> 00:05:06,570 The logical OR is mentioned using two vertical 112 00:05:06,571 --> 00:05:09,210 pipes, let me add a suitable display message 113 00:05:09,240 --> 00:05:11,211 inside the if statement and execute. 114 00:05:11,212 --> 00:05:18,300 [No Audio] 115 00:05:18,301 --> 00:05:19,530 The message is displayed, 116 00:05:19,531 --> 00:05:21,750 which means that the conditions it will await 117 00:05:21,751 --> 00:05:24,210 it to true. If we want to construct a 118 00:05:24,211 --> 00:05:26,160 condition by checking if two things are 119 00:05:26,190 --> 00:05:28,860 equal, then we will use two equal signs 120 00:05:28,890 --> 00:05:31,560 instead of one equal sign, between the value 121 00:05:31,561 --> 00:05:34,740 of the two items. Negation operation can also 122 00:05:34,741 --> 00:05:36,990 be used to construct the conditions, let us 123 00:05:36,991 --> 00:05:38,300 also learn this. 124 00:05:39,366 --> 00:05:41,000 I will create a boolean variable, 125 00:05:41,001 --> 00:05:43,733 and we'll assign to it a value of true. 126 00:05:44,100 --> 00:05:46,050 Next, I will check if the value of the 127 00:05:46,051 --> 00:05:49,100 flag is not equal to false, is true or not. 128 00:05:49,333 --> 00:05:52,200 If flag is not false, then this condition will be true. 129 00:05:52,733 --> 00:05:54,833 Since flag is not false, therefore, 130 00:05:54,834 --> 00:05:57,700 this condition will evaluate to a value of true. 131 00:05:58,366 --> 00:06:00,120 Let us confirm this by adding suitable 132 00:06:00,121 --> 00:06:02,066 print statement, and then execute. 133 00:06:02,067 --> 00:06:11,300 [No Audio] 134 00:06:11,301 --> 00:06:14,040 We can always combine the simpler expressions 135 00:06:14,070 --> 00:06:16,980 involving AND or OR operation to make more 136 00:06:16,981 --> 00:06:19,320 complex conditions. Let us see detailed 137 00:06:19,321 --> 00:06:20,566 examples on this. 138 00:06:21,266 --> 00:06:22,650 I will define two boolean 139 00:06:22,651 --> 00:06:26,267 variables, and one integer variable with a value of 60. 140 00:06:26,268 --> 00:06:34,080 [No Audio] 141 00:06:34,081 --> 00:06:36,180 I will now construct a complex condition, by 142 00:06:36,181 --> 00:06:39,533 setting flag_1 == true, 143 00:06:39,800 --> 00:06:42,866 and a flag_2 == false, 144 00:06:42,867 --> 00:06:45,030 or number < 50 145 00:06:45,043 --> 00:06:49,433 [No Audio] 146 00:06:49,434 --> 00:06:51,900 This condition has two parts, the first 147 00:06:51,930 --> 00:06:54,480 part will be only true, when both the 148 00:06:54,481 --> 00:06:57,221 conditions are true. That is flag_1 149 00:06:57,222 --> 00:06:59,400 has a value of true, and flag_2 150 00:06:59,401 --> 00:07:01,950 has a value of false. The OR outside the 151 00:07:01,951 --> 00:07:03,810 brackets means that, either the condition to 152 00:07:03,811 --> 00:07:05,966 the right, or to the left is being true. 153 00:07:06,800 --> 00:07:08,910 That is for the overall condition to be true, 154 00:07:08,911 --> 00:07:11,790 either the value should be lesser than 50, or 155 00:07:11,791 --> 00:07:13,800 the value inside the bracket should be true. 156 00:07:14,130 --> 00:07:16,830 Since the value is not lesser than 50, so we 157 00:07:16,831 --> 00:07:18,720 will consider the first part, where both the 158 00:07:18,721 --> 00:07:20,760 conditions are true. So therefore, the entire 159 00:07:20,761 --> 00:07:23,820 condition is true. Let us add a suitable 160 00:07:23,821 --> 00:07:25,770 print statement to the body of the if 161 00:07:25,771 --> 00:07:27,233 statement and execute. 162 00:07:27,234 --> 00:07:35,880 [No Audio] 163 00:07:35,881 --> 00:07:38,610 The complex condition has evaluated to true. 164 00:07:38,611 --> 00:07:40,560 Please note that, if I change the brackets and 165 00:07:40,561 --> 00:07:42,270 enclose the second and third conditions 166 00:07:42,271 --> 00:07:45,330 together inside the small brackets, then this 167 00:07:45,360 --> 00:07:47,100 expression will have a different meaning. 168 00:07:49,000 --> 00:07:51,030 This means that you have to pay special 169 00:07:51,031 --> 00:07:53,520 attention when aiding smooth brackets for 170 00:07:53,521 --> 00:07:57,810 constructing complex expressions. Okay, now 171 00:07:57,811 --> 00:07:59,730 we will learn some variants of the basic if 172 00:07:59,731 --> 00:08:02,580 statement. The first variant is called if else 173 00:08:02,581 --> 00:08:06,150 conditional statement. Let us start from 174 00:08:06,151 --> 00:08:07,532 the general syntax first. 175 00:08:07,533 --> 00:08:13,920 [No Audio] 176 00:08:13,921 --> 00:08:15,810 If the condition associated with the if 177 00:08:15,811 --> 00:08:17,850 statement is true, then the statements inside 178 00:08:17,851 --> 00:08:20,970 the body of if are executed, and the statement 179 00:08:20,971 --> 00:08:23,490 inside the body of else are skipped and not 180 00:08:23,491 --> 00:08:26,190 executed. On the other hand, if the statement 181 00:08:26,191 --> 00:08:28,380 associated with the if statement is false, 182 00:08:28,410 --> 00:08:30,210 then the statement inside the body of the 183 00:08:30,211 --> 00:08:32,669 else are executed, and the statement inside, 184 00:08:32,670 --> 00:08:36,000 the body of the if are skipped from execution. 185 00:08:36,166 --> 00:08:39,033 Let us understand this with the help of an example. 186 00:08:39,765 --> 00:08:41,880 We will declare a variable of marks, 187 00:08:41,881 --> 00:08:43,433 from a value of 80. 188 00:08:43,443 --> 00:08:45,906 [No Audio] 189 00:08:45,907 --> 00:08:48,833 Then we will check if marks > 50, 190 00:08:48,834 --> 00:08:50,733 then I will print the statement 191 00:08:50,734 --> 00:08:52,350 that you have passed, and if it 192 00:08:52,351 --> 00:08:55,593 is not true, then in the else part, 193 00:08:55,594 --> 00:08:57,393 I will print the message that you have failed. 194 00:08:57,397 --> 00:09:05,429 [No Audio] 195 00:09:05,430 --> 00:09:07,657 Let us execute to see the result. 196 00:09:07,658 --> 00:09:11,833 [No Audio] 197 00:09:11,834 --> 00:09:14,579 The candidate has passed. Another frequently used 198 00:09:14,580 --> 00:09:18,166 variant of the if statement, is the if else, if ladder. 199 00:09:18,167 --> 00:09:20,152 We will start with the general syntax. 200 00:09:20,153 --> 00:09:39,450 [No Audio] 201 00:09:39,451 --> 00:09:41,550 The conditional expression in this case are 202 00:09:41,551 --> 00:09:44,940 evaluated from top to bottom. As soon as a 203 00:09:44,941 --> 00:09:46,530 true condition is found, the statement 204 00:09:46,560 --> 00:09:49,110 associated with that if statements are 205 00:09:49,111 --> 00:09:51,270 executed and the rest of the ladder is 206 00:09:51,271 --> 00:09:53,790 skipped. In other words, the conditions are 207 00:09:53,791 --> 00:09:55,980 checked in sequential order and the body of 208 00:09:55,981 --> 00:09:58,050 the first condition happening to be true is 209 00:09:58,051 --> 00:10:00,960 only executed. If none of the conditions 210 00:10:00,961 --> 00:10:02,966 are true, then the else part will be executed. 211 00:10:03,666 --> 00:10:06,240 Also note that the else included at 212 00:10:06,241 --> 00:10:09,630 the end is optional. Let us understand this 213 00:10:09,631 --> 00:10:12,120 further, with the help of an example. I will 214 00:10:12,121 --> 00:10:14,280 write a simple program, which based on the 215 00:10:14,281 --> 00:10:16,320 marks of the student, will assign a suitable 216 00:10:16,321 --> 00:10:18,600 grade to the student. I will create a 217 00:10:18,601 --> 00:10:20,520 variable which will contain the marks of the 218 00:10:20,521 --> 00:10:23,666 student, and initialize it from a value of 95. 219 00:10:24,333 --> 00:10:26,580 Now based on the marks, I will assign a 220 00:10:26,581 --> 00:10:29,670 letter grade to the student. To store the 221 00:10:29,671 --> 00:10:32,700 grade, I will declare a grade variable and 222 00:10:32,701 --> 00:10:35,200 will initialize it from a value. 223 00:10:36,333 --> 00:10:40,598 And is a sort of default value, having a meaning of no break. 224 00:10:40,599 --> 00:10:43,666 [No Audio] 225 00:10:43,667 --> 00:10:45,570 Now, I will first aid conditions 226 00:10:45,571 --> 00:10:47,970 for the maximum grade, which is A, and it is 227 00:10:47,971 --> 00:10:50,790 given to a student securing greater than or 228 00:10:50,791 --> 00:10:53,163 equal to 90 marks, so let me code it. 229 00:10:53,164 --> 00:10:59,070 [No Audio] 230 00:10:59,071 --> 00:11:01,260 The next grade is B, which corresponds to 231 00:11:01,261 --> 00:11:03,266 marks greater than or equal to 80. 232 00:11:03,267 --> 00:11:07,266 [No Audio] 233 00:11:07,267 --> 00:11:09,405 Next comes the grade of C, which corresponds 234 00:11:09,406 --> 00:11:11,655 to marks greater than or equal to 70. 235 00:11:11,656 --> 00:11:17,580 [No Audio] 236 00:11:17,581 --> 00:11:19,980 The next grade given on marks, greater than or 237 00:11:20,010 --> 00:11:20,997 equal to 60. 238 00:11:20,998 --> 00:11:27,390 [No Audio] 239 00:11:27,391 --> 00:11:30,400 In the optional else part, I will assign a grade of F. 240 00:11:30,833 --> 00:11:32,670 Let us also add a suitable display 241 00:11:32,671 --> 00:11:34,457 message for printing the student grade. 242 00:11:34,458 --> 00:11:40,200 [No Audio] 243 00:11:40,201 --> 00:11:42,566 Let us execute to examine the grade. 244 00:11:42,567 --> 00:11:47,300 [No Audio] 245 00:11:47,301 --> 00:11:51,119 The grade is A in this case, the first if statement 246 00:11:51,120 --> 00:11:52,619 happens to be true in this case, and 247 00:11:52,620 --> 00:11:55,266 therefore, the grade was being set to that of A. 248 00:11:55,633 --> 00:11:57,929 Although the remaining conditions are also 249 00:11:57,930 --> 00:12:00,179 true in this case, that is the marks are also 250 00:12:00,180 --> 00:12:03,089 greater than or equal to 80, and 70, and 60. But 251 00:12:03,090 --> 00:12:05,759 only the body of the first if statement is 252 00:12:05,760 --> 00:12:08,879 being executed. This is because as pointed 253 00:12:08,880 --> 00:12:12,089 out earlier also, in the if-else ladder, the 254 00:12:12,090 --> 00:12:14,279 conditions are checked in sequential order, 255 00:12:14,280 --> 00:12:16,589 and the body of the first condition happening 256 00:12:16,590 --> 00:12:20,249 to be true is always executed. That brings us 257 00:12:20,250 --> 00:12:22,559 to the end of this tutorial. Our discussion 258 00:12:22,560 --> 00:12:24,479 on the if statement is still not complete and 259 00:12:24,480 --> 00:12:26,549 we will discuss some of its variants in the 260 00:12:26,550 --> 00:12:28,919 next tutorial also. Along with another 261 00:12:28,920 --> 00:12:31,139 relevant statement, which is very frequently 262 00:12:31,167 --> 00:12:33,700 used in Rust, called the Match statement. 263 00:12:34,233 --> 00:12:36,419 That will be fun and exciting, so do come back for 264 00:12:36,420 --> 00:12:38,366 learning that. And until next tutorial, 265 00:12:38,367 --> 00:12:39,666 happy Rust Programming. 266 00:12:39,667 --> 00:12:44,200 [No Audio]