1 00:00:00,480 --> 00:00:04,980 In this lecture you will learn about the conditional statements in C++. 2 00:00:04,980 --> 00:00:13,960 So the most frequently used conditional statements is if statement here, if true and there are code. 3 00:00:13,980 --> 00:00:23,580 So in its simplest form, the if statement takes a logical expression, true or false, or we can do 4 00:00:23,580 --> 00:00:27,450 for example is empty function like that. 5 00:00:27,600 --> 00:00:30,510 This is not actually is empty function really. 6 00:00:30,870 --> 00:00:38,310 But we can take anything that returns boolean value and give this as parameter. 7 00:00:38,370 --> 00:00:44,940 And if the parameter returns true, the if block will execute. 8 00:00:44,940 --> 00:00:54,450 If not the and if not, then we have else block or else if block the as if block and as block will execute. 9 00:00:54,690 --> 00:01:06,510 So if neither of if we only have the just one if block, then if and our if here is false, then our 10 00:01:06,510 --> 00:01:08,600 code will not execute at all. 11 00:01:08,610 --> 00:01:09,480 So. 12 00:01:11,300 --> 00:01:20,450 And we adhere to the logical expression in a pair of parenthesis and immediately followed by the statement 13 00:01:20,450 --> 00:01:23,990 as executed if the condition is true. 14 00:01:24,110 --> 00:01:27,270 So I want to write some quotes here. 15 00:01:27,290 --> 00:01:38,750 For example, you can hear integer integer a here STD seen here where we will get some. 16 00:01:41,170 --> 00:01:43,930 Input from the keyboard here from the user. 17 00:01:44,050 --> 00:01:48,940 If a greater than ten then. 18 00:01:50,590 --> 00:01:51,700 See out. 19 00:01:52,830 --> 00:02:00,640 Here A is greater than ten. 20 00:02:01,430 --> 00:02:03,770 Beer and. 21 00:02:04,990 --> 00:02:05,650 As you can see. 22 00:02:05,800 --> 00:02:08,960 So let's write our expression, right? 23 00:02:09,370 --> 00:02:12,790 Compile our code and let's see what happens. 24 00:02:12,790 --> 00:02:21,940 So we're going to try up some value eight and we got no output because if BLOCK was not true, so eight 25 00:02:21,940 --> 00:02:23,380 is not greater than ten. 26 00:02:23,380 --> 00:02:27,370 And this block used like it doesn't even dare. 27 00:02:27,370 --> 00:02:30,400 So this if block is passed. 28 00:02:30,400 --> 00:02:35,470 So let's create again, let's run this program again and write, for example, 99. 29 00:02:35,590 --> 00:02:42,790 As you can see here, A is greater than ten and this this code executed. 30 00:02:42,790 --> 00:02:48,430 So you can also use the statement to catch the occasions when the condition is false. 31 00:02:48,430 --> 00:02:50,320 So else here. 32 00:02:52,900 --> 00:02:57,060 Elsie, If this statement is false, then run. 33 00:02:57,310 --> 00:02:57,940 Statement. 34 00:02:58,180 --> 00:03:03,370 So for example std c out. 35 00:03:03,370 --> 00:03:04,240 So if. 36 00:03:06,120 --> 00:03:09,720 Our A number is greater than ten. 37 00:03:10,770 --> 00:03:11,490 Then. 38 00:03:14,110 --> 00:03:15,160 For example. 39 00:03:16,850 --> 00:03:21,920 Within range here of let's add the. 40 00:03:28,270 --> 00:03:29,410 Here with. 41 00:03:31,410 --> 00:03:35,820 Full range and arrested and locked. 42 00:03:37,830 --> 00:03:38,310 Here. 43 00:03:41,760 --> 00:03:45,570 For example one and within range. 44 00:03:45,570 --> 00:03:53,400 So or let's create a is less than than ten. 45 00:03:54,570 --> 00:03:55,080 Here. 46 00:03:56,650 --> 00:04:02,410 And let's fry it, for example five and we will get a e is less than ten. 47 00:04:02,410 --> 00:04:11,650 If a right 99 or for example, 11, we will get a is greater than ten and this code of pluck will execute. 48 00:04:11,920 --> 00:04:20,020 So the condition is a logical expression, as I said earlier, and C++ will convert from numeric types 49 00:04:20,020 --> 00:04:21,430 to bool here. 50 00:04:21,430 --> 00:04:30,190 So here, as I said earlier, zero is false and anything. 51 00:04:31,370 --> 00:04:37,280 In think non zero is true. 52 00:04:38,420 --> 00:04:38,750 Yeah. 53 00:04:39,140 --> 00:04:45,680 So if you are not careful, this can be a source of an error that is not only difficult to notice, 54 00:04:45,680 --> 00:04:49,280 but also can have an unexpected side effects. 55 00:04:49,280 --> 00:04:53,330 So let's create an another quote here. 56 00:04:55,510 --> 00:04:56,860 For example. 57 00:04:59,850 --> 00:05:01,760 Well, it's chaos. 58 00:05:06,110 --> 00:05:06,770 So. 59 00:05:07,650 --> 00:05:15,750 We get, we create an integer, a get user, get input from user and write it to a. 60 00:05:15,960 --> 00:05:17,910 And then if. 61 00:05:20,490 --> 00:05:22,140 E equals. 62 00:05:23,430 --> 00:05:29,010 Or a equals to minus one equals to minus one. 63 00:05:30,260 --> 00:05:30,890 Then. 64 00:05:31,750 --> 00:05:34,960 We will print the message typed minus one. 65 00:05:38,400 --> 00:05:42,210 Here and here we don't need, for now, the smog. 66 00:05:42,330 --> 00:05:43,740 So let's run. 67 00:05:44,940 --> 00:05:45,510 There. 68 00:05:46,470 --> 00:05:48,150 Or let's write. 69 00:05:48,960 --> 00:05:51,930 Let's use this end as called here. 70 00:05:51,960 --> 00:05:54,480 Non minus one. 71 00:05:55,560 --> 00:05:56,070 Here. 72 00:05:58,140 --> 00:05:59,730 Let's write five. 73 00:05:59,760 --> 00:06:04,020 We will get this minus one output here. 74 00:06:04,350 --> 00:06:06,300 If they write minus one here. 75 00:06:06,330 --> 00:06:09,060 Negative one, we will get types. 76 00:06:09,480 --> 00:06:10,110 Negative one. 77 00:06:10,110 --> 00:06:10,860 At which point? 78 00:06:11,550 --> 00:06:12,540 After this. 79 00:06:13,200 --> 00:06:25,080 After we got this output, the loop finishes so if you mistype you may here end up with the code here. 80 00:06:25,080 --> 00:06:34,500 For example, if a equals two minus one, then you will get this code here, for example, five typed 81 00:06:34,500 --> 00:06:35,480 minus one. 82 00:06:35,490 --> 00:06:42,450 So in this case, the assignment operator is used instead of equality operator. 83 00:06:42,480 --> 00:06:44,850 So I want to write it here. 84 00:06:44,880 --> 00:06:49,470 This is equality operator here. 85 00:06:51,480 --> 00:06:52,470 Equality. 86 00:06:52,620 --> 00:06:54,050 Equality. 87 00:06:54,060 --> 00:06:55,290 Operator. 88 00:06:56,400 --> 00:06:59,250 And this is assignment. 89 00:07:01,580 --> 00:07:02,450 Operator. 90 00:07:03,920 --> 00:07:15,940 So make here be careful when you use the operator, because these mistakes is hard to notice, really. 91 00:07:15,950 --> 00:07:24,770 So the result is that regardless of what you type at the console, the variable A is assigned to minus 92 00:07:24,770 --> 00:07:27,920 one and the signs minus one is not zero. 93 00:07:27,950 --> 00:07:34,280 The condition in the if statement is true, hence the rule clause of the statement is executed. 94 00:07:34,280 --> 00:07:36,790 Sign the variable assigned to minus one. 95 00:07:36,800 --> 00:07:39,320 This may alter logic further on your call. 96 00:07:39,320 --> 00:07:46,700 So the way to avoid this bug is to take advantage of the requirement that in an assignment of the left 97 00:07:46,700 --> 00:07:54,860 hand side must be an l value here minus one here a here. 98 00:07:55,430 --> 00:07:56,600 Let's run this again. 99 00:08:00,250 --> 00:08:02,500 Like to mine and this is non zero one. 100 00:08:02,500 --> 00:08:06,820 So our code started to work correctly again. 101 00:08:06,940 --> 00:08:18,070 So here the logical expression is minus one equal to operator A and signs that this operator is a commutative. 102 00:08:18,070 --> 00:08:20,080 The order of operations doesn't matter. 103 00:08:20,110 --> 00:08:21,910 You get the same result here. 104 00:08:21,940 --> 00:08:31,150 So it doesn't matter if you write a in left or right, you will get the same result here. 105 00:08:31,150 --> 00:08:32,050 Minus one. 106 00:08:32,140 --> 00:08:34,270 Let's write it eight, nine. 107 00:08:34,270 --> 00:08:35,100 Here again. 108 00:08:35,110 --> 00:08:39,910 You will your work will, your code will work correctly. 109 00:08:40,630 --> 00:08:47,170 So in this case here, the assignment has an error value. 110 00:08:48,630 --> 00:08:49,200 Here. 111 00:08:49,920 --> 00:08:56,610 Assignment has an air value on the left hand side, and this will close the compiler here. 112 00:08:57,860 --> 00:08:59,030 An error. 113 00:08:59,030 --> 00:09:07,040 If a write like that, which error named el value required as left operand of assignment. 114 00:09:07,980 --> 00:09:11,670 So your code won't compile if you write like that.