## 1. Urutane `if`pratelan

Kadhangkala program kudu nindakake macem-macem tumindak gumantung saka nilai variabel utawa nilai ekspresi.

Ayo kita ngomong tugas kita kaya iki:

• Yen suhu luwih saka `20`derajat, banjur nganggo klambi
• Yen suhu luwih gedhe tinimbang `10`derajat lan kurang saka (utawa padha karo) `20`, banjur nganggo sweter
• Yen suhu luwih gedhe tinimbang `0`derajat lan kurang saka (utawa padha karo) `10`, banjur nganggo jas hujan
• Yen suhu kurang saka `0`derajat, banjur nganggo jas.

Mangkene carane iki bisa diwakili ing kode:

``````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`statement bisa nested ing siji liyane . Iki ndadekake iku bisa kanggo ngleksanakake logika rada Komplek ing program.

Nanging, programer biasane nulis konstruksi iki rada beda:

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

Conto loro sing diwenehake padha karo, nanging sing nomer loro luwih gampang dingerteni.

## 2. Nuansa `else`blok

Titik penting:

Yen ora nggunakake kurung kriting ing `if-else`konstruksi, banjur `else`nuduhake sing paling cedhak sadurunge `if`.

Kode kita Carane bakal bisa
``````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");
}``````

Yen katon ing kode ing sisih kiwa, misale jek sing output layar bakal "Sampeyan ora kudu bisa". Nanging ora ngono. Ing kasunyatan, `else`pamblokiran lan pernyataan "Sampeyan ora kudu kerja" digandhengake karo `if`pernyataan kapindho (sing luwih cedhak).

Ing kode ing sisih tengen, sing digandhengake `if`lan `else`disorot abang. Kajaba iku, kurung kriting diselehake kanthi jelas, kanthi jelas nuduhake tumindak sing bakal ditindakake. String Sampeyan ora kudu kerja ora bakal ditampilake nalika `age`luwih gedhe tinimbang `60`.

## 3. Tuladha ngginakaken `if-else`pratelan

Awit kita nliti `if-else`pernyataan kasebut kanthi becik, ayo menehi conto:

``````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
}
}``````
Nampilake minimal rong nomer