if1. İ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 10ve daha az (veya eşit) ise 20, o zaman bir süveter giyin
  • Sıcaklık dereceden büyükse 0ve altında (veya eşit) ise 10, 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-elseifadeler iç içe olabilir . Bu, bir programda oldukça karmaşık bir mantığın uygulanmasını mümkün kılar.

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");
}

Verilen iki örnek eşdeğerdir, ancak ikincisini anlamak daha kolaydır.


else2. Bloğun nüansları

Önemli bir nokta:

Bir yapıda kaşlı ayraçlar kullanmıyorsanız if-else, o zaman , elseen yakın önceki anlamına gelir if.

Örnek:

Kodumuz Nasıl çalışacak
int age = 65;

if (age < 60)
   if (age > 20)
      System.out.println("You must work");
else
   System.out.println("You don't have to work");
int age = 65;

if (age < 60) {
   if (age > 20)
     System.out.println("You must work");
   else
     System.out.println("You don't have to work");
}

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, elseblok ve "Çalışmak zorunda değilsiniz" ifadesi, ikinci (daha yakın) ifadeyle ilişkilidir if.

Sağdaki kodda, ilişkili ifve elsekı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ışmanız gerekmeyen dize, agedeğerinden büyük olduğunda hiçbir zaman görüntülenmez 60.


4
Görev
Java Syntax,  seviyeders
Kilitli
This age doesn't work for me…
Sometimes we all want to change our age. First, they don't want to sell you cigarettes or beer. Then your hair starts thinning and your back aches! A programmer may not have control over time, but he or she has total control over data in programs. In this task, we correct an error so that a Person object's age variable receives a different value.

if-else3. Bir ifade kullanma örneği

İfadeyi bu kadar iyi incelediğimize göre if-elsebir ö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
   }
}
Minimum iki sayı gösteriliyor