简体   繁体   中英

Logic - Java programming do-while loops

So when you're coding in Java and doing a do-while loop, when there are multiple conditions to break the loop you have to use && (and) instead of || (or). I know that && will break the loop.

In this example, entering 1, 2, 3, or 4 breaks the loop, but why is it && !??! My Professor cant explain it..

import java.util.*;
public class ErrorTrap
{
    public static void main(String[] args)
    {
        System.out.println("Program by --");
        System.out.println();
        System.out.println("Error Traps");
        System.out.println();
        //initialize variables
        Scanner input = new Scanner(System.in);
        char year;

        //user input
            do {
                System.out.print("Please input a 1, 2, 3 or 4:  ");
                year = input.nextLine().charAt(0);

                //output
                System.out.println(num);
                System.out.println();
                }
                while(year != '1' && year != '2' && year != '3' && year != '4');          
    }//end main
}//end ErrorTrap

Let's say the user puts in 5

Let's check the result:

5 != 1 is true
5 != 2 is true
5 != 3 is true
5 != 4 is true

true && true && true && true is true so the loop keeps looping.

Once, for example, the user puts in 3 , we have the following :

3 != 1 is true
3 != 2 is true
3 != 3 is false
3 != 4 is true

true && true && false && true is false so we break off the loop.

Let's now say you think of using || and put in 3 :

3 != 1 is true
3 != 2 is true
3 != 3 is false
3 != 4 is true

true || anything true || anything gives out true so the code doesn't respect your specifications because you want to break off when entering 2 , 3 or 4


Little rule :

If you're using || and the first test is true , the compiler doesn't even bother testing the rest of the tests, output will be true .

Same goes with && and first test is false .

Your code inside do brackets will be executed as long as year not equal to '1' and'2' and '3' and '4' .In other way as long as year value not one of these . but if you used || instead of && , then code will be executed all time . Because one condition is enough year!='1' , even if year ='2' this will execute code.

&& is restricted, you should meet all conditions to validate code.
|| is not restricted, you just need to meet one condition of desired
   conditions to validate code.
while(year!='1' && year!='2')

Is the correct syntax for multiple conditions because the added "&&" allows the program to know that each condition is dependent to the other conditions in the statement, if one is false, the statement breaks. -- while "||" is used for independent statements.

For example,

String Apple = "Red";

if(Apple.equals("Red")||Apple.equals("Green")){
    return true;
} else {
    return false;
}

the if statement will return true, even though the Apple isn't Green, it checks to see if one condition is true.

While

String Apple = "Red";

if(Apple.equals("Red")&&Apple.equals("Green")){
return true;
} else {
return false;
}

This will return false no matter what, because both conditions are dependent on each other.

Hope this helped!

while(year != '1' && year != '2' && year != '3' && year != '4');

You'll use the && operator here because in order to break the loop here "year" must not be 1 AND it must not be 2 AND it must not be 3 AND it must not be 4.

If you were to use the OR operator ( || ), one of these could be false and another true, but your loop would still continue.

Basically, in order to break your loop you need year==1,2,3, and 4 to be false. If you use && , all of them have to be false. If you use || , not all of them have to be false for the loop to break.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM