Java: Java Programming For Beginners a simple Start To Java Programming (Written By a software Engineer)


Download 0.82 Mb.
Pdf ko'rish
bet23/95
Sana09.01.2022
Hajmi0.82 Mb.
#261112
1   ...   19   20   21   22   23   24   25   26   ...   95
Bog'liq
Java Programming For Beginners - A Simple Start to Java Programming ( PDFDrive )

Conditional Operator ( ? : ):
The  conditional  operator  is  a  ternary  operator  that  contains  three  operands.  Essentially,
this operator is used for the evaluation of boolean expressions. The operator tests the first
operand or condition and if the condition is true, then the second value is assigned to the
variable. However, if the condition is false, the third operand is assigned to the variable.
The syntax of this operator is as follows:
variable a = () ? valueiftrue : valueiffalse
Sample implementation:
public class myTest {
public static void main(String args[]){


int x, y;
x = 5;
y = (x == 5) ? 15: 40;
System.out.println( “y = ” + y );
y = (x == 34) ? 60: 95;
System.out.println( “x = ” + y );
}
}
The compilation and execution of this code shall give the following result:
y = 15
y = 95

Download 0.82 Mb.

Do'stlaringiz bilan baham:
1   ...   19   20   21   22   23   24   25   26   ...   95




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling