 Hello friends, I am Sanjay Gupta and I welcome you again on Sanjay Gupta Tech School. So, as you know that this is a C language video series which is being prepared in Hindi for beginners. Its video number 7 or you can say lecture number 7. So, in this lecture we will discuss conditional statements. So, basically conditional statements are 4. So, 2 we will discuss in this lecture which are simple if and if else. And 2 we will discuss in the next lecture which will be the next video. Lecture number 8. So, first I will explain to you what is simple if, if else, what are their syntax. And after that you can see the title of these 3 programs which I will tell you by implementing. So, as in every video there is a trend that I will explain theoretical things first and then practical implementation. So, in this video we will also follow that. And if we talk about the last video then I explained to you how we use the predefined function and how to use the ask value. So, in that also I told you theoretical and practical implementation. So, if you missed this video or if you want to find the link of the entire video series. So, you go to the description of this video. In the description you will get the first link, the playlist link. If you open that playlist link then you will get all the videos automatically arranged in a sequence. And those who already have that link then they can continue. And this is my information which if you want to take it from me or if you want to discuss any query or problem. So, you can use these details. So, let's start. So, first I will tell you theoretically that these are simple if and if else syntax. And then practically understand it. So, first of all simple if. So, simple if is given here by these two syntax. So, this is the highlighter of my choice. So, this is the first syntax. So, whenever we use simple if then you have to use a keyword if. If is a keyword. So, if is a keyword in which we have to write a condition in parenthesis. And if this condition is true then this statement will be executed here. Okay. Now, if we look at the second syntax carefully then we have used this curly basis. Okay. And here it is called statement 1 to n. So, you will see the difference is that if you have only one statement when the condition is true. Then the curly basis is optional. And if there are more than one statement then you have to use the curly basis to group it. So, it is written below that if there is only one statement then the braces are optional otherwise they are compulsory. Right. And simple if only focuses upon the true part or true block of the problem. If this if condition falls then nothing will happen. Okay. Because for that we have to write else and here it is written else is optional in if statement. So, there are many situations where you have to put if condition and you have to check that if it is true then this works. And if it is false then we don't have to perform anything. So, what we will use on those places is simpler. Okay. Now, let's talk about if or else. So, basically these simple if, else or the future two things which are nested if or else a flatter. So, these all together relate to your real life. So, in real life this happens a lot and it happens daily. Whenever you want to do something then there are two things in your mind that I have to do or not. Yes or no? True or false? So, similarly we use if else to solve the same purpose. So, you want to point the false block along with true. So, for that we will use syntax if else. Okay. And syntax word meaning you have to understand that it follows the rules. Means it indicates the rules. In programming whenever you have to use the word syntax. So, if conditional is its true block. Else is its false block. Curly basis rules are the same. If there is one statement in the true block then it is optional. If there is more than one statement then you have to use it so that you can do grouping. And same with else. So, if condition is true then it will be true block. If it is false then it will be else block. If it is true block then it will be false block. If it is true block then it will be false block. If it is true block then it will be false block. Okay. So, now let's go to our compiler so that I can explain a small example. And after that we will understand these three programs. So, I will explain them in small examples. Then we will implement the real life example. So, this is our basic structure. Means in every program we don't have to write this much. And you might have understood this till now. Now, let's talk about a variable. A in which I entered 5. Okay. Now, I wrote if A greater than 2. And I wrote here printf. A is greater than 2. I wrote the message. And I saved it. Okay. Now, see if condition was A greater than 2. And if condition will be true then this printf will run. If condition will be false then nothing will happen. And with this A is the only printf. So, I didn't put curly basis. It is optional. We can put it if we want. So, let me run it first. See A is greater than 2. Condition is true because A is 5 which is bigger than 2. Now, if you put curly basis then it will work. So, you cut it. And put it under these curly basis. So, curly basis is optional. You can do it if you want. So, change it. Save it. Run it. Still, the output will be the same. Now, suppose we compare it with 20. So, this condition should be true or false? So, you will say false because it is not greater than 5. It means this thing shouldn't be a print. If we haven't written a false block then you can see. Nothing is a print. Now, you want the else part to be a print. So, what you will do is else. Now, see, it is the same with else. If there is one line then you can avoid the curly basis. If it is more than 1 then you can put it. So, I wrote printf. And here I write false. Now, if we run it then the output will be false block. Okay. Now, let's write one more line here. Here I have written true block. So, see, now the role of the curly basis is that if you have two lines then it is necessary to put the curly basis. So, if you true it, instead of 5, I will give it 50. So, the condition will be true. So, you will see that both the lines will be printed. A is greater than 2 and true block. Both are printed. So, in this way you will manage if else. So, if you want to focus only on the true part then it is simple. Okay. And if else, meaning true, false, you have to focus on both the parts. So, if else you will use it. If there is one line then the curly basis is optional. If there is more than one line then it is necessary to put the curly basis. Okay. So, I hope you have understood this. Now, let's move on to our program. So, after explaining these three programs and small things, and what mistakes you can make, I will tell you in that. First, let's understand these programs so that you will become more familiar with if else. Then we will discuss those things. So, the first is eligibility for voting of a person. So, the right to vote is, we will talk about India because I belong to India. And Hindi is in India. So, mostly Indian viewers are watching this video. So, the eligibility to vote in India is 18 and above your age. Okay. So, we have made a variable age. Then we have written printf. And we have asked the user to enter your age. scanf%d, n%h. Okay. So, through printf scanf, the user's age will be input. Then we have to apply the if condition. So, what will be the condition here? age greater than equals to 18. So, if you are careful, I have made a video on operators. Maybe it was the fourth video. Let's see if it goes back. The fourth video is lecture number 4. You will see. So, in operators, I have explained this. How can we use relational operators? Then I have told you that we can use greater than equals to. So, we have used that. So, if the age is more than 18, then you can print eligible for vote. And if it is not, then you can print eligible for vote. Right. So, we have written true part and false part. Now, save it. Compile it and run it. So, the age I have entered is 30. So, output is eligible for vote. Right. Let's run it again. This time, I have entered 18. So, again, you are seeing eligible for vote. Because, if it is 18, then you are eligible for vote. Now, if it is less than 18, like 10. So, output is not eligible for vote. So, we have written the program once, but we have checked it for 3 different ages. This is the benefit of the programming. And you have understood the use of if-else also. Okay. Now, if you want, we can include the curly basis also. So, I am cutting this printf. It is written inside. Okay. And I will write one more printf. That is why I have written true block. And similarly, let's modify the else. So, this printf, I have written one more printf. And here, I have written false block. So, I am writing this only for example. True block, false block. It is possible that when you solve a big problem, automatically you have to write a statement inside if. So, I am explaining this so that you can know how to use the curly basis also. So, in this program, if there are two lines in the true part, then we have used the curly basis along with if. And if there are two lines along with else, then we have used the curly basis also. Okay. And even now, your program will run properly. So, once we enter 20. So, eligible 4-vote and true block. Both are printed. And if we run it again, and enter 10, then you can see not eligible 4-vote and false block. Both are printed. So, in this way, we can use the curly basis. Depending upon how many statements you have, true or false. And it is possible that if there are two statements in the true part, then you have used the curly basis. And if there is one statement in the false part, then you have avoided it. So, this is also valid. So, it is not necessary that if you have used the curly basis in the true block, then you have used the curly basis along with else also. There is no fixed rule like this. So, I have explained this in the first program. Now, let's talk about the second program. So, we will have to find out max among two numbers. So, in this, we will have to input two numbers from the user. So, find out max among two numbers. So, let's erase it. Now, we have to declare two variables AB. We have to input from the user. So, we have written enter two numbers. Then, we will input two numbers. So, we will have to write the percentage in the scanup. The first number will go to A and the second number will go to B. Now, we will apply F. So, we will write F A greater than B. So, again, you don't have to use the curly basis because we have to use the same printf for else. So, what will you write here? A is max and else we have written printf is max. So, if the condition is true, then it is greater than AB. If the condition is false, then it is greater than BA. So, right now, we are not checking the equal case. You will see how it will be checked in my next video. Because, right now, we are focusing on the true and false part. Means, there are two situations. If we check the equal case as well, then there will be three situations. The first situation can be greater than AB. The second situation can be greater than BA. And the third situation can be equal as well. So, for that, we will use nested for else. So, you will see that in my next video. Next is number eight. So, I will tell you two things there. So, nested for else. So, there, we will understand how to handle three situations. So, to understand that, you will definitely watch the next video. So, right now, we will run it. So, the first number is 10 and the second number is 20. So, what is in the output? B is max. Now, you want to print the values of A and B instead of A. So, instead of A, you wrote %D and A. And instead of B, you wrote %D and %B. So, now, we will run it. Now, you will see that the values will be printed. So, 10 and 5. So, what is printed in the output? 10 is max. So, in this way, you can modify the printf according to your recording. So, in printf, you have become an expert when you are watching the seventh lecture of this video series. So, I have used printf a lot of times. And you must have implemented it practically. And I assume that after watching this video, you practice too. Because programming is only easy until you practice. If you don't practice, then nothing is tougher than this. So, do a lot of practice. And in my every video, you see that I have told you things after theoretical explanation with proper practical implementation. So, any problem or doubt, comment. Discuss with me, discuss with your friends so that you understand programming well. Now, this is the last program. Check the number for even or odd. So, first I will explain this program. And then I will tell you some tips and tricks which are important to use if or else. So, after understanding this program, you will also see those things so that you understand it well. So, first we will check the even odd. So, even odd may be known to everyone. So, the number which is completely divisible from 2 is even and the one which is not, is odd. So, we have to check that. So, the user will give you a number. You have to tell that the number which the user has input is even or odd. So, first let's get the number input. Scanner, person D, m%n. So, the number has come to us. Now, we have to check that whether the number 2 is completely divisible or not. So, if any number 2 is completely divisible then its remainder should be 0. So, in the operators video, I had told you to use the remainder. If you are aware and have seen that video, then you must have seen this symbol. So, let's write it down in the comment. This symbol's name is modulus. And what does this calculate as the remainder value? So, now we will write if n modulus 2 double equals to 0. So, see here we have used two operators. One is modulus and one is double equals to 0. So, the first thing to solve is this. Because the precedence of modulus is high. So, the first number which you have input will be divided from 2 and its remainder will come to us. So, for example, we have taken n, 10. So, when we divide 10 from 2, then the remainder will be 0. So, the result of this equation is 0. And how do we compare it with 0? So, both are equal. So, for comparison, we use double equals to use that both the entities are equal. So, if this happens, you will write printf number is even. And else, you will write printf number is odd. Okay. So, modulus is working. You have to understand it better. Or, all the operators like double equals to or all the operators, you will have to see lecture number 4. So, again, in the description of this video, you will find the link of the playlist. In that video number 4, you will get familiar with the operators. So, you have to see each video in sequence. Because in this video series, in this way, I am preparing for the next video, you will understand the next video very well. Okay. So, we have implemented this. Now, let's run it. So, I have entered 10. So, you can see that the number is even. Let's run it again. And I have entered 9. So, it is printing number is odd. So, it is running properly in both the cases. So, in this way, we have fulfilled the requirement. So, you have seen how we can use the same method. Now, where can mistakes be made in the case of F4Ls? And there are small but big things that are there, I will tell you. Okay. So, let's remove the printf scanf. Let's remove the variable also. Okay. So, let's run it straight away. 5 graded on 2. Let's write it here. True. And let's write it here. False. Now, this is going to be very interesting. These things I am telling you are very interesting. Because you will get to see these things in very few classes or videos. Very few people explain these things. So, sometimes, when we use semi-colon programming, we use semi-colon after every line. So, what will happen by mistake? Even after if, we use semi-colon. So, after if, when we use semi-colon, what does semi-colon do? It terminates the statement. It means, this if has been terminated here. And this printf, else, and printf, this has been omitted. Omitted means, this is not associated with if. Okay. So, what does this mean? That now this printf, else, and printf, this if statement is not connected with it. It means, it will treat the compiler separately. Now, tell me one thing. Without if, we cannot write else. Okay. Without condition, you cannot write else. In that case, there will be an error. So, whenever you put semi-colon after if, and you will save it and compile it, the compiler will show you an error. And this error will show else without a previous if. So, we stopped if here. It means, this if is independent. It has no meaning with else. It cannot be implemented in real life. That is why this error is there. So, always remember, after if, never use semi-colon. Okay. Now, if I remove it, and then I put semi-colon, then tell me what will happen? Should error be there or not? So, if you are saying that error should not be there, then you are right. And if you are saying that error should be there, then you are wrong. Why? Because else is not there. If you put semi-colon, then this is not connected with if. So, if it is true or false, then it will not matter. It will always be there. Okay. If it is true or false, then it will always be there. So, if condition is true, then it will be printed. Okay. True printed. Now, let's false it. 1 greater than 2. Now, it is false. It is true. Why? Because printf does not matter to if. If you put semi-colon, then when you remove the semi-colon, now these two are connected. Now, because semi-colon has such a big role. Now, you will understand that if you use semi-colon and if you do not write else, then what will happen? If you use semi-colon and if you write else, then what will happen? Okay. So, you will understand this. Now, we will put semi-colon after else. Let's see what happens. If condition is true, then it will be printed and after else, you put semi-colon. Now, you can easily understand that this printf is not part of if else. It will always be there. Okay. So, one way is that it does not matter to write else. If you put semi-colon after else, then it does not matter to write else. Because this printf will always be there. When it should be there, when if condition is false. So, if you put semi-colon, then you are separating it. So, if we run it, then you will see that both the things are printed. True is also printed and false is also printed. So, it will happen many times that you will put semi-colon after else and if the output is incorrect, then you should also take care of it. Okay. Now, let's talk about when I taught you operators, if you watch operator's video carefully, then I had told you that zero means false and non-zero means true. Okay. Zero means false and non-zero means true. Now, let's see it here. Practically apply it. So, I have written zero in if. So, you will say that this is not a condition. How can we write it? But we can write it because zero, non-zero also indicates true and false in programming. So, let's see the output of this. False. I will run it properly, there is no problem. And if we write here five, then it will indicate true because if it is non-zero, then it will be true-printed. If it is minus five, then it will be true-printed. So, any non-zero value whether it is positive or negative, it always indicates true and zero always indicates false. Okay. And if you want to make a variable store five in that variable and then use it here then it will still behave the same. True-printed. So, in this way, you have understood that we can directly use the constant values and we can use them by wrapping them in the variable. If the value is zero, then false. If the value is non-zero, then true. So, in this way, you will treat them. Okay. Now, we often write this like n double equals to five. So, what is the value of n? So, both are equal. So, what will be printed in this case? True-printed. Okay. Now, by mistake, often you will put single equals to five. And you are comparing it to zero. Okay. Or let's assume that it is compared to two. n is five. And we are comparing it to two. And by mistake, we put single equals to instead of double equals to. So, should this be an error or not? So, you will say for once that this is an error. You cannot compare it like this. But, single equals to also exists. And what does it do? It assigns the value of the right-hand side to the left-hand side. So, if you compile it, it will not show any error. Because this is a valid statement. So, this is a logical error. That you have put double equals to or single equals to. So, what will happen in this? Two will be assigned to n. So, this will basically convert to n. And what will be the value of n? Two. So, finally, this will be f2. And two is a non-zero value. So, this will be considered true. And its result will be true. So, if I write n equals to two. Save it. And run it. So, you will see that it is true. And if we write zero here, then it will be false. So, this means that if we write this calculation in f, then it will be solved properly. And then according to it, true-false is decided. But, if you write this, like we have checked n-modular-2 and then you will write equals-2. So, you have to keep in mind that the variable on the left-hand side will be valid then. If there is an expression, then it will be invalid. Because n-modular-2 will give you a constant. So, if n is five, divide it from five to two, then it will remain one. So, what will be the equation? One equals to zero. So, can we assign zero in one? No. Because nothing is assigned in constant. So, if we save it and compile it, l-value is required as left operand of assignment. That means, on the left-hand side of equals-2, the variable is required. You cannot use the expression. So, if we put double equals-2, then it will be valid. Otherwise, it is invalid. So, these are small things that you have to keep in mind when you use equals-2. So, I hope this is interesting because there are hidden facts that are not told in the classroom or you will not be able to see them in many more videos. So, I hope you have understood and if else, it is not complete yet. I have told you two parts simple if and if else. The advanced version of this will come in the next video in which you will understand nested if and else if. I will tell you practically in all the programs. So, do not forget to watch these videos and the full series link is available in the description. So, please follow my videos if you are a beginner and you want to improve your programming skills. These are my details that you can contact me for classes or to solve a problem. So, I hope that whatever I have explained you have understood. Thank you for watching this video.