Java Conditional Operators

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.

OperatorDescription
&&Conditional-AND
||Conditional-OR

Screenshots of Conditional Operator Java Program

Simple Java Program Conditional operators:



//Simple Java Program  Conditional Operator

public class ConditionalOp {

    public static void main(String[] args){
        int num1 = 20;
        int num2 = 10;
        if((num1 == 20) && (num2 == 10))
            System.out.println("num1 is 20  AND  num2 is 10");
        if((num1 == 20) || (num2 == 10))
            System.out.println("num1 is 20  OR  num2 is 10");
    }
}
 

Output Here In cmd Window

Another conditional operator is '?:', which can be thought of as shorthand for an if-then-else statement This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true, assign the value of num1 to result. Otherwise, assign the value of num2 to result."

See The Simple Java Conditional Operators Program


   
//simple java program of ConditionalOp with using Scanner class

import java.util.Scanner; 
 public class ConditionalOprtr {
   public static void main(String args[]){
  //create Scanner object to obtain input from keyboard
     Scanner input=new Scanner(System.in);
     System.out.print("Enter the Three Number : ");  //input from Keyboard
     int  num1=input.nextInt();   //Read First number
     int  num2=input.nextInt();   //Read Second number
     int  num3=input.nextInt();   //Read third number
     int result = (num1>num2)? ((num1>num3)?num1:num3) : ((num2>num3)?num2:num3);
     System.out.println( "Greater Number is :="  +result);

    }
}  
    
     

Download Source code 'Java Conditional Operators Program'

Output Here In cmd Window

Java Operators
Arithmetic Operators
Assignment Operator
Bitwise Operators
instanceof Operator
Relational Operators
Unary Operators
Conditional Operators

References : oracle

share on :        :  

Love to hear your Views / Guidance / Recommendations on this Post…

Explore the Technology World

The mind behind Linux & GIT : Linus TorvaldsLinus Torvalds transformed technology twice — first with the Linux kernel, which helps power the Internet, and again with Git, the source code management syst ...Read MoreTensorflow and deep learningWith Tensorflow ,deep machine learning transitions from an area of research into mainstream software engineering. ...Read MoreJava program to return all possible permutations from collection of distinct numbersexample of Java program to return all possible permutations from collection of distinct numbers ...Read MoreReal Time Analytics with Spring Application Development FrameworkSpring is the most popular application development framework for enterprise Java.Millions of developers use Spring to build simple, truly portable, fast and fle ...Read MorePath Setting in Java When an application is launched from command prompt Windows OS searches for the executable program in the current working directory. The prompt throws an error ...Read MorePicture-in-Picture App for Android devices with Source CodePicture-in-Picture is a special type of multi-window mode mostly used for video playback for Android devices.Released under Apache License 2.0 , which means you ...Read MoreFacebook |Google+ | Twitter | LinkedIn | Youtube About | Cookies-Policy | Disclaimer | Site Map | Contact

Call / Visit for New Batch

Services

Why Join Us

  • ➯ Free Demo Classes
  • ➯ No Registration Fee
  • ➯ Interview Questions
  • ➯ Study Materials
  • ➯ Softwares
  • ➯ Aptitude & Reasoning
  • ➯ Placement Assitance

Address

  • MyJava Training Institute ,
  • Plot No.243 , shop No 14 ,
  • Secnd Floor , Zone-II ,
  • M.P.Nagar , Bhopal,
  • Madhya Pradesh ( 462011 )
  • ☎  08871209400 , 7987028543
  • 🌐 www.myjava.in
FacebookGoogle +TwitterYouTube

MyJava - java Training Institute