Session 4- Working with Java Conditional Statements | Java & Selenium

SDET- QA11,319 words

Full Transcript

okay so in the last class uh we have seen how 

to work with the different type of operators in Java now today let's continue with the some 

more important topics uh some Basics uh control statements so the next topic control statements 

so normally uh when you write your Java program and when you're executing it so the statements 

will be executed top to bottom let's say if you have written 10 statements or 100 statements so 

the execution will always happens top to bottom so each and every statement will be executed 

in 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 uh if we want 

to execute a specific statements based upon certain condition let's say now I want to 

execute only this particular statements based upon certain condition and I want to execute 

another set of statements based off 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 uh controlling uh your statements means 

instead of executing each and every statement in your program we can select set of statements or 

group of statements and we can execute only those group of statements based on certain condition 

okay this is one scenario another scenario is suppose I have set of number of statements and I 

want to repeat the same statements multiple times again and again based upon certain condition I 

want to repeat the same steps same statements again and again multiple times this is basically 

called as a loop and we can execute the same set of statements multiple times repetitively we call 

it as a loop and we can execute set of statements based upon 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 are there in Java so the 

first type is conditional statements conditional statements conditional statements and the second 

is looping or item ative 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 are available 

in Java so there are four types of conditional statements conditional statements there are 

four types of conditional statements one is uh only if condition if condition second is 

if else condition third is nested if Els and fourth is is switch case so these are the four 

different conditional statements supported in Java so the purpose of using the conditional 

statements is same but we will use these statements the different scenarios if if Els 

nested if Els and then we have a switch case statement so these are the four different type 

of conditional statements supported in Java so now we'll see practically how can we use these 

conditional statements and where we can use this conditional statements with the different type 

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 uh 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 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 a 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 examp go to eclipse and this is our project which we already 

created now today is day four let's create 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 

if condition demo take this main method also and then say finish okay so now my requirement 

is uh my problem statement is for example uh I will take a person age as a variable let's say in 

person uncore age 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 o or not so according to my condition if the 

person age is greater than or equal to 18 He is eligible for OT that I want to verify okay so 

I have simple a variable created person age equal 25 now I want to check this person is eligible 

for V or not so if you want to check this person is eligible for what simply we can check the 

condition if the person age is greater than or equal to 18 we can simply say he is eligible for 

order but how can we verify that condition how we can check this person age is greater than 

or equal to 18 or not so now we can write one conditional statements so if condition can use 

if in the bracket we can specify the condition what is our condition the person age should 

be greater than or equal to 18 then only he is eligible for order so here I'm taking that 

variable whatever variable I defined that is person uncore age that should be greater than or 

equal to 18 so will it return true or false this 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 returns 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 return true or false so if the condition is true if the expression is return true then we can 

specify the statements which we want to execute based on the condition here inside this bracket 

I can say system. println eligible eligible for p like this you can specify now this particular 

statement will execute only if this condition is return returns true only if this expression 

is returns true then only this state 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 WR true now I'll just make this as a 15 now the personage is 15 15 is greater than or equal to 

18 expression return false so if it is a 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 

specify anything else other than the statement so only one statement be specified and this will 

be executed only if this expression is return returns true right so this is how we can simply 

use if condition if the expression returns true statement will be executed if the expression 

return false statement will not be executed so this is how we can simply use if condition 

only if condition but if it is not true then what should happen we haven't specified that we 

haven't specified that suppose if the condition is false what should happen if the condition is 

true the statement will execute if the condition is false what should happen that we haven't 

specified here so this is only if a statement we haven't specified any else this is only if 

a statement if the condition is true statement will execute if the condition is false state will 

not be executed that's it only if condition the first one okay but sometimes we should also 

specify else part that means if the condition is not true then what should happen that also we 

have to include right so for that we use another statement if along with else if block along with 

else this is called a block you can put one single statement here or you can put multiple statements 

also allowed in if condition you can keep single statement or multiple statements okay sometimes 

if the condition is false then what should happen that also we can specify in the else block so 

this is only if condition okay now how can we use if and else block combinations so the syntax 

will be like this if we put one condition if the condition is true then the group of statements 

will be executed and and if this condition is false for example if this condition is false 

then we write another block called else block like this so if the condition is true then these 

set of statements will execute if the condition is false then else block will execute inside the 

else block also we can write the statements okay now if the condition is true if block will 

execute if the condition is false then else block will automatically execute this is alternate 

okay now this is called if else condition if else condition okay so how to use this ifs condition 

so let's say uh save this let me create another class suppose if condition is 

uh if condition return return false so then what we should happen 

let me just rename it this is if El condition okay so this is a if else condition same 

program I'm taking the person age if person AG greater than or equal to 18 He is eligible for and 

if this condition is false then what we should do else we have to write else Block in else block we 

can specify and what we can specify not eligible not eligible for what like this so based on the 

condition we are choosing the statements right if the condition is true if block will execute if 

