Speaker A
Okay, so in the last class, we have seen how to work with the different types of operators in Java. Now today, let's continue with some more important topics, some basics, control statements. So the next topic is control statements. Normally, when you write your Java program and when you're executing it, the statements will be executed top to bottom. Let's say if you have written 10 statements or 100 statements, the execution will always happen top to bottom. So each and every statement will be executed in a sequential manner. Let's say these are the different statements, and execution will happen like this. So each and every statement will be executed, right? But sometimes, if we want to execute specific statements based upon certain conditions, let's say now I want to execute only this particular statement based upon a certain condition, and I want to execute another set of statements based on another condition. That means every time you don't need to execute the same set of statements. So based upon the condition, we can also choose the statements which we want to execute. Okay, this is basically called controlling your statements. It means instead of executing each and every statement in your program, we can select a set of statements or group of statements, and we can execute only those group of statements based on a certain condition. Okay, this is one scenario. Another scenario is suppose I have a set of number of statements, and I want to repeat the same statements multiple times again and again based upon a certain condition. I want to repeat the same steps, same statements again and again multiple times. This is basically called a loop, and we can execute the same set of statements multiple times repetitively. We call it a loop, and we can execute a set of statements based upon a certain condition. This we can achieve by using conditional statements. So basically, we can control the execution of your program or execution of your code by using control statements. There are three kinds of control statements, and by using those control statements, we can control the execution of your program or execution of your code. So mainly, there are three kinds of control statements in Java. So the first type is conditional statements, conditional statements, conditional statements, and the second is looping or iterative statements, and the third is jumping statements. So these are the three kinds of control statements which we have. So what is the use of these control statements? We can control the execution of your program or can control the execution order of your program. That we can achieve by using conditional statements or looping statements or jumping statements. So we will discuss today conditional statements, mainly conditional statements, and of them, we'll discuss in the next sessions. So what are the conditional statements available in Java? So there are four types of conditional statements. Conditional statements, there are four types of conditional statements. One is only if condition, if condition. Second is if else condition. Third is nested if else, and fourth is switch case. So these are the four different conditional statements supported in Java. So the purpose of using the conditional statements is the same, but we will use these statements in different scenarios: if, if else, nested if else, and then we have a switch case statement. So these are the four different types of conditional statements supported in Java. So now we'll see practically how can we use these conditional statements and where we can use these conditional statements with different types of examples. So the first condition statement is if. So how to use this if condition? The syntax of if condition is like this: if, and here we put some condition, and if this condition is true, then whatever steps we want to execute or whatever statements we want to execute, we can keep those statements inside this block. So this is called syntax how to use if condition. So if this condition is true, then only these statements will be executed. If this condition is false, these statements will be ignored. These statements will not be executed. So this is only if condition. Let me show you how to use this, and there is no semicolon for if condition. Okay, if you need to put some condition, open bracket, closing bracket, and what are all statements you want to execute based on the condition, we can specify them inside this block. Now let me show you some example. Go to Eclipse, and this is our project which we already created. Now today is day four. Let's create a new package, day four, and click on finish. So how to use if condition, only if condition. So let's create a new class, and I'll name it as IfConditionDemo. Take this main method also, and then say finish. Okay, so now my requirement is, my problem statement is, for example, I will take a person's age as a variable. Let's say personAge equal to, let's say, 25. This is the variable I have created. Now my requirement is I want to check this person is eligible for voting or not. So according to my condition, if the person's age is greater than or equal to 18, he is eligible for voting. That I want to verify. Okay, so I have a simple variable created, personAge equal 25. Now I want to check this person is eligible for voting or not. So if you want to check this person is eligible for voting, simply we can check the condition if the person's age is greater than or equal to 18. We can simply say he is eligible for voting. But how can we verify that condition? How can we check this person's age is greater than or equal to 18 or not? So now we can write one conditional statement. So if condition can use if, in the bracket we can specify the condition. What is our condition? The person's age should be greater than or equal to 18. Then only he is eligible for voting. So here I'm taking that variable, whatever variable I defined, that is personAge, that should be greater than or equal to 18. So will it return true or false? What is that operator we have used here? Greater than or equal to, which is a relational operator, right? So what is relational operator will do here? It will compare the personAge is greater than or equal to 18 or not, and if it is greater than or equal to 18, it will return true. If it is not, return false. That means here we have to always specify a Boolean expression. Here we have to always write a Boolean expression which will return true or false. So if the condition is true, if the expression returns true, then we can specify the statements which we want to execute based on the condition. Here inside this bracket, I can say System.out.println eligible, eligible for voting. Like this, you can specify. Now this particular statement will execute only if this condition returns true. Only if this expression returns true, then only this statement will be executed. And if this statement is written false or if the expression is written false, the statement will not be executed. That means based on the condition, we are executing this statement. If the condition is true, execute the statement. If the condition is false, then don't execute the statement. Okay, for example, let's say I'm taking 25. 25 is greater than or equal to 18 or not? Yes, the condition is true. True. So if the condition is true, then obviously the statement will be executed. So when I execute, run as a Java application, so now we can see the output here. So statement is got successfully executed because the expression is true. Now I'll just make this as 15. Now the personAge is 15. 15 is greater than or equal to 18. Expression returns false. So if it is false, the statement should not be executed. Now when I run this code, you will not get any output in the console window. So no output because we are not specifying anything else other than the statement. So only one statement is specified, and this will be executed only if this expression returns true, right? So this is how we can simply use if condition. If t











