JAVA: Operator Ternary dengan Contoh: Difference between revisions

From OnnoCenterWiki
Jump to navigationJump to search
Created page with "Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various..."
 
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types:  
Operator merupakan blok bangunan dasar untuk bahasa pemrograman apa pun. Java juga menyediakan banyak jenis operator yang dapat digunakan sesuai dengan kebutuhan untuk melakukan berbagai perhitungan dan fungsi, baik itu logika, aritmatika, relasional, dll. Diklasifikasikan berdasarkan fungsionalitas yang disediakan. Berikut beberapa jenisnya:


Arithmetic Operators
* Operator Arithmetic
Unary Operators
* Operator Unary
Assignment Operator
* Operator Assignment
Relational Operators
* Operator Relational
Logical Operators
* Operator Logical
Ternary Operator
* Operator Ternary
Bitwise Operators
* Operator Bitwise
Shift Operators
* Operator Shift
This article explains all that one needs to know regarding Arithmetic Operators.


Ternary Operator
Artikel ini menjelaskan semua yang perlu diketahui tentang Operator Aritmatika.
Java ternary operator is the only conditional operator that takes three operands. It’s a one-liner replacement for the if-then-else statement and is used a lot in Java programming. We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators. Although it follows the same algorithm as of if-else statement, the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.


Ternary Operator in Java


Syntax:
==Ternary Operator==


variable = Expression1 ? Expression2: Expression3
Operator ternary di Java adalah satu-satunya operator kondisional yang membutuhkan tiga operan. Ini adalah pengganti satu baris untuk pernyataan if-then-else dan banyak digunakan dalam pemrograman Java. Kita dapat menggunakan operator ternary menggantikan kondisi if-else atau bahkan mengganti kondisi menggunakan operator ternary bersarang. Meskipun mengikuti algoritma yang sama dengan pernyataan if-else, operator kondisional membutuhkan lebih sedikit ruang dan membantu menulis pernyataan if-else dengan cara sesingkat mungkin.
If operates similarly to that of the if-else statement as in Exression2 is executed if Expression1 is true else Expression3 is executed.


if(Expression1)
[[File:Conditional-or-Ternary-Operator- -in-Java.jpg|center|300px|thumb]]
{
    variable = Expression2;
}
else
{
    variable = Expression3;
}
Example:


num1 = 10;
Sintaks:
num2 = 20;


res=(num1>num2) ? (num1+num2):(num1-num2)
variable = Expression1 ? Expression2: Expression3


Since num1<num2,  
Jika beroperasi mirip dengan pernyataan if-else seperti pada Exression2 dijalankan jika Expression1 true, selain itu Expression3 di jalankan.
the second operation is performed
res = num1-num2 = -10
Flowchart of Ternary Operation 
Flowchart for Ternary Operator


Example 1:
if(Expression1)
{
    variable = Expression2;
}
else
{
    variable = Expression3;
}


Contoh: 


// Java program to find largest among two
num1 = 10;
// numbers using ternary operator
num2 = 20;
   
   
import java.io.*;
  res=(num1>num2) ? (num1+num2):(num1-num2)
   
 
class Ternary {
  Since num1<num2,
    public static void main(String[] args)
  the second operation is performed
    {
res = num1-num2 = -10
 
        // variable declaration
[[File:Java-Ternary-Operator.jpg|center|300px|thumb|Flowchart of Ternary Operation]]
        int n1 = 5, n2 = 10, max;
 
==Example 1:==
        System.out.println("First num: " + n1);
        System.out.println("Second num: " + n2);
   
        // Largest among n1 and n2
        max = (n1 > n2) ? n1 : n2;
   
        // Print the largest number
        System.out.println("Maximum is = " + max);
    }
}
Output
First num: 5
Second num: 10
Maximum is = 10
Example 2:  


// Java program to find largest among two
// numbers using ternary operator
 
import java.io.*;
 
class Ternary {
    public static void main(String[] args)
    {
 
        // variable declaration
        int n1 = 5, n2 = 10, max;
 
        System.out.println("First num: " + n1);
        System.out.println("Second num: " + n2);
 
        // Largest among n1 and n2
        max = (n1 > n2) ? n1 : n2;
 
        // Print the largest number
        System.out.println("Maximum is = " + max);
    }
}