the condition is false else block will execute at a time only one block will be executed both 

will not be executed because the condition uh will return true or false right if it returns 

true if block will execute if returns false else block will execute now when I say 15 the 

condition is false the expression will return false so obviously else block will execute okay 

so not eligible for either this one or this one only one block will be executed always okay so 

this is how uh we can use El okay so if condition and then else along with else and you should 

not put semicolon for if condition like this okay for else also should semicolon only the 

statements which we have included inside this block we have to specify the statements okay so 

if and else block like this fine so sometimes you may have multiple statements inside this okay so 

here if you have a single statement you can put multiple statements or single statements suppose 

if you have a single statement here the brackets are optional okay the curly braces are optional 

you can directly keep the statement inside with condition and here also if you have one single 

statement in the lse block these brackets are optional okay so let me show you so currently 

I have single statement in every block right so what I will do is I'll try to remove the I 

will try to remove the C braces just observe this okay now just observe here I just try to 

remove the Carly press and here also I'll Remo it okay so now just observe when I running this 

so you will get a not eligible for now I'll take another number 25 and you will get eligible 

forward so how come it is executing so that means if you have a single statement under the 

condition you no need to specify the curly braces you can directly write under this so if the 

condition is true the statement will execute and if the condition is false else statement 

will execute so if you have a single statement you no need to put in the CRA but if you have a 

multiple statements you have to include them in the curly braces okay so what I will recommend is 

even though if you have a single statement better to keep inser the curly brace why because you 

clearly know whether the statement is belongs to this condition or not so by seeing the code 

you will understand this particular block will be executed based upon this condition okay so 

if you have a single statement curly braces are optional and if you have a multiple statements you 

should keep inside the curly places okay that is another one you can just remember this so This 

is called if else condition so if condition is true if block will execute if the condition is 

false else block will execute at a time only one block of statements will be executed okay now 

let me show you one more example for if else condition so the first example we have already 

seen elig ible for V or not now the example two so suppose we Define one number and we need to 

find out that number is even or odd number so we provide one number and that number we have to find 

even number or odd number through our program okay so how to find out so normally if I ask you if I 

give some number like this let's say I've given 10 so if I ask you whether it is even number or odd 

number how we will tell how we will know whether it is even number or odd number we will simply 

say it is an even number when you say five you can simply say which is an odd number but how we 

will know whether this is even number or not this is even number and this is odd number so how we 

will know whether is even number or odd number so we have some mathematical calculation behind that 

right so if any number divided by by two returns zero as a remainder that is an even number okay 

for example let's say if I take any number let's say 10 10 ided 2 returns zero as a remainder 

that is a even number right and 10 ided by two suppose 5 divided by two which is written one as a 

remainder so other than zero whatever remainer you will get other than zero they all comes under odd 

numbers it can be one or it can be anything else right this is a mathematical calculation simply we 

can say so if you want to say a number is a even number that number divided by two should return 

zero as a rem remainer and if you want to say a number is an odd number when it divided by number 

by two that should return non zero other than zero you will get any number as a reminder that is an 

odd number that is a logic so now programmatically we have to find a number is even number or odd 

number okay and how can we find out so again based on the condition if a number divided by 2 equal to 

Z if condition is true then we can say even number and if not we can simply say odd number so based 

on the condition we can check so how can we use if Els condition here let me create a new class even 

or odd number so how to check a number is even number or odd number so what we can simply say 

first we'll take one number in number equal to 10 so I have taken I have defined one variable right 

so now we need to find this number is even number or odd number so how can we check if this number 

divided by two divided by two which is returning zero and uh which operator we have to use here W 

equal to or equal to equal or equal to which one we have to use condition if number percentage two 

returns zero as a remainer we we have to compare this the remainder whatever is written by this 

expression should equal to zero or not we want to compare so we have to use comparison operator 

that is double equal to right so if number percent two why we are using percentile instead of Slash 

because percentile operator will return reminder right it is also division operator but it will 

return return the remainer that we want to check so number percent 2 equal to0 that means if 

this is true when this expression true if number percent Del to returns zero then this expression 

will returns true overal expression right Boolean value will be return so if this condition is 

true then what we can simply say that number is even number okay that number is even number and 

other than zero okay you suppose this condition Falls whenever the remainder is non zero that 

means other than zero if it is return any other value this condition become false so in that 

case what we can simply say it is a odd number that you can specify in else block else block we 

can say system. print odd number like this so at a time only one statement will be executed based 

on the condition so if the condition is true if block will execute if the condition is false else 

block will execute so when I execute this it will say Java application even number so we can say 10 

right so 10 is a even number let's say 15 15 now condition is false so obviously else block will 

execute which is odd number okay so this is how we can simply use if else condition okay if FSE 

condition we can use now let me show you some more examples example three we can practice so many examples like this 

is and number of examples you can find so now I want to check a number is positive negative or 

