Summary
Keywords
Full Transcript
Hi everyone and welcome to the Java placement course. We will learn in this lecture 3 and conditional statements. Let us start with a positive attitude. Let us fill out today with gratitude, thankfulness.
Nice skill to learn and definitely learn a new skill. We will learn a lot in life. Let us start with the lecture 3 in which we will learn conditional statements. Let us fill out today's topics in the FL, L, S, Switch and Break.
Let us learn some key words in the language of Java. What are the key words? In English, there is a dictionary in which all the words are written. All the valid words in English are the rest.
The rest do not belong to English. The Java key is a dictionary in which words are stored. And the word is fixed. We cannot use the word in other words.
We cannot use the name of the word in the variable. The word is called the key words in the Java. All the keys are the key words. First of all, we have learned the input output in the Java.
What are the variables and what are the data types? We are learning all these things in this class. You will understand the best in this class. When you have seen the first class, the basic Java.
Let us start with the FL statement and conditional statements. If there is a logical statement in English, If the age of the above is a adult, then it is not an adult. We have talked about a simple thing in Hindi. Now we will translate it into English.
If you are older than 18, if your age is greater than 18, then you are an adult. Else you are not an adult. We have talked about this in English. That is, if or else we use the word in English.
Now in these conditional statements, we write the words in the Java key words in the FL. Conditional statements are called a condition. What is the condition on the age? If the age of the above is a condition, if it is true, then it is an adult.
So, in these conditional statements, we write the Java key words in it. If we write the syntax in it, then we represent it in some way. The Java key words in the normal code will be written in some statements. And in the middle, the FL will be written in the FL.
We will write it like this, if there is a condition in the parenthesis, or there is a condition, then we can write it as number n greater than 18, or our age greater than 18, or if our age is equal to b, now in the Java, we only write the two times in the same way. So, age equal to age equal to if b is, then it can also be a condition. If our Boolean variable is, we know that this is an adult. Is it true that its value is true?
It can also be a condition. So, in this way, if there is any condition in the F, if it is true, then the curly braces are in it. The ones in it, the ones in it, they will run all. But if that condition falls, then in that case, the L's, curly braces, the statement, statement 5 and 6.
So, in some way, if L works in the Java, now we try to understand it by simple example. First of all, what we will do is, we will take the input from the user, age, we have learned how to take input in the last class in the Java. The first step is, check the second step, if our age is greater than 18, then we will print it. Adult.
No, that means, L is printed, not adult. It is a very simple work. Now, let's do the simple work by the code of the code. First of all, we will make a scanner class object.
We will use the scanner class, to take input in the Java. Apart from that, there is a buffer reader, we will read it in advance classes. It is less time, that means, the code is fast. Now, in the basic stage, we will do it slowly, then we will understand more things.
We will write the system.in. Now, the scanner, to use this, we have to import a package, Java.util.star. Let's take everything. For example, the input for the input.
We have created the object of the scanner class. What we will do after that, we will do the input in.in. A is equal to sc.next.int. Now, we have written the input in the F.L.
So, if age is greater than 18, we have written here, this is condition. Then, we will print it. Adult. Now, you must be thinking that I have typed S.Y.S.O.
and then how the whole statement came out? The code is in the short. You will type S.Y.S.O. and then, you will print the tab on the keyboard.
So, the whole statement will be printed. Then, we will write the F.L.S. Then, we will print the case. We will not add the print.
So, in this way, we will print the case. If the user has aged, it is not adult. If age is greater than 18, then print adult. Else, system.out.println.
Not adult. So, the whole code is saved. Control S. Or, go to the file and do run.
After running, we will not debug it. So, we will give the input to our age. What we want to write is, first of all, we will enter 15. So, 15 is obviously not adult.
Now, we will run it again. And, what will we do in age? We will do 21. So, if we pass 21, then adult.
So, in some way, if age is greater than F.L.S. Now, let us take another example. The next example, which we are leveling up. Now, what will we do in the example?
We will check, we will take an input from the user. Okay. Now, we will check for this number, whether it is odd or even. Now, if we check for odd or even, we will not learn it.
So, we will check for odd or even. We have an operator in the Java, which is called ModLow. What is ModLow operator? This is calculated as remainder.
If we have to do plus, then we have to do plus operator. If we have to do minus operator, we have to multiply operator. So, multiply operator. So, if we have to do this in the Java, then the modulo operator is If we have to do x modulo y, then x divided by y, then the remainder will come.
The remainder will be in the output. So, whenever we have to do x divided by y, then we have to do x modulo y. But, what is to take the modulo from odd or even? So, here we will do logic.
The logic is that we have learned in maths, that we will divide the number from 2 to the power of 2. And, if the remainder is 0, then what we would say is, we would say even. So, x divided by 2, if we have to do this, then we would say even. But, x divided by 2, if we do not give the number, then what is the number?
odd. Now, the divide is in the Java, then we can give the quotient in the Java. But, we can take the remainder from the modulo. So, what we have to do is, we have to do simply, we have to do simply that, if we have given a number, which we will get from the user, we will say that it is x, then we will check the modulo with 2.
If it is 0, then that is even number. But, if it is not 0, then it is odd number. So, that is odd number. The same logic is written by the code.
What we will do in the code? We have taken the input first, and we will take the input this time, the number x. x is here with us. If x modulo 2 is equal to 0, then print it in that case.
Even, that is, a number of numbers are completely divided with 2. As you print it, odd. So, in some way, if and the help of the else, we have seen that the number of numbers are odd or even. We save this code, and run it.
After running, we have the number, we have to give it, so let us give the number first. 5. 5 is odd number, the output is exactly the same. We will enter this time, we enter this time, 2.
2 is even number, so, in some way, logically, if we are able to use the else, to know that our statement, how the code represents. Now, we will see one more question, and see, we will learn some new concepts from this question, first of all, how will we easily solve this question? So, the question given to us, what you do is, use it, this time, 2. If a, is equal to a, then print it in that case, if a, is equal to a, then print it in that case, if a is equal to a, then print it in that case, a is lesser.
So, in this way, you have two three conditions, you have to make it in the form of if, and see how we will do it in the code form. First of all, we will take this, what we are taking first, we will take the x, similarly, we will do the next int, now, we will check, if a is equal to equal to b, then print it in that case, but else, if it is not like this, then what will happen in that case? Let's check in that case, because a, b is bigger than a, then what will happen in that case? a is greater, but if it is not like this, that means, a is smaller than a, then in that case, a is lesser.
So, we have written the code, if a is equal to a, b is equal, then print it equal to l's, if a is bigger than a, then print it in a is greater, l's, means if it is less, if a is bigger than a, then print it in a is lesser. Now, let's run this code, because we will pass a, that is 2, b, that is 5. So, a is lesser, very clear. So, what I have understood from this?
If we only give a if-else, then after a if-else, there are many conditions, which we want to check. That means, if we have checked, then we have to check the greater, what is less? So, in this case, we have to write all the conditions inside the if-else. So, the code is not clear yet.
So, in the java, the solution is created. In the java, the solution is created, we call it else if statements. Else if, you have checked the if-else, if it is not, then you can do else from the other side. You can't write everything inside the else.
In this way, it will be syntax. You have checked the condition of the statement 1, statement 2, if the condition 1 is true. But if it is not so, then you can check the condition 2 else if, if it is written, then it will be executed in 3 or 4, then you can write it in the end, and then you can write in the end, in the final, else. Now, you can say, we can write it in the bar bar if, if it is written in the if, then the condition 1, the second, the condition 2, and then, in this way, we would check our condition.
But again and again, if you write it, then the first one will also be checked, the second one will also be checked. But when you write the if-else, then the first one will be true. So, this will not be checked. But if the first one is not true, then this will be checked.
If the first one is not true, then this will not be true, then this will be the case. So, in this way, our if-else works in the java. Now, if we want to change the form of the if-else, then how will we do it? If we want to keep the value of the if-else, then we will make this the case.
Else, if, and this value of the if-else is not true, then we will make this the case. So, let's take a look at the if-else is equal to b, then this condition, Else, if it is not true, then again, you should check the if-else is greater than b, then a is greater. Else, you should check the if-else is less. So, let's clean this up a little.
And save. Now, in the java, there is one thing that, which is the curly braces, you open the same line on the same line, and the closing one, it comes in the next line. So, if you will do it here, then there will not be any problem in the code, but there is a convention to write the java, then you do it on the same line. So, this basically shows that you are a good coder, you are doing it from a good time, then you will know which language you are using, which rules.
So, we have used the L-SIF in this way, now we will run this code once and verify it. This time, we will pass it in a, in a, in a, in a, in a, in a, then a is greater. So, the code is absolutely right. Now, let's do a little quest questions, let's practice a little bit, whatever we have learnt.
First of all, the question given by the quest, is if three is an odd number, it will be a print. Now, here, the odd place, should be even, should be a print. This is a little printing mistake. If three is an even number, print will be the same.
And here, multiple choices will be correct, that is, one more, which is your option, it can be the same. Now, first of all, analyze, if three is an even number, print will be the same. So, first of all, we analyze the first option first. Let's see, what is the first option?
The first option is, what is the question given? First, option is if three modulo 2, that is, whatever number was three, modulo 2, we will check, if it is an even number, then print will be the same. It is absolutely correct. So, option A is correct, because statement, syntactically, syntax wise is absolutely correct.
Now, let's talk about option B. If three modulo 2 is equal to 0, check, if it is an even number, then print will be the same. Now, in this case, there are no curly braces. How did this happen?
Now, the fact is that, if you have curly braces in the Java, then it is more logical to write, when you have to print only one statement. But in the DDS, there are no curly braces. How did this happen? So, in the Java, in the curly braces, your multiple statements can be found.
So, all the prints will be there, if it is true. But, if your single statement is there, and you will write without curly braces, then it will be the print. So, if you have written anywhere, and if it is true, then the next statement, the statement is connected with the same one. So, in this way, without curly braces, you can print a single statement.
But, if you had to print something else, in the next line, if this statement is true, then you have to write curly braces. Because, if you didn't write curly braces, then only the first statement was executed after the first statement. So, this option is also, this is the correct option. Now, if we talk about the option C, then there is F3 modulo2, E is equal to 0, same condition, if we check the F3, then what is the even?
But, here, you have seen a special terminator. Terminator, now, you have to finish this line here. Because of this terminator, this next line, this next line will always be executed. That means, if it is true, it is not a difference, the next line will always be printed.
So, this statement is incorrect. That means, it didn't follow the logic we had written. That's why, the ABC option is, A and B are the same statements. C option is wrong, because the terminator is here.
If the terminator is not here, then this is a correct option. After this, let's talk about another question. We will learn to solve this question, which is the meaning of switch, in the Java. Now, the question is, why you have given a robot?
There are three buttons on it. First, you press the button, then it says hello. Second, you press the button, then it says hello. And third, you press the button, then it says bonjour.
Now, bonjour, bonjour, bonjour. Friends, I don't have an idea that I am pronouncing it right or not. So, I am really sorry if I am pronouncing wrong. So, in some way, if you press 1, 2, 3 buttons, then your different greetings, will be printed.
First of all, let's talk about the help of this thing, after that, we will see how the switch's story comes after the effort. So, we know what we need to do. First of all, we will take the input button, so, take the input button, sc.nextint. After that, if we have one button on our button, then print it in that case.
Hello. But if we have two buttons on the button, then print it in that case. Greetings. But if we print it on your button, then print it in that case.
Bonjour. And if you have pressed the button, that means, don't press that button, it is an invalid button. So, we will write it here. Invalid button.
Let's save it. So, the logic is, if you have input button, then print it. Hello. If you have two buttons, then print it.
If you have three buttons, then print it. And if you have pressed the wrong button, then print the invalid button. Let's run all the codes. First of all, we want to press the button.
So, we have printed it. If we want to press the button again, then press the button number 2. So, for us, Hello. So, now, the code we have, we have written it as if, if, if, if, if, if, if, if, if, and here, there were only three buttons.
There were only two buttons. Then, again and again. The button is equal to the button. It is equal to the time when it gets disturbed.
So, here, the Java has a lovely concept, which we call as switch. What is switch? Like, on our home, we have switch on the light board. You press the button, and run it with a stick, and it runs with an AC, and it is getting light.
In the Java, switch is switched. You can use a machine, which you have given an input, which button you want to press. The input case will start with the case, which case is matching with this button press. And the case will match, then, it will work on you, and it will work on you.
Now, if we talk about the syntax, then it will show you something like this. So, in the syntax of switch, we first write the switch. After writing the switch, we write the parenthesis, there are no variables in the parenthesis. This is the button, which you pressed.
Now, this button's value can be a number, like 1, 2, 3, 4, 5, or, there is a character, A, B, C. Here, we have taken the number case. Now, the button which is matched with the case, will be executed. That is, if you pressed the button 1, then it will match with the case 1, and if you pressed the button 2, then it will match with the 2.
So, we write the case A, case B, case C. So, in some way, characters can also be, but here, there are numbers. So, if you press the button 1, then the statement 1, the execute will be, and then the execute will be break. Now, what is this break?
So, the break is also the keyword of the key word. Now, as soon as you come on the break, you will go out of the switch. So, as soon as you encounter this statement, the curly braces, we will go out. But if this break is not here, then what happens?
All the statements of the bottom are executed. Until we have no break. So, after every case, there is a break after statements, so that you execute the statement and then you place a break there. And now, the switch will come out.
So, the case is true, so this statement is executed and then break. If the case is true, then this statement is executed and this break. And the special case, which is the default case, is true, then this statement is executed. And we do not need to break in the end.
Because you know that the statement, the curly braces, if you finish your break, then you will break. So, in some way, the switch is in it, you pass the input, or press the button, then you place the cases in your brain and then you print something. Now, what we did with the robot, we do with the help of the switch. So, we will comment the whole code and we will comment what we do with the code.
We will comment what we do with the code. We will ignore the code, which is a compiler. This is not the code part, this is normal English, which is going to ignore the code. And in our code, this is also executed, it will not happen.
So, we will learn about the comments in the next classes. If you will delete it completely, then we will also go. We are just keeping the comparison. So, we will write in the code, what we will pass the switch in our button.
And here we will write, case 1, case 1, what is the case 1? You print it, hello. Hello, we print it. We have done it again.
Break. Then we come to our case 2. What is the case 2? You print it, hello.
Hello, we print it, break. Then we come to the case 3, case 3, we say, you print it, bonjour. Bonjour, we printed it, after that you print it, break. Then we come to our default, that is, special case.
In default, we print it, invalid button. Invalid button, when you print it, you will not need to break it here. So, in some way, our FL statements, that was converted into the switch form. And if you look carefully, then it looks very clean.
So, in the case of the switch, you will see the switch code. So, this is why we use the switch, which is used a lot, when we are sleeping in the FL statements. And we need to clean our code a little. So, we will run the same thing, go inside the Java.
And press the button, press press button number 2. And we have a sprint from this. Hello. So, in the Java, we have covered the FLs, L-SIF, switch and break.
In the next description box, you will get notes. Along with that, you will get an important homework problem. The homework problem is, that you have to make your calculator. Now, the homework problem is important, because the concept you learn, you have to do it yourself.
And when you will do that problem, then you have to try it yourself, at least one hour, whether it is coming from you or not. And if you have a little problem, then the homework problem solution is also given. So, you will refer the solution. You will learn new things from that.
And as much as we will learn, we will continue to continue. So, I hope that today's class will be helpful, to understand conditional statements. And to understand the Java a little more. Today, we will meet again in the next video, Till then, keep learning, keep practicing and keep exploring.
