• Post Reply Bookmark Topic Watch Topic
  • New Topic

While loop (need understanding)  RSS feed

 
Thabo Ntsane
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wooow thanks this is interesting

Thank you
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Junilu. I hope to learn more in this forum as I am a self taught noob.
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you avoid <= and >=. Not because they are wrong, but because > and < are easier to understand.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!