# Logical Operators (and Booleans)

In previous nodes, we showed how you can use `if`, `while` and `for` statements to execute code only when a certain condition is true. This works fine if you just care about checking one condition, but sometimes you will want to run code when either A OR B is true, or only when A AND B are true.

In such cases, you will want to use the logical operators "And" and "Or". In Java they are written as `&&` and `||`. For example:

``````int a = 5;
int b = 7;
boolean aLargish;
boolean bLarger;            //declare a true/false variable

bLarger =  b > a && a > 0;  //true, since both true.
aLargish =  a > b || a > 0; //true, since a>0 and it's an OR statement
boolean both =  aLargish && bLarger; //true again!
``````

In the above code, the results of the first comparison was stored in the boolean variable `bLarger`. As in the previous node, this could then be used in a conditional statement :

`````` if(bLarger){
//do something...
}
``````

Alternatively, you can put the expression itself in the if-statement without using a variable:

`````` if(b > a && a > 0){
//do something...
}
``````

Note that:

• `a && b` will return `true` only when BOTH `a` AND `b` are true.
• `a || b` will return `true` when either `a` AND/OR `b` are true.

Challenge
As briefly mentioned in About Programming, computers ultimately calculate just by doing simple logical operation like AND and OR on Logic Gates. While these gates are physical parts of a computer, you can simulate them in software. The NAND gate is a logical gate that returns false only when both its inputs are true. In other cases it returns true. You will simulate a NAND gate in this challenge. Try to do this challenge without using any `if` statements.

### Challenge

You will be given two integer inputs, `a` and `b`. Simulate a NAND gate by returning a boolean false only if both inputs are equal to 1, and returning true in all other cases.

Alternatively, you can try out Learneroo before signing up.

• #### Carlos Díaz Ramirez

inverse way also works 'a==1 && b ==1'

• #### Carlos Díaz Ramirez

'a==1 && b ==1' can work too

• #### Learneroo

@Carlos, with an overall not `!`, yes.

• #### pat

What is the code to solve this?

• #### Vignesh Prabhu S

boolean bLarger = ( a==1 && b==1);
return (!bLarger);
this works

• #### Jesse Li

can be done in 1 line of code using sums
'return ((a + b) != 2);'

• #### mistermase

boolean gates = a!=1 || b!=1;

• #### Ellis Keith

The not so advanced one line solution:

return( ! (a == 1 && b ==1));

• #### joeal

return a!=1 || b!=1;

• #### flowra

also this solution is true :
return !(a==1 && b==1);

• #### Marlon Buella

`!((a == 1) && (b == 1))` also works!

• #### Stanixlav

This might be a slight cheat, but we know that a and b are both integers, meaning there are no fractions in play. Therefore, the following also works: my code

• import java.util.Scanner;
public class BooleanExm2
{
public static void main(String[] args)
{
int a;
int b;
Scanner input = new Scanner(System.in);

• #### Gary

Lol, my too-many-steps solution:

boolean inputa = a > b || a < b;
boolean inputb = a == 0 && b == 0;
boolean both = inputa || inputb;
return both;

• #### rajni dound

import java.util.Scanner;
public class Main(){

public static void main(String[] args){
int a= 4;
int b= 1;

``````boolean collect=(a==1 && b==1);
boolean recollect=(a==1||b==1);
System.out.println("recollect" + recollect);
System.out.println("collect" + collect);
}
``````

}

• thanks guys!