can you have two conditions in an if statement

66,643

Solution 1

You can use logical operators to combine your boolean expressions.

  • && is a logical and (both conditions need to be true)
  • || is a logical or (at least one condition needs to be true)
  • ^ is a xor (exactly one condition needs to be true)
  • (== compares objects by identity)

For example:

if (firstCondition && (secondCondition || thirdCondition)) {
    ...
}

There are also bitwise operators:

  • & is a bitwise and
  • | is a bitwise or
  • ^ is a xor

They are mainly used when operating with bits and bytes. However there is another difference, let's take again a look at this expression:

firstCondition && (secondCondition || thirdCondition)

If you use the logical operators and firstCondition evaluates to false then Java will not compute the second or third condition as the result of the whole logical expression is already known to be false. However if you use the bitwise operators then Java will not stop and continue computing everything:

firstCondition & (secondCondition | thirdCondition)

Solution 2

Here are some common symbols used in everyday language and their programming analogues:

  • "," usually refers to "and" in everyday language. Thus, this would translate to the AND operator, &&, in Java.
  • "/" usually refers to "or" in everyday language. Thus, this would translate to the OR operator, ||, in Java.

"XOR" is simply "x || y but both cannot be true at the same time". This translates to x ^ y in Java.

In your code, you probably meant to use "or" (you just used the incorrect "incorrect solution" :p), so you should use "||" in the second code block for it to become identical to the first code block.

Hope this helped :)

Solution 3

You're looking for the "OR" operator - which is normally represented by a double pipe: ||

 if (b.equals("good") || b.equals("it was good")) {
        System.out.println("Thank goodness");
 }  else if (b.equals("bad") || b.equals("it was bad")) {
        System.out.println("Why was it bad?");
        String c = scanner3.nextLine();
        System.out.println("Don't worry, everything will be ok, ok?");
        String d= scanner10.nextLine();
 }

Solution 4

This is probably more answer than you need at this point. But, as several others already point out, you need the OR operator "||". There are a couple of points that nobody else has mentioned:

1) If (b.equals("good") || b.equals("it was good")) <-- If "b" is null here, you'll get a null pointer exception (NPE). If you are genuinely looking at hard-coded values, like you are here, then you can reverse the comparison. E.g.

if ("good".equals(b) || "it was good".equals(b))

The advantage of doing it this way is that the logic is precisely the same, but you'll never get an NPE, and the logic will work just how you expect.

2) Java uses "short-circuit" testing. Which in lay-terms means that Java stops testing conditions once it's sure of the result, even if all the conditions have not yet been tested. E.g.:

if((b != null) && (b.equals("good") || b.equals("it was good")))

You will not get an NPE in the code above because of short-circuit nature. If "b" is null, Java can be assured that no matter what the results of the next conditions, the answer will always be false. So it doesn't bother performing those tests.

Again, that's probably more information than you're prepared to deal with at this stage, but at some point in the near future the NPE of your test will bite you. :)

Share:
66,643
myt
Author by

myt

Updated on March 13, 2020

Comments

  • myt
    myt about 4 years

    I'm a beginner in coding. I was recently working with to create a chatting programme where a user will chat with my computer. Here is a part of the code:

    System.out.println("Hello, what's our name? My name is " + answer4);
    String a = scanner1.nextLine();
    System.out.println("Ok, Hello, " + a + ", how was your day, good or bad?");
    String b = scanner2.nextLine();
    
    **if (b.equals("good"))** {                //1
        System.out.println("Thank goodness");
    } else **if (b.equals("it was good"))** {  //2
        System.out.println("Thank goodness");
    } else **if (b.equals("bad"))** {          //3
        System.out.println("Why was it bad?");
        String c = scanner3.nextLine();
        System.out.println("Don't worry, everything will be ok, ok?");
        String d= scanner10.nextLine();
    } else **if (b.equals("it was bad"))**{    //4
        System.out.println("Why was it bad?");
        String c = scanner3.nextLine();
        System.out.println("Don't worry, everything will be ok, ok?");
        String d= scanner10.nextLine();
    }
    
    if(age<18){System.out.println("How was school?");}
    else if (age>=18){System.out.println("How was work?");}
    

    The conditions of the if statements are in Bold (surrounded with **). In case of first and the second condition I want my application to do same thing. Similarly third and fourth condition. I thought it was possible to somehow group them in if statement.

    I tried with below code but it doesn't compile:

    if (b.equals("good"), b.equals("it was good")) {
        System.out.println("Thank goodness");
    }  else if (b.equals("bad"),(b.equals("it was bad"))) {
        System.out.println("Why was it bad?");
        String c = scanner3.nextLine();
        System.out.println("Don't worry, everything will be ok, ok?");
        String d= scanner10.nextLine();
    }
    

    Can someone correct it for me?

  • myt
    myt almost 7 years
    Thank you so much! This is really helpful!
  • myt
    myt almost 7 years
    Thank you! This is really helpful!
  • myt
    myt almost 7 years
    Thank you! This is really helpful!
  • myt
    myt almost 7 years
    Thank you! This is really helpful!
  • myt
    myt almost 7 years
    so in my code, if I want to use both in my condition, is this what I have to do: if ((b.equals("good"))||(b.equals("it was Good") )){ System.out.println("Thank goodness"); }
  • Zabuzard
    Zabuzard almost 7 years
    Yes exactly. Just combine the statements with the logical operators.