zero check a number is positive or negative or zero so if I provide a number as an input and 

the program should display whether that number is positive number or negative number or zero so 

normally how can we check a number is positive negative or zero as per mathematical calculation 

how can we verify number is positive negative or zero how can we say on what basis we can 

say a number is positive negative or zero positive negative or zero how can we find yes simple calculation right if any 

number which is greater than zero that is positive number right if any number if I take any 

number as a variable if that is greater than zero obviously that is a positive number right and if 

the number is less than zero that is a negative number that is a negative number and if the 

number is equal to zero obviously that is a zero only right so we have multiple conditions 

here so far we have seen only one condition one if condition so sometimes we have to write 

multiple if conditions multiple conditions we have to verify if the number greater than zero say 

POS POS if the number less than zero say negative if the number equal to zero then say zero so here 

we have to check multiple conditions if you have only one condition then we can use only one if 

one statement one if statement here just like this if condition is true block will execute if 

the condition is false another block will execute only one single condition which we have here but 

if have a multiple conditions in case if we have a multiple conditions then how we can specify that 

so let me show you one more example take another class positive or negative number okay so how can we specify the multiple 

conditions okay first of all let us take a number in number equal to let's take zero first the 

number value is zero now we want to check which is zero or positive or negative okay so the what 

is the first condition if this number greater than zero if this number greater than zero then what 

we can say here it is positive number which is positive number fine suppose if this number is 

not greater than zero so condition is false then we have to verify another condition right how to 

verify another condition we can write like this else if else if again one more condition number 

less than zero then what we can say here system is a negative number it's a negative number even 

this condition is also false for example number is not negative less than zero then what we can 

specify the last option which we have that is zero only right so else here you can say system. 

out. zero like this so multiple conditions we can also verify by writing multiple if conditions so 

if number greater than zero execute the statement if this condition is false else one more if 

condition else number less than zero execute this statement even this condition is also false 

then else last statement will be executed so else block else block will always execute if none of 

the conditions are true okay if all of the if conditions are false finally else block will 

execute so here we only need to put another condition again you only need to write else 

if number equal to zero not needed because obviously else block will execute after execution 

of these two statements after execution of these two conditions if both the conditions are written 

false then finally else block will automatically execute so if you have a multiple conditions you 

can also write like this else give one space and then if so the structure will follow like this 

this is called uh if else later we can call it as a if else later so the syntax will be like 

this if condition one first condition okay else if they're not satisfied else one more condition 

if condition two and statements and even second condition is not possible not returns true then 

again one more condition else if condition three you can put any number of condition like this 

even this is also not satisfied then else one more condition else if condition four like this 

and if all conditions are done so finally if none of the conditions are becomes true finally you 

can write only one else blog like this this is called if else later if else later so this is 

the syntax which you have to use you can put any number of conditions if condition one is true 

statements will execute in this blog else another condition is true then execute the statement 

even this condition is also false then third condition it will verify even third condition 

is false then fourth condition will verify even this is also false finally block will execute 

so like this you can put any number of levels different levels and different conditions you can 

specify okay now let us test this code at a time only one block of statement will be executed so 

initial I say number equal to zero so when I say number value zero obviously it will return zero 

as an output now when I say 10 then is a positive number which is greater than zero so when I 

execute it is written positive number so when I say minus 10 minus - 10 is a negative number so 

when I execute it is written negative number the second block is got executed okay so at a time 

only one block of statements will be executed based on the condition we can control it okay now 

let me show you one more example so we can also use sometimes logical operators in the conditions 

okay so let me show you one more example example four so in example four uh is we want to find 

largest of three numbers largest of three numbers or you can call anything okay largest 

of three numbers I want I will provide three numbers the program should find out which one is 

the largest number okay for example uh let's take three variable let's say a = to 10 B = to 20 

C = to 30 like this now program should say C is the largest number or if B is the larger then 

program should find out and say B is the largest number or a is the largest number okay so before 

writing the program so nor normally let us think about the logic how can we find a largest number 

if I give these three numbers immediately we will say C is the largest number but how come you say 

C is the largest number on what basis you are saying C is the largest number do you have any uh 

mathematical thing here calculation to say which one is the largest number how we are saying 

actually we comparing each number with other numbers right so for example if a greater than b 

okay a greater than b and also a greater than C then we are saying a is the largest number right 

this is a condition or not first we are taking one number we comparing with the rest of the two 

numbers if this number is greater than rest of two numbers obviously that is the largest number 

same we are repeating for other numbers also for example if I take b b is greater than a a and also 

B greater than C then we are saying what B is the largest number B is a largest number same thing we 

are repeating for C also if C greater than a and C greater than b also then obviously C is a largest 

number so based upon certain condition we are saying after comparison we are seeing each number 

we are comparing with other numbers so if that number is if the particular number is greater than 

other two numbers we are saying that is a largest number or which a greater number so these are the 

