1. Kolejność ifwypowiedzi
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ż
20stopnie, załóż koszulę - Jeśli temperatura jest wyższa niż
10stopnie i niższa (lub równa)20, załóż sweter - Jeśli temperatura jest wyższa niż
0stopnie i niższa (lub równa)10, załóż płaszcz przeciwdeszczowy - Jeśli temperatura jest niższa niż
0stopnie, 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-elseinstrukcje 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 elsebloku
Jeśli nie używasz nawiasów klamrowych w if-elsekonstrukcji, to elseodnosi 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 elseblokada i stwierdzenie „Nie musisz pracować” wiąże się z drugim (bliższym) ifstwierdzeniem.
W kodzie po prawej stronie powiązane ifi elsesą 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 agejest większy niż 60?
3. Przykład użycia if-elsestwierdzenia
Ponieważ tak dobrze zbadaliśmy to if-elsestwierdzenie, 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
}
}
GO TO FULL VERSION