1 00:00:01,260 --> 00:00:04,710 In this lecture you will learn about the conditional statements. 2 00:00:04,710 --> 00:00:09,540 So a conditional statements allows you to make decisions in your programs. 3 00:00:09,540 --> 00:00:12,900 So these decisions rest on building expressions. 4 00:00:12,900 --> 00:00:16,220 So which evaluate a true or false. 5 00:00:16,230 --> 00:00:23,430 So for example, you can compare some operators such as greater than or not equal to build to build 6 00:00:23,430 --> 00:00:25,020 a Boolean expression. 7 00:00:25,020 --> 00:00:28,110 So some basic comparison operators. 8 00:00:28,260 --> 00:00:31,740 Let's create an here, for example, integer. 9 00:00:31,770 --> 00:00:34,080 Let's create an integer value variable. 10 00:00:34,080 --> 00:00:39,120 Name x here and assign it to zero and 42 equals here. 11 00:00:39,120 --> 00:00:43,980 I will I will write all of this codes here and we'll explain later here. 12 00:00:44,160 --> 00:00:49,920 4 to 2, not equals to zero. 13 00:00:52,170 --> 00:00:52,740 Here. 14 00:00:53,100 --> 00:00:56,970 And 109 X here. 15 00:00:57,720 --> 00:00:58,710 One, two, three. 16 00:00:58,740 --> 00:01:02,490 Greater than or equal to X here. 17 00:01:02,910 --> 00:01:05,640 And minus ten. 18 00:01:06,990 --> 00:01:08,940 Less than X here. 19 00:01:10,510 --> 00:01:14,680 And -99 less than or equal to X here. 20 00:01:15,790 --> 00:01:19,570 So let's let me write this here. 21 00:01:19,600 --> 00:01:21,520 This is the equality operator. 22 00:01:22,420 --> 00:01:25,230 Bit further term boolean or false. 23 00:01:25,450 --> 00:01:29,170 This is the inequality operator Inequality. 24 00:01:30,340 --> 00:01:34,150 And this is his actual allotment. 25 00:01:34,390 --> 00:01:36,010 Use the tabs here. 26 00:01:38,350 --> 00:01:38,860 Okay. 27 00:01:38,860 --> 00:01:40,420 One, two. 28 00:01:42,980 --> 00:01:44,660 This is a nuclear operator here. 29 00:01:44,660 --> 00:01:46,610 This is greater than. 30 00:01:47,000 --> 00:01:47,900 And the. 31 00:01:51,520 --> 00:01:55,000 This is less greater than. 32 00:01:55,240 --> 00:01:59,800 Greater than or equal to. 33 00:02:01,310 --> 00:02:01,640 Here. 34 00:02:01,640 --> 00:02:11,550 This says less than and this is less than or equal to. 35 00:02:11,570 --> 00:02:12,350 Operator. 36 00:02:13,060 --> 00:02:14,670 Click here. 37 00:02:15,300 --> 00:02:18,930 So this program produce no output. 38 00:02:19,380 --> 00:02:22,140 Let's compile it and show what it will produce. 39 00:02:22,170 --> 00:02:25,290 This will return zero because we are not a parent. 40 00:02:25,530 --> 00:02:30,600 We are printing nothing here and using no variables here, as you can see. 41 00:02:30,630 --> 00:02:40,170 See lion automatically assign them, assign not them, not them as unused variables. 42 00:02:40,180 --> 00:02:48,060 So while the program doesn't produce any output compiling it helps to verify that your written is valid. 43 00:02:48,510 --> 00:02:49,810 C++ code. 44 00:02:49,830 --> 00:02:59,940 So to generate more interesting programs, you use conditional statements like if your SO and if statement 45 00:02:59,940 --> 00:03:05,100 contains a boolean expression and one or more nested statements. 46 00:03:05,220 --> 00:03:12,360 So depending on whether the boolean value is a true or false, so the program decides which nested statement 47 00:03:12,360 --> 00:03:13,220 to execute. 48 00:03:13,230 --> 00:03:24,090 So there are several forms of statement, but basic logic here is you use a boolean expression version 49 00:03:24,270 --> 00:03:26,160 and then statement. 50 00:03:27,620 --> 00:03:28,730 Your statement. 51 00:03:29,880 --> 00:03:32,070 Or, for example, statement here. 52 00:03:32,730 --> 00:03:35,710 So as an expression, let's create a bull type here. 53 00:03:36,240 --> 00:03:39,270 Boolean expression. 54 00:03:40,730 --> 00:03:41,250 Test. 55 00:03:41,540 --> 00:03:42,600 Test here. 56 00:03:42,980 --> 00:03:44,120 Assign it true. 57 00:03:44,570 --> 00:03:48,680 So if this boolean expression is true. 58 00:03:51,870 --> 00:03:53,240 Purely an expression. 59 00:03:53,250 --> 00:03:59,760 So if this boolean expression is true, then our statement will be executed. 60 00:03:59,760 --> 00:04:01,210 So as a boolean expression. 61 00:04:01,230 --> 00:04:02,750 So let's create a bool here. 62 00:04:02,760 --> 00:04:08,790 A You can write this as a boolean expression here, so let's put it here. 63 00:04:08,940 --> 00:04:12,780 And as you can see here, we got no error here. 64 00:04:12,810 --> 00:04:23,580 So if here print F or C out STD or let's just use print F. 65 00:04:27,190 --> 00:04:27,590 Yeah. 66 00:04:29,760 --> 00:04:32,750 If a f. 67 00:04:34,390 --> 00:04:36,700 We have to enter the variable here. 68 00:04:37,420 --> 00:04:47,560 We use this studio here because this printf contains this, printf has this studio has the printer function 69 00:04:47,560 --> 00:04:47,980 in it. 70 00:04:48,310 --> 00:04:55,210 So this, as you know, printf is not default just system method in C++ here. 71 00:04:55,240 --> 00:04:59,560 So let's create create an executed or. 72 00:05:00,560 --> 00:05:01,010 Yeah. 73 00:05:01,100 --> 00:05:01,720 Thanks. 74 00:05:02,730 --> 00:05:03,230 Here. 75 00:05:03,620 --> 00:05:05,750 And let's run this code. 76 00:05:05,780 --> 00:05:14,180 As you can see, we got no output here because our Boolean expression is false, as you can see here. 77 00:05:14,330 --> 00:05:21,830 And if we put this, if 42 equals 42, this will evaluate true. 78 00:05:21,860 --> 00:05:25,250 That's why our code will be executed. 79 00:05:25,970 --> 00:05:27,440 Here we have another point. 80 00:05:27,740 --> 00:05:29,840 Another if statement. 81 00:05:29,840 --> 00:05:35,090 You can write this like compound statement because sometimes you will want a group of statements to 82 00:05:35,090 --> 00:05:37,020 run rather than a single statement. 83 00:05:37,040 --> 00:05:40,900 If you do a single statement, you can use this as follows. 84 00:05:40,910 --> 00:05:45,050 So such a group is called the compound statement. 85 00:05:45,050 --> 00:05:52,520 So to declare the compound statement, simply wrap the group of statements in a brazier like this so 86 00:05:52,520 --> 00:05:57,300 you can use compound statements with within the if statements as follows here. 87 00:05:57,320 --> 00:06:01,490 So let's create an A here or let's get another Boolean expression. 88 00:06:01,490 --> 00:06:08,030 So if 100 is a Boolean, be here, if 100 is greater than 89 00:06:11,090 --> 00:06:12,530 100 is greater than X. 90 00:06:12,530 --> 00:06:14,030 Here, here. 91 00:06:20,300 --> 00:06:22,670 Let's make it look nice. 92 00:06:24,570 --> 00:06:25,200 Yes. 93 00:06:30,240 --> 00:06:31,800 Here and here. 94 00:06:34,300 --> 00:06:35,050 So. 95 00:06:36,610 --> 00:06:37,210 Okay. 96 00:06:37,660 --> 00:06:49,600 So if B will our B variable is if this expression will return the grid true, then our if function if 97 00:06:49,600 --> 00:06:52,390 statement, if statement will be executed. 98 00:06:52,540 --> 00:06:54,190 If not, this is not a function. 99 00:06:54,190 --> 00:06:55,300 This is a statement here. 100 00:06:56,420 --> 00:06:59,870 So let's create let's give this as an example. 101 00:07:00,680 --> 00:07:02,030 If B. 102 00:07:03,660 --> 00:07:04,620 Is true. 103 00:07:04,620 --> 00:07:09,720 If it's true, then printed this year, for example, code. 104 00:07:11,060 --> 00:07:12,110 Executed. 105 00:07:13,900 --> 00:07:16,450 S.F. executed. 106 00:07:17,830 --> 00:07:18,790 Quote to. 107 00:07:20,710 --> 00:07:27,370 Pier, code one and executed code here and use this. 108 00:07:27,370 --> 00:07:29,530 So let's use this. 109 00:07:29,530 --> 00:07:32,320 And as you can see here, let's actually add new line. 110 00:07:32,320 --> 00:07:34,150 So make it look nice. 111 00:07:35,210 --> 00:07:35,600 Here. 112 00:07:35,720 --> 00:07:37,820 So executed Code one. 113 00:07:37,820 --> 00:07:39,560 Executed code two. 114 00:07:39,710 --> 00:07:43,270 So in this case, our both this statement is executed. 115 00:07:43,280 --> 00:07:46,940 So actually, let's make this comment to. 116 00:07:48,470 --> 00:07:54,950 So if the comment is, as you know, the compiler will not compile this code and use this as a command, 117 00:07:54,950 --> 00:07:55,940 just a text here. 118 00:07:55,970 --> 00:08:05,000 As you can see, the both of our functions, both of our expressions and codes is executed in this if 119 00:08:05,000 --> 00:08:05,910 statement. 120 00:08:05,930 --> 00:08:15,860 So if we if this values true, or we can use this and directly give this as an here, if 100 is greater 121 00:08:15,860 --> 00:08:18,600 than X, then execute our code. 122 00:08:18,620 --> 00:08:20,560 As you can see, we got that same output. 123 00:08:20,570 --> 00:08:29,180 So if we change to use less than operator here, this function, this code will not this if statement 124 00:08:29,180 --> 00:08:30,330 will not execute. 125 00:08:30,350 --> 00:08:33,770 As you can see here, the x equals zero. 126 00:08:33,770 --> 00:08:38,050 So our 100 is not less than zero. 127 00:08:38,060 --> 00:08:43,910 And this if statement, we will not execute in this time. 128 00:08:43,910 --> 00:08:49,660 So you can elaborate if statement using the ls if and l statements here. 129 00:08:49,670 --> 00:08:55,710 So this optional addition allows you to describe more complicated branching behavior. 130 00:08:55,730 --> 00:08:58,730 So let's create another example here. 131 00:09:00,780 --> 00:09:04,410 If so, make it here. 132 00:09:04,440 --> 00:09:12,180 And if our boolean expression is true, then use the statement. 133 00:09:12,900 --> 00:09:13,830 Print F. 134 00:09:16,790 --> 00:09:18,590 My statement. 135 00:09:20,260 --> 00:09:23,170 One new line. 136 00:09:25,370 --> 00:09:28,010 And here else if. 137 00:09:29,340 --> 00:09:31,950 Our my statement. 138 00:09:31,980 --> 00:09:34,260 My statement is true. 139 00:09:34,560 --> 00:09:35,610 My statement. 140 00:09:35,700 --> 00:09:44,190 This is this is an expression we have to give here if false or let's actually use the create a new variable 141 00:09:44,190 --> 00:09:44,940 integer. 142 00:09:44,940 --> 00:09:50,220 My var variable here is hundred and integer. 143 00:09:50,220 --> 00:09:52,860 My my. 144 00:09:54,180 --> 00:10:01,500 My car and my van here is 50 and integer. 145 00:10:01,500 --> 00:10:02,220 My. 146 00:10:04,030 --> 00:10:09,580 My phone is, for example, ten, just as an example variable names. 147 00:10:09,580 --> 00:10:12,130 So if our my car. 148 00:10:13,510 --> 00:10:16,120 Is greater than zero. 149 00:10:18,690 --> 00:10:20,310 Actually, we don't need these variables here. 150 00:10:21,270 --> 00:10:23,520 Then evaluate my car. 151 00:10:23,550 --> 00:10:27,990 Greater than greater than 100. 152 00:10:29,100 --> 00:10:29,610 Here. 153 00:10:29,850 --> 00:10:34,680 And then if this statement is not executed, you lose the. 154 00:10:37,160 --> 00:10:41,060 My car and give the second statement. 155 00:10:41,060 --> 00:10:54,590 If my car equals greater than actually make this 50 and my car is greater than 50, and then you give 156 00:10:54,590 --> 00:10:56,360 this my car. 157 00:10:57,140 --> 00:11:01,160 Greater than 100. 158 00:11:01,270 --> 00:11:01,700 Yeah. 159 00:11:04,650 --> 00:11:05,310 Hundreds. 160 00:11:14,580 --> 00:11:17,550 And then else and else. 161 00:11:17,550 --> 00:11:19,460 We don't have to give condition here. 162 00:11:19,470 --> 00:11:29,970 Then if these conditions will not exceed good, then use this else condition here and you're going to 163 00:11:31,110 --> 00:11:32,220 write it here. 164 00:11:33,460 --> 00:11:41,190 We don't know about what or just values. 165 00:11:42,490 --> 00:11:44,310 As statement here. 166 00:11:48,330 --> 00:12:04,530 If and else if statements does not executed here and our SE comment is executed here and use this here. 167 00:12:04,530 --> 00:12:12,930 So let's run this program and execute it code my car greater than 50 here as you can see here, our 168 00:12:12,930 --> 00:12:15,150 car my car is equal to zero. 169 00:12:15,810 --> 00:12:17,580 My car is equal to 100. 170 00:12:17,580 --> 00:12:20,820 And that's why this ls If command is not executed. 171 00:12:20,820 --> 00:12:24,150 And let's create an let's assign my car 101. 172 00:12:24,600 --> 00:12:29,550 And as you can see here, my car is greater than 50 here. 173 00:12:30,760 --> 00:12:42,490 So if we do like the little else here and use this and as you can see, both of our expression expressions 174 00:12:42,490 --> 00:12:44,290 will be executed here. 175 00:12:45,680 --> 00:12:46,640 Make it nice. 176 00:12:46,640 --> 00:12:52,340 So like my car greater than 50, my car greater than 100. 177 00:12:52,640 --> 00:12:57,160 So the first boolean expression here is evaluated here. 178 00:12:57,190 --> 00:12:59,260 Actually, let's make it evaluated. 179 00:12:59,270 --> 00:13:08,690 So if boolean expression one is true and then statement one is evaluated and if statement stops executed 180 00:13:08,960 --> 00:13:15,670 and if boolean expression one is false, then boolean expression two is evaluated. 181 00:13:15,680 --> 00:13:16,430 So. 182 00:13:17,530 --> 00:13:20,320 If true this statement to hear. 183 00:13:20,320 --> 00:13:26,130 If boolean expression two is true, then the our statement two will be executed. 184 00:13:26,140 --> 00:13:30,040 Otherwise statement free will be executed. 185 00:13:30,040 --> 00:13:34,330 So if you make it like one, then our. 186 00:13:35,570 --> 00:13:36,420 Excluded. 187 00:13:36,440 --> 00:13:43,610 Court to hear actually if integer my car and else else if. 188 00:13:47,020 --> 00:13:53,450 Here, so you can include any number of URLs if classes. 189 00:13:53,470 --> 00:13:55,750 Or omit them entirely. 190 00:13:55,780 --> 00:14:04,000 So as with the initial if statement, the boolean expression for each else if class is evaluated in 191 00:14:04,000 --> 00:14:04,360 order. 192 00:14:04,360 --> 00:14:12,580 So when one of these boolean exploration evaluates to true evaluation stops and corresponding statement 193 00:14:12,580 --> 00:14:13,180 executes. 194 00:14:13,220 --> 00:14:24,250 So if no else if course evaluates to true the else here statement tree will always execute. 195 00:14:47,220 --> 00:14:50,220 So now I want to write some quote here. 196 00:14:50,220 --> 00:14:59,870 So integer, create an integer here or let's value make my card variable zero in here. 197 00:14:59,880 --> 00:15:13,740 Then if our my car greater than zero, then here print my car, my car greater than zero here or less 198 00:15:14,190 --> 00:15:14,760 here. 199 00:15:15,750 --> 00:15:18,990 And as if here. 200 00:15:22,710 --> 00:15:27,880 If my car less than zero then print. 201 00:15:31,160 --> 00:15:32,120 Print test. 202 00:15:32,190 --> 00:15:32,920 Negative. 203 00:15:32,930 --> 00:15:34,610 My car. 204 00:15:35,060 --> 00:15:35,990 My car. 205 00:15:36,290 --> 00:15:37,580 Less than zero. 206 00:15:37,940 --> 00:15:39,890 And F statement here. 207 00:15:43,350 --> 00:15:50,550 My car print off my car equals to zero. 208 00:15:51,930 --> 00:15:55,580 So now we can run this program and see what happens. 209 00:15:55,890 --> 00:15:56,670 Let me. 210 00:15:59,730 --> 00:16:02,730 So as you can see in this case, my card is zero. 211 00:16:02,730 --> 00:16:09,810 So if a sign and this if statement is executed, as you can see here. 212 00:16:09,840 --> 00:16:14,070 So if we make it more why my car to. 213 00:16:15,410 --> 00:16:22,760 Then my car greater than zero, will execute us well if we make it minus one. 214 00:16:24,190 --> 00:16:27,060 Then my card less than zero here. 215 00:16:27,070 --> 00:16:28,810 This statement is executed. 216 00:16:28,810 --> 00:16:36,940 If I make it zero again, then the SE statement my car equals to zero will be executed. 217 00:16:37,060 --> 00:16:38,030 And that's it. 218 00:16:38,050 --> 00:16:41,500 But the C++ conditional statements here.