1. Kolejność if
wypowiedzi
Czasami program musi wykonać wiele różnych akcji w zależności od wartości zmiennej lub wartości wyrażenia.
Powiedzmy, że nasze zadanie wygląda mniej więcej tak:
- Jeśli temperatura jest wyższa niż
20
stopnie, załóż koszulę - Jeśli temperatura jest wyższa niż
10
stopnie i niższa (lub równa)20
, załóż sweter - Jeśli temperatura jest wyższa niż
0
stopnie i niższa (lub równa)10
, załóż płaszcz przeciwdeszczowy - Jeśli temperatura jest niższa niż
0
stopnie, załóż płaszcz.
Oto jak można to przedstawić w kodzie:
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
instrukcje mogą być zagnieżdżane w sobie. Umożliwia to zaimplementowanie w programie dość złożonej logiki.
Ale powyższy przykład jest również interesujący, ponieważ możemy nieco uprościć kod, pomijając nawiasy klamrowe:
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");
Jednak programiści zwykle piszą tę konstrukcję nieco inaczej:
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");
Wszystkie trzy z tych przykładów są równoważne.
2. Niuanse else
bloku
Jeśli nie używasz nawiasów klamrowych w if-else
konstrukcji, to else
odnosi się do najbliższego poprzedniego if
.
Przykład:
Nasz kod | Jak to będzie działać |
---|---|
|
|
Jeśli spojrzysz na kod po lewej stronie, wydaje się, że na ekranie pojawi się komunikat „Nie musisz pracować”. Ale tak nie jest. W rzeczywistości else
blokada i stwierdzenie „Nie musisz pracować” wiąże się z drugim (bliższym) if
stwierdzeniem.
W kodzie po prawej stronie powiązane if
i else
są podświetlone na czerwono. Dodatkowo nawiasy klamrowe są umieszczone w sposób jednoznaczny, jasno pokazując jakie czynności zostaną wykonane. Czy ciąg Nie musisz pracować nigdy nie jest wyświetlany, gdy age
jest większy niż 60
?
3. Przykład użycia if-else
stwierdzenia
Ponieważ tak dobrze zbadaliśmy to if-else
stwierdzenie, podajmy przykład:
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
}
}