Java: Java Programming For Beginners a simple Start To Java Programming (Written By a software Engineer)


Download 0.82 Mb.
Pdf ko'rish
bet31/95
Sana09.01.2022
Hajmi0.82 Mb.
#261112
1   ...   27   28   29   30   31   32   33   34   ...   95
Bog'liq
Java Programming For Beginners - A Simple Start to Java Programming ( PDFDrive )

The if…else Statement
An if proclamation can be trailed by a non-compulsory else explanation, which executes
when the Boolean outflow is false. The syntax for this construct is as follows:
if(){


//Executes if condition is true
}
else{
//Executes if condition is false
}
Sample Implementation:
public class myTest {
public static void main(string args[]){
int i = 0;
if( i > 1 ){
System.out.print(“The if construct is executing!”);
}
else{
System.out.print(“The else construct is executing!”);
}
}
This would create the accompanying result:
The else construct is executing!
The if…else if Statement
An if proclamation can be trailed by a non-compulsory else if…else explanation, which is
exceptionally helpful to test different conditions utilizing single if…else if articulation.
At  the  point  when  utilizing  if  ,  else  if  ,  else  proclamations  there  are  few  focuses  to
remember.
An if can have zero or one else’s and it must come after any else if’s.
An if can have zero to numerous else if’s and they must precede the else.
If one of the if conditions yield a true, the other else ifs and else are ignored.
The syntax for using this decision making construct Is as follows:


if(condition_1){
//Execute if condition_1 is true
}
else if(condition_2){
//Execute if condition_2 is true
}
else if(condition_3){
//Execute if condition_3 is true
}
else
{
//Execute if all conditions are false
}
Sample Implementation:
public class myTest {
public static void main(string args[]){
int i = 0;
if( i > 1 ){
System.out.print(“The first if construct is executing!”);
}
else if(i == 0){
System.out.print(“The second if construct is executing!”);
}
else{
System.out.print(“The else construct is executing!”);
}
}


This would create the accompanying result:
The second if construct is executing!

Download 0.82 Mb.

Do'stlaringiz bilan baham:
1   ...   27   28   29   30   31   32   33   34   ...   95




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling