public static void main(String[] args) {
int xaridorningYoshi=17;
if(age < 18){
Sytem.out.print("Tamaki mahsuloti sotilmasin");
}
}
}
Ekranda:
Tamaki mahsulot sotilmasin
int xar idorningYoshi=17; -> kiruvchi qiymat
if(age < 18 ) -> tekshiruv jarayoni
Java IF-else operatori ta’rifi
Shart tekshirish jarayonida shart true bo’lsa, if blokdagi amal bajariladi, aks holda else block dagi amal bajariladi
-
if(condition){
-
//code if condition is true
-
}else{
-
//code if condition is false
-
}
Misol: Berilgan sonni toq yoki toq emasligini bilish dasturi
-
publicclass IfElseExample {
-
publicstatic void main(String[] args) {
-
int number=13;
-
if(number%2==0){
-
out.println("juft son");
-
}else{
-
out.println("toq son");
-
}
-
}
-
}
Ekranda:
Toq son
Java IF-else-if tanlash operatori
O’zbekcha qilib aytganda unisi bo’lmasa bunisi, bunisi bo’lmasa keyingisi degan ma’noda. if-else-if ketma ketligi shartlar ko’p bo’lgan hollarda ishlatiladi.
-
if(condition1){
-
//code to be executed if condition1 is true
-
} else if(condition2){
-
//code to be executed if condition2 is true
-
} else if(condition3){
-
//code to be executed if condition3 is true
-
}
-
...
-
else {
-
//code to be executed if all the conditions are false
-
}
-
publicclass IfElseIfExample {
-
-
publicstatic void main(String[] args) {
-
Do'stlaringiz bilan baham: |