// Java code to illustrate ternary operator
import java.io.*;
class Ternary {
    public static void main(String[] args)
    {
        // variable declaration
        int n1 = 5, n2 = 10, res;
        System.out.println("First num: " + n1);
        System.out.println("Second num: " + n2);
        // Performing ternary operation
        res = (n1 > n2) ? (n1 + n2) : (n1 - n2);
        // Print the largest number
        System.out.println("Result = " + res);
    }
}
Output
Output
First num: 5
First num: 5
Second num: 10
Second num: 10
Result = -5
Maximum is = 10
 
==Example 2:==


// Java code to illustrate ternary operator
 
import java.io.*;
 
class Ternary {
    public static void main(String[] args)
    {
 
        // variable declaration
        int n1 = 5, n2 = 10, res;
 
        System.out.println("First num: " + n1);
        System.out.println("Second num: " + n2);
 
        // Performing ternary operation
        res = (n1 > n2) ? (n1 + n2) : (n1 - n2);
 
        // Print the largest number
        System.out.println("Result = " + res);
    }
}


Output
First num: 5
Second num: 10
Result = -5


==Referensi==
==Referensi==


* https://www.geeksforgeeks.org/java-ternary-operator-with-examples/
* https://www.geeksforgeeks.org/java-ternary-operator-with-examples/

Latest revision as of 23:26, 6 May 2022

Operator merupakan blok bangunan dasar untuk bahasa pemrograman apa pun. Java juga menyediakan banyak jenis operator yang dapat digunakan sesuai dengan kebutuhan untuk melakukan berbagai perhitungan dan fungsi, baik itu logika, aritmatika, relasional, dll. Diklasifikasikan berdasarkan fungsionalitas yang disediakan. Berikut beberapa jenisnya:

  • Operator Arithmetic
  • Operator Unary
  • Operator Assignment
  • Operator Relational
  • Operator Logical
  • Operator Ternary
  • Operator Bitwise
  • Operator Shift

Artikel ini menjelaskan semua yang perlu diketahui tentang Operator Aritmatika.


Ternary Operator

Operator ternary di Java adalah satu-satunya operator kondisional yang membutuhkan tiga operan. Ini adalah pengganti satu baris untuk pernyataan if-then-else dan banyak digunakan dalam pemrograman Java. Kita dapat menggunakan operator ternary menggantikan kondisi if-else atau bahkan mengganti kondisi menggunakan operator ternary bersarang. Meskipun mengikuti algoritma yang sama dengan pernyataan if-else, operator kondisional membutuhkan lebih sedikit ruang dan membantu menulis pernyataan if-else dengan cara sesingkat mungkin.

Sintaks:

variable = Expression1 ? Expression2: Expression3

Jika beroperasi mirip dengan pernyataan if-else seperti pada Exression2 dijalankan jika Expression1 true, selain itu Expression3 di jalankan.

if(Expression1)
{
    variable = Expression2;
}
else
{
    variable = Expression3;
}

Contoh:

num1 = 10;
num2 = 20;

res=(num1>num2) ? (num1+num2):(num1-num2)
Since num1<num2, 
the second operation is performed
res = num1-num2 = -10
Flowchart of Ternary Operation

Example 1:

// Java program to find largest among two
// numbers using ternary operator
 
import java.io.*;
 
class Ternary {
    public static void main(String[] args)
    {
 
        // variable declaration
        int n1 = 5, n2 = 10, max;
 
        System.out.println("First num: " + n1);
        System.out.println("Second num: " + n2);
 
        // Largest among n1 and n2
        max = (n1 > n2) ? n1 : n2;
 
        // Print the largest number
        System.out.println("Maximum is = " + max);
    }
}

Output

First num: 5
Second num: 10
Maximum is = 10

Example 2:

// Java code to illustrate ternary operator
 
import java.io.*;
 
class Ternary {
    public static void main(String[] args)
    {
 
        // variable declaration
        int n1 = 5, n2 = 10, res;
 
        System.out.println("First num: " + n1);
        System.out.println("Second num: " + n2);
 
        // Performing ternary operation
        res = (n1 > n2) ? (n1 + n2) : (n1 - n2);
 
        // Print the largest number
        System.out.println("Result = " + res);
    }
}

Output

First num: 5
Second num: 10
Result = -5

Referensi