-2

please take a look at this piece of code and explain what is going on here. When I use boolean || operator it is giving me an infinite loop but when I use && operator it seems fine. How come? I put in some debug lines too. Same thing if I rewrite it as a do-while loop. So, when I replace: while (userStatus != 1 || userStatus != 2) with: while (userStatus != 1 && userStatus != 2) it is good. Thanx guys!


import java.util.Scanner;

public class Example
{
  public static void main(String[] args)
  {

    Scanner in = new Scanner(System.in);
    int userStatus = 0;
    String maritalStatus = "";

              System.out.println("userStatus variable value outside the loop: " + userStatus); //debug

    while (userStatus != 1 || userStatus != 2)
    {
              System.out.println("userStatus variable value inside the loop: " + userStatus); //debug

      System.out.println("Enter status: ");

      maritalStatus = in.next().toUpperCase();

      if (maritalStatus.equals("SINGLE"))
      {
        userStatus = 1;
                System.out.println("after SINGLE userStatus is assigned value: " + userStatus); //debug
      }
      else if (maritalStatus.equals("MARRIED"))
      {
        userStatus = 2;
                System.out.println("after MARRIED userStatus is assigned value: " + userStatus); //debug
      }

                System.out.println("userStatus value inside the loop after IF: " + userStatus); //debug
    } //end while

    System.out.println("Value of entered String is " + userStatus);

  } //end main
} //end class

  • 1
    Think about it. It's guaranteed that `userStatus != 1 || userStatus != 2` always evaluates to `true` because `userStatus` cannot ever be equal to `1` and `2` simultaneously. – Slaw Apr 27 '20 at 02:41
  • 1
    Does this answer your question? [Why non-equality check of one variable against many values always returns true?](https://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true) – Savior Apr 27 '20 at 02:41
  • 1
    `userStatus != 1 || userStatus != 2` will be **always** `True` Since `userStatus` will never be 1 and 2 at the same time, at least one of the conditions will be always satisfied. – PM 77-1 Apr 27 '20 at 02:41
  • Ok so... userStatus IS NOT EQUAL TO 1 (TRUE)....OR userStatus IS NOT EQUAL TO 2 (TRUE) so loop continues....but when user input is "single" it equals to 1 ... so first condition is FALSE and second is TRUE ...but loop continues – Albert Krowowitz Apr 27 '20 at 03:03
  • So if using &&.... after userStatus becomes 1 then userStatus != 1 (FALSE) && userStatus != 2 (TRUE) ..... both conditions must be true for the loop to continue... so it exits.........This comment is just for me so I can summarize.....Its getting late... :)) – Albert Krowowitz Apr 27 '20 at 03:28
  • It's easy.......thanks guys! – Albert Krowowitz Apr 27 '20 at 03:29

1 Answers1

1

It is also possible to apply De Morgan's laws and change your working && into a working ||. This

userStatus != 1 && userStatus != 2

is logically equivalent to

!(userStatus == 1 || userStatus == 2)
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249