OOP IN JAVA -( CONDITIONS - if | else | else if)
java in mainly contain three types of Conditions
- if Condition
- else Condition
- else if Condition
If Condition
The condition after evaluation of if-statement will be either true or false. And also the if statement in Java accepts Boolean values and if the value is true then it will execute the block of statements under it.
important Note ;
If we do not provide the curly braces ‘{‘ and ‘}’ after if( condition ) then by default if statement will consider the immediate one statement to be inside its block. Ex :-
public Class IFstatment{
public static void main (string [] args) {
int s = 16;
if ( s<16) {
system .out.println (" hi");
}
}
}
else Condition
Use the
else
statement to specify a block of code to be executed if the condition is false. Ex - public Class IFstatment{
public static void main (string [] args) {
int s = 35 ;
if ( s<16) {
system .out.println (" hi");
} else {
system .out.println (" Bye");
}
}
else if Condition
when you Use the
else if
statement to specify a new condition if the first condition is false. Ex :- public Class IFstatment{
public static void main (string [] args) {
int s = 35 ;
if ( s<16) {
system .out.println ("hi");
} else if ( s< 22) {
system .out.println ("come");
}else {
system .out.println (" Bye");
}
}
}
THANK YOU!
Comments
Post a Comment