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:
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:
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.
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.