programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Paul Clapham
• Devaka Cooray
• Bear Bibeault
Sheriffs:
• Junilu Lacar
• Knute Snortum
• Liutauras Vilda
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Piet Souris
Bartenders:
• salvin francis
• Carey Brown
• Frits Walraven

# While loop (need understanding)

Greenhorn
Posts: 9
The loop was supposed to end at 12 but the value is 14 how is that possible can you please explain to me how the program does this. hence the loop is: while (number <=12)
I want the value of "number" to be 12 without making an infinite loop

HERE'S THE OUTPUT AFTER RUNNING THE PROGRAM

0
2
4
6
8
10
12

The value of 'number' is: 14
14

Ranch Hand
Posts: 89
The "while" loop has to become false at some point or it will run forever.
The while loop will run when the value of number = 12 and the number is incremented to 14 (number += 2).
When the while loop tries to run again the boolean condition evaluates to false (since 14 is not less than or equal to 12) and the program comes out of the loop.
If you want the number to be equal to 12 after the while loop try this:

while(number < 12) {
number += 2;
}

Thabo Ntsane
Greenhorn
Posts: 9
Thank you so much. I ran the program and the value of number is now 12

So in other words the loop must be less than 12 in the parameters, the program runs until 10.

Is it possible to run the program until 12 and have a print of the value of 'number" which is also 12?

I'm sorry if I sound stupid but I need to understand the simple things first.

Ashwin Rao
Ranch Hand
Posts: 89
Here's what you can do if you want the while loop to run for number = 12 and have the number's value stay as 12:

while(number <= 12) {
if(number == 12)
break;
number += 2;
}

What the above code does is: everytime the loop is executed,the "if" condition is checked. The if condition will be false for 0, 2, 4, 6, 8 and 10. But it will be true for 12. so When the if condition becomes true you come out of the while loop using the "break" statement. The break statement will cause the program to immediately come out of the loop (and starts executing the statements immediately after the loop) so the "number += 2" statement will not be executed and the value of number will stay as 12.

Thabo Ntsane
Greenhorn
Posts: 9
Wooow thanks this is interesting

Thank you

Sheriff
Posts: 15525
263

Thabo Ntsane wrote:Wooow thanks this is interesting

Thabo,

If you take the last solution as a way to understand how the while-loop works, that's fine. However, even though it does what you wanted, you should NOT do anything like this in real-world code.

And Welcome to the Ranch!

Thabo Ntsane
Greenhorn
Posts: 9
Thank you Junilu. I hope to learn more in this forum as I am a self taught noob.

Ashwin Rao
Ranch Hand
Posts: 89

Junilu Lacar wrote:

Thabo Ntsane wrote:Wooow thanks this is interesting

Thabo,

If you take the last solution as a way to understand how the while-loop works, that's fine. However, even though it does what you wanted, you should NOT do anything like this in real-world code.

And Welcome to the Ranch!

What would you use in real-world code?

Junilu Lacar
Sheriff
Posts: 15525
263

Ashwin Rao wrote:What would you use in real-world code?

What I was hinting at not doing in that other code snippet was to break out of a while loop based on a condition that the while loop already evaluates anyway. Any break inside a loop should be re-examined and re-evaluated. There are times where it helps streamline otherwise already messy or repetitive logic but most of the time, a break can be avoided without sacrificing brevity or clarity.

Since the "requirements" that were originally stated were kind of hypothetical in the first place, I'd have to modify or clarify them a bit so that they are more "realistic". So let's say 12 was some kind of LIMIT or MAX and that instead of the System.out.println() you wanted do something significant with numbers <= MAX. Then after that, you wanted to do something with MAX again. While still hypothetical, it seems more plausible that these requirements could align with a real-world scenario.

I would then probably do something like this:

So the approach is to make each step of the process as independent as possible. If there is some common value, you will usually extract that value to a constant. Again, this is just a hypothetical scenario but the principles of separation of concerns, low coupling, high cohesion, and modularity are basic to any good program. The other thing you may notice is that I did not use a while-loop at all but instead used a more appropriate for-loop.

I know this is quite a leap from the simple while-loop that was originally discussed. I just wanted to make sure that besides understanding how the while-loop works, the OP realized that there are certain things that you should try to avoid when using a while-loop and certain situations where the while-loop may not be the right thing to use at all.

Marshal
Posts: 68919
275
I would suggest you avoid <= and >=. Not because they are wrong, but because > and < are easier to understand.

Junilu Lacar
Sheriff
Posts: 15525
263

Campbell Ritchie wrote:I would suggest you avoid <= and >=. Not because they are wrong, but because > and < are easier to understand.

Hmmm... can you give an example of logic that uses <= or >= that would be easier to understand by using < or > ?

I know that it's more idiomatic in Java, as in C and C++, to use < when using a for loop and an index to iterate over a zero-based collection but I don't know if I would necessarily say that one is easier to understand that the other in general.

Marshal
Posts: 25452
65
In my case, if I see a for-loop and it doesn't look like this:

then I always have to stop and ask myself why it doesn't look like that, and what it's supposed to be doing instead. Sure, there's reasons to write for-loops which don't look like that, but I find those harder to understand. So if I saw this loop:

it would take me a few seconds to realize it was actually doing the same thing as my preferred version.

Junilu Lacar
Sheriff
Posts: 15525
263

This is definitely non-idiomatic in Java, so I agree, it's something that would give me a reason to pause and examine carefully. However, in cases where the increment clause is non-idiomatic (anything other than i++), I would say that you have to pause and examine the loop termination condition anyway so I don't see how <= can be less understandable than < in those cases. wouldn't necessarily think that <= is less understandable than < in those cases; it just depends, right? (I know, the standard consultant escape clause)

 Not looking good. I think this might be the end. Wait! Is that a tiny ad? Two software engineers solve most of the world's problems in one K&R sized book https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton