Describe logical operators in java, JAVA Programming

Assignment Help:

Describe Logical Operators in Java ?

The relational operators you've learned so far (<, <=, >, >=, !=, ==) are enough while you only required to check one condition. Therefore what if a particular action is to be taken only if various conditions are true? You can use a series of if statements to test the conditions, as follow:

if (x == 2) {
if (y != 2) {
System.out.println("Both conditions are true.");
}
}

This, therefore, is hard to write and harder to read. It just gets worse as you add more conditions. Providentially, Java gives an easy way to handle multiple conditions: the logic operators. There are three logic operators, &&, || and !.
&& is logical and. && merges two boolean values and returns a boolean that is true if and only if both of its operands are true. For example

boolean b;
b = 3 > 2 && 5 < 7; // b is true
b = 2 > 3 && 5 < 7; // b is now false
|| is logical or. || combines two boolean variables or expressions and returns a result in which is true if either or both of its operands are true. For example

boolean b;
b = 3 > 2 || 5 < 7; // b is true
b = 2 > 3 || 5 < 7; // b is still true
b = 2 > 3 || 5 > 7; // now b is false
The last logic operator is ! which means not. It reverses the value of a boolean expression. Thus if b is true !b is false. If b is false !b is true.

boolean b;
b = !(3 > 2); // b is false
b = !(2 > 3); // b is true
These operators permit you to test multiple conditions more simply. For instance the previous example can now be written as
if (x == 2 && y != 2) {
System.out.println("Both conditions are true.");
}
That's a lot clearer.


Related Discussions:- Describe logical operators in java

Differentiate local and global variables, Differentiate Local and Global Va...

Differentiate Local and Global Variables? Local variables are the variables have limited scope although global have bigger scope Local variables are not accessed through others

Custom website technical specification document, Custom Website Technical S...

Custom Website Technical Specification Document A Solution provider / System Architect to interpret and speak with me and build business functional needs and a set of research d

Write the javascript code for the function sumoddnumbers, Write the JavaScr...

Write the JavaScript code for the Function SumOddNumbers(maxNumber) in which can add non-negative odd numbers up to maxNumber and return their sum. Java Script Code

Applying java if and if..else statements, For this Assignment, submit the f...

For this Assignment, submit the following program: Create an application for an animal-fur trimming service. The business is open 15 weeks of the year, from April through July. The

Luminous jewels, Luminous Jewels - The Polishing Game Byteland county is v...

Luminous Jewels - The Polishing Game Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various lum

??????????, Ask question ##title.. ????? ?????#title.. ????? ?????#title....

Ask question ##title.. ????? ?????#title.. ????? ?????#title.. ????? ?????Minimum 100 words accepted#

Explain intelligent system, What is an intelligent system? Intelligent sy...

What is an intelligent system? Intelligent systems are the programs established to perform complicated jobs in which reflect human brain and thoughts. If the algorithms are too m

Implementation of function, Suppose that: A is the set of characters in ...

Suppose that: A is the set of characters in the string "pellet"; B is the set of characters in the string "teller"; C is the set of characters in the string "pertinent"; a

Create an interface with a method concatenation, a)  Using Java, create an ...

a)  Using Java, create an interface with a method called concatenation which will be implemented in the class patient in part b. b)  Using Java, write the class patient with the

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd