• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Do...While with two conditions: Question

00PS

[H]ard|Gawd
Joined
May 5, 2009
Messages
1,798
I am working on a program in java that performs a series of calculations then once finished asks a user to either quit performing these tasks or perform the calculations again. Using a sentinel I wanted the user to enter a 1 or 2 to make their choice. A sample code is as follows:

Code:
import java.util.Scanner;

public class DoWhileWithSentinel
{

    public static void main(String[] args)
    {
    	int choice;

	Scanner s = new Scanner( System.in );
  	do
	{
		System.out.print( "Type \"1\" for yes or \"2\" for no: " );
		choice = s.nextInt();
	} while (choice != 1 || choice != 2 ); // this part is what is confusing me

	System.out.println(choice);
    }
}

After coding it up, I couldn't exit the loop no matter what number I entered (i.e. 1, 2, 3, 4..). Finally after pulling my hair out I just decided to use the && logical operator:

Code:
} while (choice != 1 && choice != 2 ); // note the && and NOT the original ||

And it works! So now the user cannot move on without entering a correct value (obviously anything entered other than an integer is going to return an error, and I plan to actually make the user enter "y" or "n" not integers later in development. But for the sake of simplicity this is my example).

Why does this have to be AND(&&) and not OR(||) for this to work? I am no programmer and don't intend to be one, but I do have a pretty decent grasp on the concepts. This certainly threw a wrench into my comprehension. Can someone explain this?


The way I read it is:
Lets say the user inputs 2 and the while condition is: answer != 1 OR(||) answer !=2. When checking the condition it should check that 2(answer) != 1 : True *then* check 2(answer) != 2 : False and finally jumpst out...

FUCK...I'm an idiot. After typing this up I think I figured it out. WHILE statements need to be true to keep looping and need to be false to jump out. That being said, the boolean operator OR can have one True, one Fase to still be true...thus WHILE ( answer = TRUE) is going to keep looping. AND condition must BOTH be true to return true ( or false to return false). In this example if 2 != 1 is FALSE and 2!= 2 is FALSE thus it returns false and jumps out of the loop. I guess I just needed to type it out to help visualize what what really going on.

There really isn't any point to posting this now except maybe the seasoned programmers can get a good laugh.
 
FUCK...I'm an idiot. After typing this up I think I figured it out. WHILE statements need to be true to keep looping and need to be false to jump out. That being said, the boolean operator OR can have one True, one Fase to still be true...thus WHILE ( answer = TRUE) is going to keep looping. AND condition must BOTH be true to return true ( or false to return false). In this example if 2 != 1 is FALSE and 2!= 2 is FALSE thus it returns false and jumps out of the loop. I guess I just needed to type it out to help visualize what what really going on.

There really isn't any point to posting this now except maybe the seasoned programmers can get a good laugh.
Thanks, it made me smile, atleast you now know. Sometimes just reading about concepts in a book isn't enough, it's when you put it into action you really learn how it works. Even now when I am working on a big project I will have some small ones on the side for proof of concept type stuff.
 
I knew that a WHILE loop had to be false to jump out, but with adding the != and the || statement operators I was overlooking the entirety of the expression. Typing it up helped visualize what was really happening.

I ended up changing the condition to make it easier to read (for me at least).

Example:
Code:
} while (  !(choice == 1 || choice == 2)  ); // used a logical NOT in front
 
I knew that a WHILE loop had to be false to jump out, but with adding the != and the || statement operators I was overlooking the entirety of the expression. Typing it up helped visualize what was really happening.

I ended up changing the condition to make it easier to read (for me at least).

Example:
Code:
} while (  !(choice == 1 || choice == 2)  ); // used a logical NOT in front
You just rediscovered De Morgan's laws. :)

In words: "(Not A) and (Not B)" is the same as "Not (A or B)"

You may become a programmer, yet ;)
 
DeMorgans law was the reason I changed it. Lol I had to dig back, deep in my brain, for all those Boolean algebra lessons. :p
 
In my opinion, this is the joy of programming (well, aside from the creating things and making money part:p). There are few things more satisfying than taking a step back, thinking about the problem, and that light bulb clicking, no matter how simple or difficult the problem.

Good on the OP for keeping at it and figuring it out on his own.
 
Back
Top