1. Urutane ifpratelan

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 20derajat, banjur nganggo klambi
  • Yen suhu luwih saka 10derajat lan kurang saka (utawa padha karo) 20, banjur sijine sweter
  • Yen suhu luwih gedhe tinimbang 0derajat lan kurang saka (utawa padha karo) 10, banjur nganggo jas hujan
  • Yen suhu kurang saka 0derajat, 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-elsestatement bisa nested ing siji liyane. Iki ndadekake iku bisa kanggo ngleksanakake logika rada Komplek ing program.

Nanging conto ing ndhuwur uga menarik amarga kita bisa nggawe kode luwih gampang kanthi ngilangi kurung kriting:

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

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

Telu conto iki padha karo.


2. Nuansa elseblok

Titik penting:

Yen ora nggunakake kurung kriting ing if-elsekonstruksi, banjur elsenuduhake sing paling cedhak sadurunge if.

Tuladha:

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, elsepamblokiran lan pernyataan "Sampeyan ora kudu kerja" digandhengake karo ifpernyataan kapindho (sing luwih cedhak).

Ing kode ing sisih tengen, sing digandhengake iflan elsedisorot abang. Kajaba iku, kurung kriting diselehake kanthi jelas, kanthi jelas nuduhake tumindak sing bakal ditindakake. Apa senar Sampeyan ora kudu kerja ora bakal ditampilake nalika ageluwih gedhe tinimbang 60?



3. Tuladha ngginakaken if-elsepratelan

Awit kita nliti if-elsepernyataan 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