Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my loop skipping this conditional?
    primarykey
    data
    text
    <p>Time for my daily newbie Java question :-D </p> <p>I must not be understanding conditionals in a while loop correctly. </p> <p>I have this:</p> <pre><code>while (true){ if (){ ... } else { ... } if (){ ... } else { ... } if (SENTINEL){ break; } } </code></pre> <p>The first if/else statement is working, and the sentinel is working, but the second if statement gets skipped. If I flip the first and second if statement, then the first if statement still always gets executed and skips the second. What am I missing?</p> <p>Can I have two if/else statements in one block like this?</p> <p>I'll include the whole code, though it's pretty ugly, and I'm sure I'll get lots of people telling me better ways of doing this. I don't mind learning better ways, but for now, I just want an answer to this looping question. thanks!</p> <pre><code>public class FindRange extends ConsoleProgram { private static final int SENTINEL = 0; int value = 0; int highNumber = 0; int latestValue = 0; int lowNumber = 0; public void run() { addNumbers(); } private void addNumbers(){ value = readInt("Enter number:"); while(true){ if (value == SENTINEL){ break; } latestValue = readInt("Enter number:"); getHighNumber(); getLowNumber(); if (latestValue == SENTINEL){ break; } } println("High Number is "+highNumber+"."); println("Low Number is "+lowNumber+"."); } private void getHighNumber(){ if (latestValue &gt;= value){ highNumber = latestValue; }else { highNumber = value; } } private void getLowNumber(){ if (latestValue &lt;= value){ lowNumber = latestValue; }else { lowNumber = value; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload