• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bad coding/do while loop?  RSS feed

 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just going through another introductory text, and came across this:



isnt this bad form?

why would you have a while(!validInput), recieve a user input, and assume it is valid?? (validInput == true regardless of what is entered on line 9). In fact !validInput is a condition that is checked even before the user enters a number.

Sorry, I'm sure I've missed something, but this code, as pseudo code, reads incredibly badly in my honest, and probably incorrect, opinion.

am I on the right track? or have I just missed something in the do-while logic. I tend not to use this type of loop, which is why I'm going back over the basics


thanks,

 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:why would you have a while(!validInput), recieve a user input, and assume it is valid?? (validInput == true regardless of what is entered on line 9). In fact !validInput is a condition that is checked even before the user enters a number.


It's a do..while, so it always enters the loop once before checking the condition.
In addition, validInput only stays true if the input is between 1 and 10 (inclusive). Any other integer results in going around the loop again (ie validInput is set to false).
 
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
nick woodward wrote:
isnt this bad form?

why would you have a while(!validInput), recieve a user input, and assume it is valid?? (validInput == true regardless of what is entered on line 9). In fact !validInput is a condition that is checked even before the user enters a number.

The body of the do-while loop is executed before the loop condition is evaluated, therefore it's incorrect to say the loop condition is checked even before the user enters a number.

Whether or not this is bad form is really a matter of taste/style. The code first assumes that the input is good and then checks if the input was actually bad, in which case it would then make a correction by setting validInput to false. This is equivalent to:

Alternatively, you could also write:

Take your pick
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,

yeah I'm not a fan of this way of doing things, although i do understand that the 'do' is executed first.

Much prefer your way Junilu, which is the way I would've done it.

set the validInput to false, and test for validity.

my brain doesn't like it the other way around !!!

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--or--


 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the thing that grates on me is the "Do while NOT valid input". When obviously you want to do UNTIL not valid input. OR, conversely, do WHILE valid input.

I once read something by a guy who claimed he'd set up an entire college source on "NOT". Negation adds an extra kink in your thinking and gratuituous use of NOT isn't going to optimize your program, since that's one of the first things the compiler is going to optimize anyway.

One of the worst offenders was IBM's mainframe Job Control Language (JCL). You told it NOT to execute a program if a condition was NOT true. A great way to tie your head in kNOTs.
 
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
Tim Holloway wrote:Actually, the thing that grates on me is the "Do while NOT valid input". When obviously you want to do UNTIL not valid input. OR, conversely, do WHILE valid input.
A great way to tie your head in kNOTs.


A great example. Didn't you actually mean: do UNTIL valid input ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:--or--



Brrrrr. I hate it when people write for(;;) because they want an infinite loop. In my opinion while (true) { ... } is easier to understand. But having an infinite loop is itself already a code smell.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Carey Brown wrote:--or--



Brrrrr. I hate it when people write for(;;) because they want an infinite loop. In my opinion while (true) { ... } is easier to understand. But having an infinite loop is itself already a code smell.


You already have an infinite loop and you've had to introduce a new variable to determine when to break out of the loop.
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
** already pointed out.

yeah, i prefer while(true)

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!