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 gedhe tinimbang 10derajat lan kurang saka (utawa padha karo) 20, banjur nganggo 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, 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 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. String Sampeyan ora kudu kerja ora bakal ditampilake nalika ageluwih gedhe tinimbang 60.


4
tugas
Java Syntax,  tingkatwulangan
Dikunci
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.

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