I feel dumb

onetwenty8k

2[H]4U
Joined
Nov 24, 2006
Messages
2,554
I don't know why but I just can't seem to grasp logical operators, they simply don't seem logical to me.

A simple example that I just can't seem to grasp is if I want to have a condition where an integer has to be less than x or greater than y. I just don't see how this doesn't work, please enlighten me.

Code:
        Scanner console = new Scanner(System.in);
        int num;
        do {
            System.out.print("Enter a number: ");
            num = console.nextInt();
        } while (num < 20 || num > 60);
 
I'm not really sure where you're confused. It seems more like you just aren't thinking through what you write:

You want a number that is either less than X, or greater than Y. In this case, your X is 20, and your Y is 60.

If we read through your statement, it says "do loop while num is less than 20 OR num is greater than 60." That's exactly what you wanted, and it's exactly what you got. If you run this code, and enter say... 25, it will break the loop because 25 is neither less than 20 or greater than 60. Again, this is exactly what you wanted.
 
I guess I reversed what I wanted. I want the loop to repeat until you get a number that is either less than 20 or greater than 60.
 
Talking/writing things out in plain English will will help you understand stuff that like a lot easier. Logical operators are logical, I promise. ;-)
 
Also with your Do-While loop ... Logically speaking, do you want to have the conditions within the loop only go if "x" is within the range/boundaries needed? Your choice of Do-While will process the code within the loop at least once before even hitting the "while" operator check.

Just wanted to get some clarification on this given the subject matter of the thread...
 
I guess I reversed what I wanted. I want the loop to repeat until you get a number that is either less than 20 or greater than 60.

If you're having a tough time understanding logical operators, I suggest you get to know truth tables very well. If you get those down and commit them to memory, doing any type of logical operation will come as second nature.

With me, I learned truth tables before programming and it helped greatly.

Best of luck!
 
Also with your Do-While loop ... Logically speaking, do you want to have the conditions within the loop only go if "x" is within the range/boundaries needed? Your choice of Do-While will process the code within the loop at least once before even hitting the "while" operator check.

Just wanted to get some clarification on this given the subject matter of the thread...

Yes, if it isn't less than 20 or greater than sixty then I want it to loop.

I ended up doing this but what is the better way of doing it?

Code:
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        int num;
        do {
            System.out.print("Enter a number: ");
            num = console.nextInt();
        } while (!(num < 20 || num > 60));
    }
 
Yes, if it isn't less than 20 or greater than sixty then I want it to loop.

I ended up doing this but what is the better way of doing it?

Code:
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        int num;
        do {
            System.out.print("Enter a number: ");
            num = console.nextInt();
        } while (!(num < 20 || num > 60));
    }
yeah, the ! operator is just making your statement longer and (possibly) harder to read for no reason.

Code:
do {
  ...
} while(num > 20 && num < 60)

would probably be better. If you want to include 20 and 60, you would use <= and => instead.

Because of the && operator, both comparisons must be true for the loop to occur. If a number is between 20 and 60, its has to be greater than 20 and less than 60, right?

Think of this as an inequality: 20 < x < 60. Separate it out, and you've got 20 < x and x < 60.

The && just assures that both statements are true. With the || operator, Java is only testing until it finds at least one true statement (other wise it returns false).
 
Back
Top