Difference between revisions of "JAVA: Pernyataan if-else dengan Contoh"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Decision Making in Java helps to write decision-driven statements and execute a particular set of code based on certain conditions. The if statement alone tells us that if a...")
 
Line 1: Line 1:
Decision Making in Java helps to write decision-driven statements and execute a particular set of code based on certain conditions.
+
Pengambilan Keputusan di Java membantu menulis pernyataan berbasis keputusan dan mengeksekusi serangkaian code tertentu berdasarkan kondisi tertentu.
  
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with the if statement to execute a block of code when the condition is false.
+
Pernyataan if memberi tahu kita bahwa jika suatu kondisi benar, itu akan mengeksekusi blok pernyataan dan jika kondisinya salah, blok pernyataan tersebut tidak akan dilakukan. Tetapi bagaimana jika kita ingin melakukan sesuatu yang lain jika kondisinya salah. Di sinilah pernyataan lain. Kita dapat menggunakan pernyataan else dengan pernyataan if untuk mengeksekusi blok code ketika kondisinya salah.
  
if-else in Java
+
[[File:If-else-statement-GeeksforGeeks1.jpg|center|300px|thumb]]
  
 
Syntax:  
 
Syntax:  
Line 17: Line 17:
 
     // condition is false
 
     // condition is false
 
  }
 
  }
Working of if-else statements 
 
  
Control falls into the if block.
+
==Cara kerja pernyataan if-else==
The flow jumps to Condition.
+
 
Condition is tested.  
+
# Control falls into the if block.
If Condition yields true, go to Step 4.
+
# The flow jumps to Condition.
If Condition yields false, go to Step 5.
+
# Condition is tested.  
The if-block or the body inside the if is executed.
+
** If Condition yields true, go to Step 4.
The else block or the body inside the else is executed.
+
** If Condition yields false, go to Step 5.
Flow exits the if-else block.
+
# The if-block or the body inside the if is executed.
 +
# The else block or the body inside the else is executed.
 +
# Flow exits the if-else block.
 +
 
 
Flowchart if-else:
 
Flowchart if-else:
 
   
 
   

Revision as of 07:52, 4 May 2022

Pengambilan Keputusan di Java membantu menulis pernyataan berbasis keputusan dan mengeksekusi serangkaian code tertentu berdasarkan kondisi tertentu.

Pernyataan if memberi tahu kita bahwa jika suatu kondisi benar, itu akan mengeksekusi blok pernyataan dan jika kondisinya salah, blok pernyataan tersebut tidak akan dilakukan. Tetapi bagaimana jika kita ingin melakukan sesuatu yang lain jika kondisinya salah. Di sinilah pernyataan lain. Kita dapat menggunakan pernyataan else dengan pernyataan if untuk mengeksekusi blok code ketika kondisinya salah.

If-else-statement-GeeksforGeeks1.jpg

Syntax:

if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}

Cara kerja pernyataan if-else

  1. Control falls into the if block.
  2. The flow jumps to Condition.
  3. Condition is tested.
    • If Condition yields true, go to Step 4.
    • If Condition yields false, go to Step 5.
  1. The if-block or the body inside the if is executed.
  2. The else block or the body inside the else is executed.
  3. Flow exits the if-else block.

Flowchart if-else:


Flowchart of if-else in Java

Contoh 1:

// Java program to illustrate if-else statement
 
class IfElseDemo {
    public static void main(String args[])
    {
        int i = 20;
 
        if (i < 15)
            System.out.println("i is smaller than 15");
        else
            System.out.println("i is greater than 15");
 
        System.out.println("Outside if-else block");
    }
}

Output

i is greater than 15
Outside if-else block


Dry-Running Example 1:

1. Program starts. 2. i is initialized to 20. 3. if-condition is checked. 20<15, yields false. 4. flow enters the else block.

 4.a) "i is greater than 15" is printed

5. "Outside if-else block" is printed.

Contoh 2:

// Java program to illustrate if-else statement

class IfElseDemo {
    public static void main(String args[])
    {
        String str = "geeksforgeeks";
 
        if (str == "geeks")
            System.out.println("Hello geek");
        else
            System.out.println("Welcome to GeeksforGeeks");
    }
}

Output

Welcome to GeeksforGeeks


Referensi