actual conditions now the same conditions we have to apply in your programming also so if you want 

to say which one is the largest number we have to use multiple conditional statements okay and one 

if condition we can put multiple also right so this we can achieve uh by writing multiple if Els 

conditions so let's see how can we implement this practically so let's create a new class 

and here I'll say largest of three numers take this main method and say 

finish okay same logic we will try to apply here I keep this so that we can understand 

the conditions okay these are the three conditions to say largest of three numbers we 

have to verify these three conditions okay I put this in the comment so that you can understand 

the logic now let us try to implement this first of all we need three numbers let's create three 

different variables int a = 10 comma B = 20 comma C = 30 now I have taken three numbers three 

variables now I want to find out which one is a largest number so first let us compare a with b 

and c okay what is the first condition we have to check a greater than b and a greater than C then 

we will say a is a largest number right so what we can do it here is first condition if a greater 

than b this is one condition another condition is what a greater than C both the conditions 

should be true then only we can say a is the largest number a greater than b should be true 

a greater than C also should be true if both are true then only a is the largest number a greater 

than b returns true that means a Boolean value a greater than C also should returns true that is 

also Boolean value and if both are true then only we can say a is the largest number right so in 

that particular context which operator we have to use between these two Boolean values both 

should be true then only the final value should be true so which operator we have to use between 

these two expressions end operator end is what logical operator okay so here a greater than b 

and a greater than C so this time we specified two expressions remember this is one condition 

this is another condition both should be wrs true then only the final condition becomes true 

okay inside this we have written two expressions a greater than b and a greater than C both should 

returns true then only the if condition becomes true okay even if one is false then if condition 

will become false so in both Expressions both both the Expressions it should both returns true so 

the if condition is true then what does it mean here we can simply say a is largest number a is 

the largest number so first validation is done so this is done now suppose if this condition 

is false what does it mean if this condition is false what does it mean a is a not largest number 

right if a is the largest number OB viously this if condition becomes true if this if condition is 

not becomes true what does it mean indirectly a is a not largest number so we need to compare next 

number what is the next one b b greater than a and b greater than C then B is the largest number 

so how can we put another condition else if one more condition so here we can say a greater than 

b sorry B greater than a and B greater than C so now we are comparing B with A and C so if both 

are true then this if condition becomes true so here we can say system B is a largest number B 

is a largest number so now the second condition is also over now suppose even this condition is 

also Al return false so first condition checks a is largest or not second condition check B 

is largest or not if both the conditions are false what does it mean obviously C is the largest 

number is there any other case here nothing right if both conditions are false then obviously C is 

the largest number so do you really want to put this condition not needed right so directly you 

can write in else block condition is not needed you can simply right system C is largest number 

so for comparison of C there is no condition not required why because here we decided a is not 

largest number if it is false if the second condition is foral B is not largest obviously 

what is the other number we have is only C so we can say in lse blog we can simply say C is the 

largest number so this will execute only if about two conditions are written false so about two 

conditions written false means what A and B are not largest numbers obviously the C is a largest 

number so this is how we can implement the logic and here if I notice we are using logical 

operators along with relational operator the combinations because logical operators also 

will L true or false the if a condition required a Boolean value that expression whatever we have 

written is should return a Boolean value in the last class I think somebody asked where exactly 

we used the Boolean variables and Boolean values so here all the conditional statements looping 

statements Works based upon the Boolean values okay so if this condition returns true true means 

what booing value if the condition return false false means what is also boing value okay based on 

the value the statements will be executed now let us test this so first I'll say 10 20 30 so what we 

are expecting now C is the largest number so when I execute so you can say C is a largest number now 

let's make uh a is 100 now when execute you will say a is the largest number now let's make B is 

200 then it says b is a largest number okay so all all conditions are satisfied suppose instead of a 

b and c I just want to print a number also along with this a BC I want to just print number also 

so that I can clearly know which number is largest number so how can we customize the statement so 

a is the largest number whatever you have put in the double quotations it is as it is printing in 

the console window right now if you want to print the number also the same number I just want to 

print here simply what you can do concatenation let's plus a concatenation simple here also I 

want to print the Value Plus B concatenation so here also you can simply say plus C concatenation 

right so when I execute it along with this it will also give the number right so guys understood 

how to specify multiple conditions multiple if conditions along with multiple uh Expressions 

so if condition can have one expression or if condition can also have multiple Expressions if 

condition can have one expression or multiple Expressions okay so like this we can practice 

more number of examples by using uh if else conditions okay now let me show you one more 

example now this time you guys can tell the output uh I'm just writing a simple program I can 

say multiple statements so as I said before in if Els condition we can also specify multiple 

statements right single statement or multiple statements let me show you one more example 

so here uh if true and here I'm printing one else here I'm printing two first of 

all tell me this code is correct or not this code is correct or not valid code or not say yes or no everyone it is 

valid or not if true system P one else two can we put a Boolean value Direct if 

