Computer Science Canada

While loop repeats even if conditions are met

Author:  username123 [ Mon Nov 16, 2015 5:52 pm ]
Post subject:  While loop repeats even if conditions are met

What the title says. Here's my code:
Java:

System.out.println("Enter your payment method (supported: Visa, Mastercard, Paypal, cash");
        String payment = input.next();
        do
        {
          System.out.println("Invalid payment method.");
          System.out.println("Enter your payment method (supported: Visa, Mastercard, Paypal, cash");
          payment = input.next();
        } while(!payment.equals("visa")||!payment.equals("Visa")||!payment.equals("mastercard")||!payment.equals("Mastercard")||!payment.equals("paypal")||!payment.equals("Paypal")||!payment.equals("cash")||!payment.equals("Cash"));

Author:  Zren [ Mon Nov 16, 2015 7:31 pm ]
Post subject:  RE:While loop repeats even if conditions are met

Use payment.equalsIgnoreCase("visa") instead of doing 2^(lengthOfString) comparisions to check all possibilities.

do {} while(); loops will always run once. It will not check the exit condition until the end of the first iteration.


: