If Else if Demo in Java, some if elseif statements and their examples
If else if condition check
-
In the following send
arguments Rama and execute the program
-
send a argument Krishna and
examine
-
send a different name and
examine the code.
In this program I am sending Rama
let's see the out put of the following class.
public
class
IfElseIf {
public
static
void
main( String [] args ){
// get argument
//
print partner name
//
use if else if to achieve this
String partnerName= args[0];
if(
partnerName.equalsIgnoreCase("Rama")
){
System.out.println(
"Partners are Sita
and " + partnerName);
}
else
if(
partnerName.equalsIgnoreCase("Krishna")){
System.out.println(
"Partners are Bhama
and " + partnerName);
}
else
if(
partnerName.equalsIgnoreCase("Vishnu")){
System.out.println(
"Partners are Laxmi
and " + partnerName);
}
else
if(
partnerName.equalsIgnoreCase("Shiva")){
System.out.println(
"Partners are Parvati
and " + partnerName);
}
else
if(
partnerName.equalsIgnoreCase("Brahma")){
System.out.println(
"Partners are
Saraswati and " + partnerName);
}
else{
System.out.println(
"No partners ");
}
}
}
Out put of the program when argument
is Rama:
Partners are Sita and Rama
Case 2) Same program and confirm how
many times if else if condition checks for same
conditons in else if
Observe the code change differeces
in above program and below.
public
class
IfElseIf {
public
static
void
main( String [] args ){
// get argument
// print partner name
// use if else if to
achieve this
String partnerName= args[0];
if(
partnerName.equalsIgnoreCase("Rama")
){
System. out.println(
"FIRST TIME IN
BLOCK " + partnerName);
}
else
if(
partnerName.equalsIgnoreCase("Rama")
){
System. out.println(
"SECOND TIME
IN BLOCK " + partnerName);
}
else{
System. out.println(
"No partners ");
}
}
}
out put of the program when Rama is
argument:
FIRST TIME IN BLOCK Rama
Please send comments to vgdarur.javafive@blogger.com
|