Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, How to close do-while loop using multiple character boolean expressions?

This is quite a beginner question but I'm wondering why my do...while loop is not closing.

The program is supposed to loop while the user input is not 'C', 'c', 'F', or 'f'. It seems to close when just one boolean expression in the while section is valid but not if multiple are valid.

  public class CelsToFaren
  {
      public static void main(String[] args)
      {
          // scanner setup

          Scanner sc = new Scanner(System.in);
          // Variable declarations
          int celsius;
          int answerC;
          int farenheit;
          int answerF;
          char userLetter;
          do
          {
          // initial menu options
          System.out.println("Which temperature would you like to convert from? ");
          System.out.println(" >(C)elsius ");
          System.out.println(" >(F)arenheit ");
  
          // user input of C, c, F, or f to select option
          userLetter = sc.next().charAt(0);
  
          // if user input C or c
          if ((userLetter ==  'C' || userLetter == 'c'))
          {
              System.out.print("Please enter the temperature: ");
              celsius = sc.nextInt();
              answerC = ((celsius*9/5)+32);
              System.out.println("The answer is: " + answerC + " Farenheit ");
          }
          else
          {
  
              // if user input F or f
             if ((userLetter == 'F' || userLetter == 'f'))
              {
                  System.out.print("Please enter the temperature: ");
                  farenheit = sc.nextInt();
                  answerF = ((farenheit-32)*5/9);
                  System.out.println("The answer is: " + answerF + " Celsius ");
             }
              else
              {
                  // if user input not F, f, C, or c
                 if ((userLetter != 'F' || userLetter != 'f' || userLetter != 'C' || userLetter != 'c'));
                  {
                      System.out.println("Please enter a valid option");
                  }
              }
         }
 
         } while ((userLetter != 'c') || (userLetter != 'C') || (userLetter != 'f') || (userLetter != 'F'));
  
      }
  }
like image 528
DynoNap Avatar asked Aug 25 '21 04:08

DynoNap


People also ask

Is a do-while loop a Boolean expression?

The do-while loop executes a block of code while a boolean expression evaluates to true. It checks the boolean expression after each iteration. It terminates as soon as the expression evaluates to false.

Can you use != In a while loop?

You need to use while (result != 1 && result != 0) . This will loop only if the answer is both not equals to 1 and not equals to 0.

Can we use if condition inside while loop?

If you check inside the loop then it will not work it will still multiply the fact . You need to make sure that the num is not negative before you start the while loop. int num = 0; do { if (num<0){ System.


2 Answers

You need to change the exit logic.

In your case 1 | 0 | 0 = true so the loop continues. You need to change it to:

while ((userLetter != 'c') && (userLetter != 'C') && (userLetter != 'f') && (userLetter != 'F'));
like image 80
Alex Tbk Avatar answered Sep 30 '22 02:09

Alex Tbk


Your condition is wrong. Lets assume you want to break loop in if statement. It would look like

if(userLetter == 'c' || userLetter == 'C' || userLetter == 'f' || userLetter == 'F')

Now let's apply negation to get a condition under which you do not need to exit the loop

if(!(userLetter == 'c' || userLetter == 'C' || userLetter == 'f' || userLetter == 'F'))

this condition is simillar to

if(userLetter != 'c' && userLetter != 'C' && userLetter != 'f' && userLetter != 'F')
like image 44
vszholobov Avatar answered Sep 30 '22 02:09

vszholobov