condition without having any expression so I have not put any expression so just I'm 

specifying a boing value can we put like this why not we can easily put right ultimately 

we need a Boolean value here right even if you say a greater than b or whatever what we are 

expecting from this a Boolean value right this will return return it true or false so instead of 

writing an expression I can directly write a boing value like this what's wrong in that right so 

if condition is always accepting a Boolean value and that Boolean value can be a direct value 

or it can written by some expression okay so when I say if true then what is an output of 

this statement so this is true then if block will execute right so when I execute this see 

here you got one can we pass false here false so if false then what will execute now if it 

is a false else block will execute so when I run this this will return true you got my point 

so what is my intention here is I just wanted to tell you instead of writing an expression we can 

also Al write a a boan value because ultimately the expression will written true or false a boan 

value will be written by the expression right in all previous examples in if condition whatever 

expression we have written all those expressions will return true or false a Bo value will be 

return returned okay so if it is a true then if block will execute if it is a false else block 

will execute I hope this is clear to everyone got my point here we are not comparing anything 

actually here we are not comparing anything just we are WR some Boolean value because the ultimate 

goal of expression is when I compare something it will return true or false right instead of 

comparison I'm directly say some Boolean value here it is valid because if condition always 

accepting either true or false boan value it can be directly or through expression whatever 

it is right you understood my context here in if condition you can pass an expression or you 

can pass a Boolean value directly that's what I'm saying okay for example let's say here I'm 

saying 1 equal to 1 can we put like this 1 equal to 1 yes because this will also returns true 

or false 1 equal to one true or false true if it is a true which one will execute if block will 

execute so it will print one okay when I say one equal to two the expression will return return 

false so else block will execute else block will execute so what I'm saying here is you can put 

either expression or you can directly put buan value But ultimately the if condition required 

a Boolean value okay the if condition is always required provide a Boolean value if it is a 

true if block will execute if it is a false else block will execute okay that true or false 

you can directly specify with the Boolean value or that comes through expression okay now let 

me show you some more examples now this time you guys can tell the output of this program 

okay so this is the one I'm commenting this at the time of practicing you can just uncomment 

and you can try okay this is second example now you guys can tell me the output 

of this program so I can say if true okay and inside this I will put one more if condition okay so let me read the question 

first when in If part you passed the Boolean value as a true okay then why it is passed 

first block and return Valu as one because if it is a true if it is a true obviously if 

block will execute right so if the condition is true if block will execute in if block 

what we have written here one only we have written that is the reason it is printing 

one okay if a condition is true obviously the if block will execute that will return 

one if the condition is false then else block will execute then it will returns two 

because we put one and two in the print statements okay yes now we can just 

see here if true again inside the if condition I'll put one more if condition 

this is called nested if else condition so the third one nested if Els condition the 

third approach nested if nested if Els means what one if condition can have another if 

condition multiple nested if Els condition you can spfy so nested if Els I'm showing 

here so if it is a true and enter into the block here I will again say true and then 

here I'm printing some value so I can say ABC ABC lse system print and XY J okay now 

after coming out of this if condition else here I will say 1 2 3 this is my code 

now tell me what is an output of this output ABC or XY Z or 1 2 3 which one what 

is an output of this code so this piece of code okay so let us try to understand this 

so first we'll start from here the condition is true here if it is a true it will enter 

into the block so what is the block of this if statement from here to here this is all 

comes under if block first outer if block okay and this is comes under first if block now 

if it is a true these statements should execute again if you come inside there is another if 

condition so if it is a true then ABC will execute else part will ignore okay so if it 

is true then ABC got executed so the output of this code is what ABC so when I execute it 

will return ABC because the outer if condition is true it come inside and then it this is also 

true then the statement is called executed so rest of them will ignore okay suppose when I 

say false here now tell me what is an output of this false so outer if condition is true 

so it is entered into this block but inner if condition is false if it is a false the else 

part immediate else block will execute that is XY Z so this is the output of this program 

XY Z okay now let us make this if condition false now tell me the output of this so if condition is false 

means these statements will not be executed so this if block will ignore obviously come to else block inside this we put 

one 23 so it will execute obviously 1 2 3 okay so this is how we can also write 

multiple if conditions and nested if else conditions one if condition can have 

multiple if conditions one if condition can have a multiple if else conditions 

so depends upon our requ requirement we can use these statements control statements 

okay so we have seen only if a statement if El a statement nested if nested if in the sense 

one if condition can have another if condition multiple times you can specify now let me 

show you one more example for multiple if conditions so example five so in this now I want to just print weak names 

display weak names based on week number okay that means I will provide some number when I say one 

the program should display Sunday when I provide two the program should display Monday so when I 

provide a number between 127 the corresponding week name should be displayed as an output 

okay so how many weeks we have totally seven weeks Sunday to Saturday right so all these seven 

weeks we have to print based upon the week number so what is the input here week number is an input 

