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