• Post Reply Bookmark Topic Watch Topic
  • New Topic

do... while loop, questions with logic  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm learning online from sololearn using their web compiler, unsure if that was the problem. But here is my question:

The example code they give:



I don't understand why it only prints up to 4 when the condition asks to be larger than 5 (x<5). Shouldn't it print all the way up to 5 until the figure reaches 6 but not printed?

I then later added the = sign, now reads (x <= 5), and then it prints 5.

Please can someone explain me the logic?

Many thanks.
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHILE X is-less-than 5
As soon as it detects 5 it stops the loop before it goes back up to 'do'.
 
Jon Tsao
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:WHILE X is-less-than 5
As soon as it detects 5 it stops the loop before it goes back up to 'do'.


Succinct and made me think "oh yeahhhh"
 
Marshal
Posts: 56606
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch

We see lots of people who get < and > the wrong way round; I think <= and >= are even easier to get confused by.
 
Ranch Hand
Posts: 46
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1st do ... while loop --> print: 1 (by statement System.out.println(x)) and increase x into 2 (by statement: x++)
2nd do... while loop --> print: 2 (by statement System.out.println(x)) and increase x into 3 (by statement: x++)
3rd do... while loop --> print: 3 (by statement System.out.println(x)) and increase x into 4 (by statement: x++)
4th do... while loop --> print: 4 (by statement System.out.println(x)) and increase x into 5 (by statement: x++)
At that time the condition (x < 5) is false. So do... while loop is break, block code in do {} does not run
It is why the output is:
 
Campbell Ritchie
Marshal
Posts: 56606
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Tsao wrote:. . .. . .
I think you have got good explanations there (thank you Duc Ta), but you can get lots more out of that sort of code. I shall tweak it a bit, getting rid of line 4. If I simply delete line 4, you will get 1 1 1 1 1 1 1 .... printed until the Last Day or you turn your computer off or you push ctrl‑C whichever comes first. So you will have to put the ++ back somewhere. Assuming you only want one ++, I can see four places to put it:-How well have they taught you about ++? Can you predict what the four new versions will print? Will they have the same output or different output?
 
Ranch Hand
Posts: 200
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya a good question, however outputs came out to be different, but same also in 2 cases.
 
Campbell Ritchie
Marshal
Posts: 56606
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please let the OPs work out the complete answers for themselves.

It does go to show that control structures are by no means easy to learn; iteration is probably more difficult than selection. If you look here, you will see that there are other programming paradigms without such control structures. Is that do loop easier or harder to understand than this Streams codeYou do need to read about the range() method first, to work out whether it will produce a Stream of 1-2-3-4 or 1-2-3-4-5.

Even so, you still need to learn to write loops.
 
O Shea
Ranch Hand
Posts: 200
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya, you are right, Flow Control is easy but sometimes becomes quite confusing also, because sometimes we can't understand where the flow is going and it is incrementing within the loop.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!