okay so we need to check multiple conditions if the week number is one then print Monday if the 

week number is two then print Tuesday if the week number three then print another so like this we 

can write multiple conditions okay so let's try to do this close and new class and print weak 

names take this main method say finish so based upon the weak number I want to just print name 

of the veker so what is an input here what is a variable you have to take a weak numers so I'll 

take integer variable in week number let us start with one first now we need to check this weak 

number we need to compare with uh multiple weak numbers like 1 2 3 4 5 up to seven so what is the 

first one if this weak number equal to one if the weak number equal to one what we should say this 

is Sunday this is Sunday else one more condition else if week number equal to two then we can say 

m day one more condition else if the weak number weak number equal to three then you can say to St 

so multiple conditions you can keep like this if Els if Els it FS like this so all the conditions 

we can verify like this because total seven weeks are there so four five six and finally what seven 

obviously the last one uh is seven so that I can keep LF also lsf week number equal to 7 then I can 

print certainty okay suppose if I provide invalid number other than 1 to 7 so weak number should 

be between 1 to 7 so if the week number is other than 1 to7 then what we should say invalid weak 

number we should display invalid weak number so now what we need to do if we specify more number 

of conditions if none of them are not satisfied finally else Block in this we can say invalid week 

number because this else block will execute after completion of all the conditions if no condition 

is not matching finally else block will execute so this is invalid weak number you can print like 

this so multiple conditions also we can verify by using IF else conditions like this okay now let 

us test this code so initially say week number one so execute say Sunday so that means the 

first condition itself is matched so thus this statement is got executed and as soon as this 

condition is matched so rest of the conditions will not be verified okay now let us make this 

seven sorry here seven week number seven so it will verify all the conditions one by one and 

finally one condition is got matched here week number seven so it is got printed Saturday 

fine now let's say 10 here 10 so when I said 10 no condition is matching so finally else 

block will execute so it will say number is invalid weak number okay so this is how we can 

Implement logic so we can also write multiple conditions multiple if else conditions depends 

upon your requirement you can frame the logic okay so these are all conditional statements we 

have seen if condition if else condition nested if Els condition means if conditions can also 

have another if conditions and finally switch case statement so when we have to use switch case 

this is also comes under conditional statement but we need to understand when we have to use 

switch case statement switch case statement uh we will prefer to use if we have a more number of 

conditions for example let's look at this example here we have almost seven conditions so how many 

lines of code we have written here almost 40 plus almost 40 plus lines of code we have written to 

verify the seven conditions so if you have more conditions like this seven conditions sometimes 

you have a 10 condition 20 conditions if you have a more conditions like this instead of using IF 

Els conditions better to go with a switch case statement why because switch case statement will 

reduce the lot of code in switch case statement it will also perform the same thing the objective 

of same whatever the IFL condition is doing here same thing we can achieve by using switch case 

statement but what is a major advantage of it is it will reduce the code it will reduce the code 

so why we need to red uce the codee because if you are saying a developer is writing very good code 

on what basis you will say if if the developer is more efficient he will write a small thing 

and do bigger task inefficient developers they will write a big code but do the small task so 

unnecessarily we should not increase the size of the code we always try to reduce the code with 

minimal code we should achieve the larger things so then only he's a efficient developer so instead 

of writing more number of f conditions like this more much much code we can try to reduce this code 

by rating the switch case statement right so now let me show you how can we write switch case 

statement and how to use switch case statement same program I will try to write a switch case 

using switch case and then we will see how much of lines it will almost 50% of the code will 

be reduced okay now we'll see the first syntax syntax switch case so the syntax will be like this first we need 

to start with the switch and here we don't specify the condition Cas okay we specify the variable 

okay and inser this switch we will write one thing called case keyword Case Case is a keyword 

here we specify the value of the variable okay so if the variable value is equal to this one let's 

say value one okay and then here we specify the statements okay and suppose if the variable 

value is not equal to this one then another Case Case value equal to two then execute 

another set of statements and suppose even this variable is not equal to this value two 

then another Case Case value three and then execute statements and if the variable value 

is not equal to any of these value not matching with any of these values then finally we specify 

default it is just like a else block so if none of the cases are not matching finally default 

block statements will execute this is a switch case switch and case syntax so now let me show 

you how can we use this uh with this switch case commands so can we write a program without 

declaring the variable using conditional statement yes you can write if you have such 

requirement you can write you can directly put the expression because why variables are needed 

you need to first understand that concept why we need to create a variable what is the importance 

of variable to store the data right to store the data or to maintain the data we have to create 

a variables if you do not have variables what you want to achieve with the program if you do 

not have any variables defined in your program what you want to achieve first of all you need to 

clear about your goal right why you are writing the program what you want to achieve with this 

program if you're clear about the goal objective then you will know exactly variables are required 

or not okay variables are required or not so just think logically guys why variables are required 

