Java Essentials - If else statement in java
1K views
Nov 18, 2024
Java Essentials - If else statement in java Watch more Videos at https://www.tutorialspoint.com/videotutorials/index.htm Lecture By: Mr. Arnab Chakraborty, Tutorials Point India Private Limited
View Video Transcript
0:00
If else if else statement in Java so how this if else if else statement will get
0:07
executed in Java so let us go for some detailed discussion if else statements are
0:14
used to check some conditions whether the condition is true or false and it
0:18
only executes during the runtime so now you see this is the condition this
0:24
condition will be executed during the runtime that means the condition will be
0:28
checked during the runtime if it is found true then this if code will get executed if code means it might be a
0:35
might be collection of multiple statements or a single statement might be there
0:39
and if the condition is false then this else code will get executed else code can
0:44
contain a single statement or multiple statements and then the control will get
0:50
combined here and then after if statements will get executed and the program
0:55
will get completed ultimately so now this is our else if ladder statement now what is the else if ladder statement so sometimes
1:05
there are multiple conditions are present to check and when the prior condition is
1:09
false it goes to the next condition for the checking so just consider this one
1:15
so here the control is coming to this condition number one when the condition number is true then the statement one will get executed then it is going for the next statement execution but if the condition one is false then condition two will be checked
1:29
and when this true then the statement two will get executed and contour will go to the next statement
1:36
but when this condition two is false then condition in this way you can have the multiple such
1:41
conditions so then we're having this condition n will be getting checked and then if it is true
1:48
then statement n will be executed and next statement will get the control afterwards otherwise the
1:54
statements other statements will get executed and the next statement will get the control accordingly
1:59
so now let us go for one demonstration to illustrate this concept for your better understanding
2:05
so here is the demonstration for you in this demonstration we are discussing if else if else statement
2:12
so at first we are defining a scanner class object and mentioning that will be taken
2:18
inputs from the keyboard that is our system's default input device and we're asking
2:23
the user to enter a number and then we are taking the number from the keyboard as
2:27
input it will be treated as a as an integer and that will be assigned to the
2:31
I and T number variable then we're having the if so in this if statement after
2:37
if within the first bracket we're writing the condition if the condition is true
2:41
then all the statements which will be written within this calibres within this block they will get executed so here here the condition is number less than zero that means if the number is less than zero we are executing this statement to print one string that the number is
2:57
negative else if so otherwise we're going for check another condition that is
3:02
number is equal to is equal to zero and when this particular condition will be true
3:07
then the statements which will be written within this callibrasis within this block
3:10
will get executed and here we're having a single statement to print that number is equal to zero so the number is zero next one is
3:19
the else one so in the else that means when this condition is false and when also this condition
3:25
is false then we are going for the else part and in the else part we're writing that system dot
3:30
out print ellen the number concatenation number is positive so you can find that depending upon
3:36
the value of the number which will be given as input from the keyboard by the user either the
3:41
message the number is negative or the message the number is zero or the message the number
3:46
is positive is going to take place and then we're closing the scanner object so let us go
3:52
for the execution to show you that which statement is getting executed and depending
3:57
upon the input number so I'm giving the number as 100 you can easily find the
4:02
number 100 is positive that means number less than zero that is 100 less than
4:07
zero condition is false so this line is not getting executed number is equal is equal to zero so hundred is equal to easily zero the condition is false so this line is not getting executed number is equal to is equal to zero so hundred is equal to zero the condition is false so this
4:17
line is not getting the control otherwise else this line is going to get the
4:22
control so accordingly we are getting the output like this one so we are putting
4:26
here 100 the number is positive is coming if I execute the program once again
4:31
and then I'm giving the numbers say minus 100 so what will happen number less than
4:37
0 that is minus 100 less than 0 the condition will be true so as a result of that the output will be the number minus 100 is negative so this statement is getting executed because the condition is getting true so the control is coming to this block let us go for the another one where we will be providing the number as 0 so when we are providing the number as 0 then we are getting this one that number is equal to is equal to 0 the condition is becoming true so the number is
5:07
this statement is getting executed so from this demonstration I think you were
5:12
getting this idea that how to use if else if else block in our Java code if is
5:19
mandatory else if and else they are optional a single with single if we can
5:24
have only a single else but we can have multiple else if as required in our
5:31
coding in our logic development thanks for watching this video
#Computer Education
#Java (Programming Language)