Which costs more while looping; assignment or an if-statement? <p>Consider the following 2 scenarios:</p> <pre><code>boolean b = false; int i = 0; while(i++ &lt; 5) { b = true; } </code></pre> <p>OR</p> <pre><code>boolean b = false; int i = 0; while(i++ &lt; 5) { if(!b) { b = true; } } </code></pre> <p>Which is more "costly" to do? If the answer depends on used language/compiler, please provide. My main programming language is Java.</p> <p>Please do not ask questions like why would I want to do either.. They're just barebone examples that point out the relevant: should a variable be set the same value in a loop over and over again or should it be tested on every loop that it holds a value needed to change?</p>