to store the data and you have some data now you want to have some comparison then what you 

will do definitely you will go with the control statements and if you don't want to compare 

anything then don't go with the conditional statements if you have such requirement for 

example I just create created two variables I just want to print the values of them do you 

need control statements if else conditions you you need them you don't need just you create a 

variables and you're printing the data just by using print statement you can print values of 

a b c you're not comparing anything so no need any control statements or if El conditions but 

if you want to compare something if you want to do some operation based upon certain conditions 

definitely you have to write a if else conditions you guys understood so variables definitely 

we needed for almost in every program without variable we don't write any program first of all 

it doesn't make any sense without variable you don't want to achieve anything right if you 

want to achieve something we have to write some program some logic some implementation 

should be there if you just want to print a name I don't need any control statement what is 

the main achievement for this right if you just simply write a system pintel and your name 

what is the main objective of the program if your objective is just printing the data you 

don't need conditional statements but if you want to perform certain operations based upon 

certain conditions comparison all these things you need to use conditional statements so you can 

write program you can use conditional statement with variables and without variables also 

but if I don't want to use variables there is no objective of that program see in the 

previous example in multiple statements here uh have you written any variables here 

did I create any variables here in this program have you written any variables no 

right I haven't used any variables still I have used conditional statements why because 

I directly put the values instead of writing variables and storing the data I directly put 

the values here so I have not used variables it depends on your requirement it keeps changing 

okay so the input also we can pass through the console window at the run time so that I will 

show in the coming sessions because we want to know the scanner class so through the scanner 

class we can take this input at the run time so once your program is got executed at the run 

time you can provide the number randomly you can pass input that is possible okay that I will 

cover in the coming sessions for now just follow this approach okay okay so now let us see how 

can we write the same program using switch case statement so if you want to achieve this we have 

written almost 40 lines of gold 40 plus lines of gold when I use if else conditions right but if 

you write the same program by using switch case statement we'll see how many lines of code is 

got reduced fine so let me create another class switch case most important listen carefully 

many times we prefer to use this switch case statement okay just observe so I already told you 

syntax so if the switch variable here we should not specify any condition okay we just specify 

only variables and here we are comparing it so first we'll take a variable ENT weak number 

let's take one now we need to write a switch case statement how to start switch and here 

we specify the variable what is the variable here weak number is a variable and inside this 

switch we have to write a multiple cases so what is the first case here is case if the weak 

number equal to one here value is what one if the weak number is one colon here we have 

to put colon not curly Braes okay colon here you can write whatever the statement you want to 

execute so here I want to execute system. can say something and after completion of the statement 

immediately you have to write one more thing called break break is basically called jumping 

statement but here we have to use break command after executing the statement but I'll tell 

you why we have to use this break statement just hold on so this is one case suppose 

if the weak number is not matching with one then another Case Case two here system 

print say Monday again after the statement break bre okay like this I can write 

multiple conditions I can write multiple conditions okay so you can put this break 

statement in the next line or in the same line no problem because the semicolon is representing 

the end of the line this statement is got ended so after that you can put the statement in the 

next line or you can keep in the same line no problem like this okay now we can put multiple 

conditions case 1 2 3 4 up to seven conditions we have to specify so here we can write seven 

conditions like this and after completion of all seven cases finally if suppose none of the 

cases are not matching then finally default here we have to say system P invalid weak number here 

break is not required here break is not required okay so this is our program see how much size 

it has got reduced almost 50% of the code is got reduced if I use switch Cas statement okay 

now let us first test this code and after that we'll see how it is got executed so week number 

one means what week number one and case one is matching so obviously it will print Sunday okay 

Sunday is going printed now let's make it as a seven so seven me seventh case matched 

and Saturday got printed now let us make [Music] 10 okay so 10 so let's try to 

execute copy Java application now invalid weak so default is got executed if none of 

the cases are not matching finally default is got executed fine it's perfectly working 

fine now let us try to understand how this code is got executing and why we need this 

break additional statement in every case so normally switch case statements uh we work 

based upon the case not condition actually so this week number equal to one then this 

statement will execute if you not specified the break here for example if you're 

not specified the break then what will happen Okay let me remove the break here okay and 

we'll see what will happen let's say make it as a one so first case is matching the statement 

will execute right so when I execute this just observe what happens it is displayed two week 

numbers Sunday is fine because the case one is got matched Sunday is got printed now immediately 

what is printing Monday also it is printing so how it is happened because as soon as the statement 

is got printed immediately it will execute the next statement without checking any case without 

checking this case by default the second statement will be executed okay so if you put this break 

then what will happen after the statement is got executed we no need to execute the rest of the 

statements it doesn't make any sense right at a time only one statement we want to execute so 

when you specify break automatically this Brak command will jump out from this switch command 

this come out from entire block wherever The Brak is got executed that will break the condition 

it will break the block it will come out from this block automatically same thing suppose if 

