Conditional operator in java with example

Conditional operator is alternative to simple if-else statement

Syntax:-

expr1 ? expr2 : expr3

Execution:-

expr1 is always evaluated, if results is true than expr2 is executed otherwise expr3 is executed.

Example:-

Program to find given number is even or odd

import java.util.Scanner;

class group{
public static void main(String arg[]){
int num;
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num = data.nextInt();
System.out.println((num%2)==0 ? "even number":"odd number");
}
}


Previous Code:-
Bitwise operators

List Code:-
Java codes

Next Code:-
if decision making statement

Java Programs:-
List of Java Programs

Leave reply

Add your comments here

Back to Top