# Need help with Sentinel Control Loop

• March 20th, 2013, 10:13 PM
Roku
Need help with Sentinel Control Loop
I have to do this problem where I use a Sentinel Control loop to repeatedly get the lenghts of the base and height of a triangle from a user, call the method, and then display the Values of the three sides of the triangle. This all has to take place inside a sentinel control loop where the user has to enter a -1 to terminate the loop. My problem is that after I get the base and height and do the calculations, the loop will not terminate when -1 is entered only one time. Even though I have an or conditional in my while loop, the program will only terminate if both are -1. What do I have to do to get the loop to terminate as soon as a -1 is entered, but still be able to calculate the hyp using my method theHypotenuseIs?

Code Java:

```import java.util.*; public class Hypotenuse { public static double theHypotenuseIs(double base, double height) { double calculation, hyp; hyp= Math.sqrt((Math.pow(base,2) + Math.pow(base,2)));   return hyp; }   public static void main(String[]args) { final int FLAG = -1; double hypotenuse, base, height; Scanner input = new Scanner(System.in);     while(base!=FLAG ||height!=FLAG) {   base = input.nextDouble(); height = input.nextDouble();   hypotenuse = theHypotenuseIs(base, height);   System.out.println("The Base is:" + base);   System.out.println("The Height is:" + height);   System.out.println("The Hypotenuse is:" + hypotenuse);     } } }```

--- Update ---

Never mind, I found a workaround. IDK if it's good style, but I added a conditional after each input that will break if the value is -1.
• March 21st, 2013, 06:52 AM
Chris.Brown.SPE
Re: Need help with Sentinel Control Loop
The problem is your "or" condition. This should be an "and". The way your condition reads now, in basic english, is "while base is not -1 or height is not -1" so it evaluates, base is -1, but height isn't, keep going! If this is an "and" it will work as you expect.

Side note: the way your code is written, if the user puts in a -1 then it executes "theHypotenuseIs" and still puts output. Therefore your code is not stopping immediately after you get your stop condition. You should add in a mechanism to check for good input before doing the calculation and output.