Sometimes you don't realize the mistake...

Tarrant64

Diamond Member
Sep 20, 2004
3,203
0
76
For a class I'm taking, I have to write psuedo code for a program and turn it in before the assignment. I usually have to turn in the assignment about a week later. Well, I wish I had did it all at once because it looked OK when I typed it out, but just now I went to do the actual program and realized I'm an idiot.

I was creating a while loop in a java program. I was trying to get the psuedo code down pretty quick on paper so I'd have a map to follow when I was doing the actual program. I tried to start the program going into a while loop, and I had it check a counter(only do this 3 times), and also had it check against a number it was passed through every loop. If the number was <= 0, then the program would quit. In order to get it to prompt for a number all 3 times though I had the it come up inside the while loop.

But, starting the program the while loop was looking for a number. :) Well hell, I didn't pass it one to start off with. So, I have turned in a bogus psuedo code, and an actual assignment that's nothing like it. oh well. dumb mistakes!!!!

So basically,

start
while (counter < turn && number <= 0) <---never passed a number...weak.
get number;
......
end while
stop

 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Psuedo code should be treated as the ideas and concept, not the implimentation.

It is a template for the code to be built from.
 

JasonE4

Golden Member
Mar 14, 2005
1,363
0
0
I would hardly say that your pseudo code is "nothing like" your implementation if you simply changed a while loop to a do while.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
start
get number;
while (counter < turn && number <= 0)
get number;
......
end while
stop

or

start
do
get number;
......
while (counter < turn && number <= 0)
stop
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
Originally posted by: Tarrant64

start
while (counter < turn && number <= 0) <---never passed a number...weak.
get number;
......
end while
stop
Or after start, number could have been initialized to something to let the loop be entered assuming there was nothing between the while and the get. I don't know why, but do..while loops confuse some people.

If that's the worst you've done, then you're not doing so bad.
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
Either will work - the setup will differ. The body of a do..while is always executed at least once. Depending on the circumstances, one may be more appropriate than the other. But I have seen some comments elsewhere about the undesirability of the do..while construct. Personal taste really...
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Agreed, it's personal taste.

Want to know something else?

I'm beginnning to change my opinion on gotos also.

I believed mindlessly that gotos were bad, but lately, seeing multiple exit points in my functions, I'm beginning to think they aren't so bad to converge all exit conditionals to one point.
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
I cut my teeth on programming in an era where goto's were verboten and considered sloppy. I still think they should be avoided but one every few years isn't the end of the world. In my experience, many exit points in a function indicates that the function needs to be redesigned or split into two or more functions. High level languages have many constructs which make goto's unnecessary. I do use break's in while and for loops which could be considered a form of a goto. Another reason not to use goto's is that you have to keep creating useful labels. And for a long time, goto's gave optimizer's fits - some just disabled optimization for that function.
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Originally posted by: seemingly random
I cut my teeth on programming in an era where goto's were verboten and considered sloppy. I still think they should be avoided but one every few years isn't the end of the world. In my experience, many exit points in a function indicates that the function needs to be redesigned or split into two or more functions.

Maybe redesigned, but not split as the reason for all the exit points were because of error checking.

I used to develop functions to have very deep levels of conditionals to make it one exit point, but I found I had trouble reading my own work when it started getting 4 or more deep. Lines had to be split, etc.

Now, I try to keep the levels shallow for readability. I'll try an dig up an example this weekend, I'm sure I have one in fisix.




 

Tarrant64

Diamond Member
Sep 20, 2004
3,203
0
76
Originally posted by: seemingly random
Originally posted by: Tarrant64

start
while (counter < turn && number <= 0) <---never passed a number...weak.
get number;
......
end while
stop
Or after start, number could have been initialized to something to let the loop be entered assuming there was nothing between the while and the get. I don't know why, but do..while loops confuse some people.

If that's the worst you've done, then you're not doing so bad.

A do/while is possible, but just the while was required for the code. I'm still just mad I didn't see it. It's a big "duh"!

Thanks for the comments!

 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
Originally posted by: Tarrant64
start
while (counter < turn && number <= 0) <---never passed a number...weak.
get number;
......
end while
stop
If you're looking for something to chew on, look at concepts instead of silly mistakes. What does "never passed a number" mean? I can only think of programmatically passing something - by value or by reference - to a function. Did you really mean "never initialized number"?
 

Tarrant64

Diamond Member
Sep 20, 2004
3,203
0
76
Originally posted by: seemingly random
Originally posted by: Tarrant64
start
while (counter < turn && number <= 0) <---never passed a number...weak.
get number;
......
end while
stop
If you're looking for something to chew on, look at concepts instead of silly mistakes. What does "never passed a number" mean? I can only think of programmatically passing something - by value or by reference - to a function. Did you really mean "never initialized number"?

Sure.