if
1. İfadelerin sırası
Bazen bir programın, bir değişkenin değerine veya bir ifadenin değerine bağlı olarak birçok farklı eylemi gerçekleştirmesi gerekir.
Diyelim ki görevimiz şöyle bir şey:
- Sıcaklık dereceden yüksekse
20
, bir gömlek giyin - Sıcaklık dereceden büyükse
10
ve daha az (veya eşit) ise20
, o zaman bir süveter giyin - Sıcaklık dereceden büyükse
0
ve altında (veya eşit) ise10
, o zaman bir yağmurluk giyin - Sıcaklık dereceden düşükse
0
, bir kat giyin.
Bunun kodda nasıl temsil edilebileceği aşağıda açıklanmıştır:
int temperature = 9;
if (temperature > 20)
System.out.println("put on a shirt");
else // Here the temperature is less than (or equal to) 20
{
if (temperature > 10)
System.out.println("put on a sweater");
else // Here the temperature is less than (or equal to) 10
{
if (temperature > 0)
System.out.println("put on a raincoat");
else // Here the temperature is less than 0
System.out.println("put on a coat");
}
}
If-else
ifadeler iç içe olabilir. Bu, bir programda oldukça karmaşık bir mantığın uygulanmasını mümkün kılar.
Ancak yukarıdaki örnek, kaşlı ayraçları atlayarak kodu biraz daha basit hale getirebileceğimiz için de ilginçtir:
int temperature = 9;
if (temperature > 20)
System.out.println("put on a shirt");
else // Here the temperature is less than (or equal to) 20
if (temperature > 10)
System.out.println("put on a sweater");
else // Here the temperature is less than (or equal to) 10
if (temperature > 0)
System.out.println("put on a raincoat");
else // Here the temperature is less than 0
System.out.println("put on a coat");
Ancak, programcılar genellikle bu yapıyı biraz farklı yazarlar:
int temperature = 9;
if (temperature > 20)
System.out.println("put on a shirt");
else if (temperature > 10) // Here the temperature is less than (or equal to) 20
System.out.println("put on a sweater");
else if (temperature > 0) // Here the temperature is less than (or equal to) 10
System.out.println("put on a raincoat");
else // Here the temperature is less than 0
System.out.println("put on a coat");
Bu örneklerin üçü de eşdeğerdir.
else
2. Bloğun nüansları
Bir yapıda kaşlı ayraçlar kullanmıyorsanız if-else
, o zaman , else
en yakın önceki anlamına gelir if
.
Örnek:
Kodumuz | Nasıl çalışacak |
---|---|
|
|
Soldaki koda bakarsanız, ekran çıktısının "Çalışmanıza gerek yok" olacağı görülüyor. Ama durum böyle değil. Gerçekte, else
blok ve "Çalışmak zorunda değilsiniz" ifadesi, ikinci (daha yakın) ifadeyle ilişkilidir if
.
Sağdaki kodda, ilişkili if
ve else
kırmızıyla vurgulanmıştır. Ek olarak, kaşlı ayraçlar, hangi eylemlerin gerçekleştirileceğini açıkça gösterecek şekilde net bir şekilde yerleştirilmiştir. Çalışmak zorunda değilsiniz dizesi, is age
less than than 'dan büyük olduğunda asla görüntülenmiyor mu 60
?
if-else
3. Bir ifade kullanma örneği
İfadeyi bu kadar iyi incelediğimize göre if-else
bir örnek verelim:
import java.util.Scanner;
public class Solution {
public static void main(String[] args)
{
Scanner console = new Scanner(System.in); // Create a Scanner object
int a = console.nextInt(); // Read the first number from the keyboard
int b = console.nextInt(); // Read the second number from the keyboard
if (a < b) // If a is less than b
System.out.println(a); // we display a
else // otherwise
System.out.println(b); // we display b
}
}
GO TO FULL VERSION