the case one is not matching then it will go to case two and case two Monday is got displayed 

immediately break it will come out from the Block if I don't specify break here then what 

happens if break is not created here then what happens so it is got printed 

Sunday so let me just put two here then so it has got printed Monday and also it is 

printed Tuesday because we haven't put we haven't put break here so now you understand what is the 

importance of break here if I don't put break command it will automatically execute the next 

command next statement without checking any case so that is the reason as soon as your case is got 

executed immediately we have to use break command this is mandator required for every case but 

why we haven't specified break for default here we just specify only statement there is no break 

can anyone guess why we haven't specify break for default why yes because after execution of 

the statement there is no other statement to be executed right this is the last statement 

obviously it will automatically break after execution of this default statement it will 

Anyway come out from the Block so break is not at all needed here but rest of the cases 

break is mandator required so if I compared this with our previous example same program same 

objective we have achieved in two different ways by writing multiple if conditions by writing a 

switch case statement and if you look at the size of The Code by using ifls conditions almost 

we have written 40 plus lines and by using switch case statement we have just written 20 plus 

lines almost 50% % of the code is got reduced so this is the main advantage of using switch case 

statement so whenever you have a more number of conditions to verify or compare try to use switch 

case statement instead of using IF else condition statement so nothing wrong in this you can use 

if else or switch case both are correct but which one will be used more efficiently when you compare 

switch case statement will be preferred okay this is also another type of switch case statement so 

these are all four different type of conditional statements which we have discussed if if else 

and N if else switch case these are all called conditional statement first category of control 

statements so what is the use of conditional statements if you want to execute set of statement 

or statements based upon certain condition we can go with conditional statements conditional 

statements based upon the condition we can choose the particular block we want to execute 

or not that we can decide based on the condition then we can go with conditional statements and 

we also have something called looping statements looping means the same set of statement we will 

repeat multiple times reputation will be there and we have a while loop for Loop do while loop 

multiple type of looping statements are there so in the next session we will discuss about looping 

statements and then jumping statements okay and I got one question here is there a shortcut on the 

keyboard what is the shortcut or copy duplicate uh shortcut to copy or duplicate the code like you do 

you can just copy paste control C and control V so actually some of the statements I already written 

somewhere so I'm just copying from there okay so you have to write it or you can just copy paste 

control C and call control V that's it nothing new okay so based on these topics I 

will give you some assignments today so you guys can try those assignments it 

based on total today's topic conditional statements assignments okay so the first assignment so we 

have seen how to find largest of three numbers right so by using multiple if else conditions 

so you guys can try largest of two numbers very small the largest of three numbers we WR multiple 

condition but here the conditions will be reduced largest of two numbers and you can achieve this by 

using two different ways using IF else conditions you can try also by using tary operator this is 

very famous inter question tary operator do you guys remember what is tary operator yesterday 

we have discussed it tary operator so by using tary operator also we can achieve this solution 

simple one single line we can achieve this you can also write if else conditions but instead 

of writing if else conditions you can simply write one single statement ternary operator 

to find largest of two numbers try this okay yesterday we have already discussed about tary 

operator now the second example we have seen how to find largest of three numbers so you guys 

can try smallest of three numbers okay smallest of three numbers again you have to use if Els 

conditions multiple condition ifs okay now the third example you have to print weak name name 

okay so you have to print weak number based on weak name exactly opposite we have here we have 

seen we have taken weak number as an input and printed weak names right now what you have to 

do you have to take weak name as an input and then print a weak name number here exactly you 

have to do reverse okay so you have to take a weak name here as an input and here you have to 

specify the weak number so how to replace this so simple just I'm giving some clue you guys can 

try so here we say in week number equal to 2 3 4 5 right but here you have to take a string 

variable string week name equal to you can specify Sunday Monday or whatever you want this 

is the input string we have to take input but how can we compare this in the switch case and if 

you want to write a switch case you can write like this switch and here we name whatever the 

variable we Define and how to write a cases here case 1 2 3 4 no because this 1 2 3 4 is a values 

actually weak numbers so here the weak names you have to write okay case because it is a string 

we have to keep inside the double putting case Sunday like this okay case a Sunday then system. 

and one and then break like this you have to write you understood now so completely opposite so 

here we Tak the week name and comparing with the week name in every case we have to write 

a week name Sunday Monday Tuesday thday like this seven week names and you have to print week 

number exactly opposite of this right so just you try this because we are keeping the strings right 

because if it is a string we have to keep inside the double quotations if it is a number we can 

directly put the number try this so this is one assignment it is okay so so print week number based on 

week name so here use switch case okay so you guys can try these 

three examples today and tomorrow once you discussed looping statements then I 

will give you some more assignments right so we stop here for today's session 

and tomorrow we'll continue it

Need a transcript for another video?

Get free YouTube transcripts with timestamps, translation, and download options.

Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact

Session 4- Working with Java Conditional Statements | Jav...