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