NZGames.com Forums
Register FAQ Calendar Mark Forums Read

Go Back   NZGames.com Forums > General > Coders' Forum
User Name
Password

Reply
 
Thread Tools
Old 22nd August 2004, 19:45     #1
Kudos
 
Converting while loops to if statements

how would one go about doing this?
cause for one assignment I'm doing at the moment, we are not allowed to use loops of any kind including recursion.
i can only think of cases and if to do it.
  Reply With Quote
Old 22nd August 2004, 19:50     #2
LordP
 
That doesn't make any sense to me - what's the point in teaching you to not use a fundamental programming er, thing...?
  Reply With Quote
Old 22nd August 2004, 20:10     #3
Baal
 
You could emulate the behaviour of a while by using a goto or similar. But it really defeats the purpose.

Yeeech.
  Reply With Quote
Old 22nd August 2004, 20:20     #4
samael
 
Maybe its to make you think of the problem in a different way, find an alternate solution that doesnt involve looping, or iterating over the same set of operations.
  Reply With Quote
Old 23rd August 2004, 05:14     #5
Noodles
Wireless Slacker
 
Wacked

no amount of if statements will do the same thing as any loop

they're completely different things
  Reply With Quote
Old 23rd August 2004, 11:15     #6
MadLep
 
Sounds like one of these comp sci "oh look how clever us lecturers are!" wank style assignments that have zero value for anything in the real world.

I can't think of a way off hand that an if statement could replace a while loop in every situation, but in specific cases you could rewrite some cases of logic done with a while loop to use a bunch of conditional tests instead.

More info on the specific problem pls?
__________________
@madlep
I'm not Australian, I just live there
ubercharged.net - Tales of Team Fortress 2 pwnage and other hilarity
  Reply With Quote
Old 23rd August 2004, 11:46     #7
Jodi
 
Twitch Haha.

I would just unroll the loop, and put heaps 'o' ifs in there. Still, a complete waste of time.

Without knowing the specifics of the question, noone will really be able to help you.
__________________
"I distinctly remember leaving my God at home
in my room where he won't interfere with my life."
-Quan Zee Teng
  Reply With Quote
Old 23rd August 2004, 12:10     #8
Farmer Joe
Word To Your Motherboard!
 
I don't agree that it's a complete waste of time. Sure you'd probably never use it in a program but it's exercises like these that get students thinking about what while and if statements actually do.
  Reply With Quote
Old 23rd August 2004, 12:15     #9
Putrid
 
It's probably trying to make you think outside the newb philosophies of looping and simple code flows.
So that you learn advanced techniques such as goto or setjmp/longjmp, which are required to create good code.
  Reply With Quote
Old 23rd August 2004, 12:31     #10
Baal
 
Quote:
Originally posted by Putrid
It's probably trying to make you think outside the newb philosophies of looping and simple code flows.
So that you learn advanced techniques such as goto or setjmp/longjmp, which are required to create good code.
Exactly, who needs all that sequence, selection and iteration bullshit anyway? Madness.
  Reply With Quote
Old 23rd August 2004, 12:48     #11
MadLep
 
o_O

Quote:
Originally posted by Putrid
So that you learn advanced techniques such as goto or setjmp/longjmp, which are required to create good code.
Just so long as I never have to debug this "good" code.
__________________
@madlep
I'm not Australian, I just live there
ubercharged.net - Tales of Team Fortress 2 pwnage and other hilarity
  Reply With Quote
Old 23rd August 2004, 13:01     #12
verve_rat
 
Quote:
Originally posted by Putrid
It's probably trying to make you think outside the newb philosophies of looping and simple code flows.
So that you learn advanced techniques such as goto or setjmp/longjmp, which are required to create good code.
__________________
Fuck you... I've been to the Moon!
  Reply With Quote
Old 23rd August 2004, 13:14     #13
Farmer Joe
Word To Your Motherboard!
 
Quote:
Originally posted by MadLep
Just so long as I never have to debug this "good" code.
My worst nightmare was debugging 130k of php code. The guy use if statements instead of ANY functions... if he learnt that from comp sci I take what I said back .
  Reply With Quote
Old 23rd August 2004, 13:16     #14
samael
 
For so many problems, the instinctual way do solve it via code is often not the best, or the most efficiant.

Kudos may have been set a problem whose obvious solution involves loops etc... (maybe the obvious solution is a brute force approach) but there is a better,faster way to achieve the goal. The requirement that his solution not use loops may just be a hint, or a way to force the student to think past teh obvious solution.

Of course, until we have the exact assignement in front of us, the above is just guesswork.

When i was at uni, we had a class which was all problem solving. We had teams of four, a pc each and whiteboards etc.... Often, if the problem allowed us to, we would get one of the team to brute force the answer, and the rest of the team would try to work it out properly, we'd have a race. All the problems given to us sounded stupid and annoying, but all turned out to be beneficial, and i learnt more about computing and problem solving in that class than i ever did in all my other classes.
  Reply With Quote
Old 23rd August 2004, 20:39     #15
Beccara
 
Quote:
Originally posted by samael
For so many problems, the instinctual way do solve it via code is often not the best, or the most efficiant.
OK now i dont have a comp.sci degree and its been a 18 hours working day so far but that just sounds wrong and i cant put my finger on why
  Reply With Quote
Old 23rd August 2004, 20:59     #16
samael
 
Quote:
Originally posted by Beccara
OK now i dont have a comp.sci degree and its been a 18 hours working day so far but that just sounds wrong and i cant put my finger on why
Heres an example :

A student, half way through second year at uni, is given a problem which involves working out all the prime numbers between 0 and 1000, and printing them to the screen.

The student says "a prime number is defined by only being divisble by itself, and one."

So the student sits down codes up a loop to iterate through the integers between 0 and 1000, dividing it by all the numbers lower than itself, and deciding weather its a prime numebr depending on the results. Then the student wonders why the hell its taking so long

This is but one example, there are many others which even seasoned professionals get wrong. The better solution seems simple to people who know a bit of math.
  Reply With Quote
Old 23rd August 2004, 22:03     #17
MadLep
 
OK, another example.

A coder is tasked with creating some back end processing job that needs to be created to do a decently complicated amount of work. The coder immediately spots a few places that could be optimised to create something that runs faster with some tricks he learnt in comp sci. So he codes it up, and the job runs in the background once a month taking 5 minutes. If he'd left the optimisations out, it might have taken 10 minutes. Great.

Coder leaves, 5 years go by. In the mean time a couple of hundred thousand lines of other processing has now been hacked around the original code and it's become a mission critical system. No one has a clue how it works, and no one dares touch it for fear of breaking it.

Personally, I'd have preferred the original system just take 10 minutes to run instead of 5 and be able to maintain it.
__________________
@madlep
I'm not Australian, I just live there
ubercharged.net - Tales of Team Fortress 2 pwnage and other hilarity
  Reply With Quote
Old 23rd August 2004, 22:24     #18
samael
 
Quote:
Originally posted by MadLep
OK, another example.

A coder is tasked with creating some back end processing job that needs to be created to do a decently complicated amount of work. The coder immediately spots a few places that could be optimised to create something that runs faster with some tricks he learnt in comp sci. So he codes it up, and the job runs in the background once a month taking 5 minutes. If he'd left the optimisations out, it might have taken 10 minutes. Great.

Coder leaves, 5 years go by. In the mean time a couple of hundred thousand lines of other processing has now been hacked around the original code and it's become a mission critical system. No one has a clue how it works, and no one dares touch it for fear of breaking it.

Personally, I'd have preferred the original system just take 10 minutes to run instead of 5 and be able to maintain it.
True, its a sliding scale. But anyone hacking a couple hundred thousand lines of code around anything is going to be a bitch to work out no matter wether the origanal code was optimised or not. I am not saying the coder should drop into assembly at the drop of a hat any time it may give a perfrormance boost.
Intelegent solutions are a lot easyer to understand than to come up with in the first place, when combined with good coding practice. Like well thought out variable names, and decent comments.
The problem with the coder in the above problem is that they may not have good coding practices even though they can imagine a good solution.

The example i gave doesnt rely on optimisations as such and the better solution would be a few orders of magnitude better, not just twice as fast. If the upper limit was 1,000,000, not 1000, i would hazard a guess that the niave solution could 100 times slower than its more intelligent brother. If thats not the case with the problem i posted, i can assure you their are problems that meet the criteria.

In fact, the niave solution may "not work" given the apropriette problem domain. What i mean is, if the code was to be run in some time critcal environment, say an abs brake system, coding the software niavely would mean some things just are not possible. Doing it the intelegent way, either by figuring out some better equation rather than a niave one, or using some sort of look up table of pre worked-out answers to a commonly used equation may permit the coder to do more, with less. Or do it at all.
  Reply With Quote
Old 25th August 2004, 20:22     #19
Kudos
 
Basically the assignment was to convert the user input of arabic numerals into the corresponding roman numerals and vice versa.
Obviously if i was allowed to use loops i can solve this without a fuss, which i cant.
I managed to do arabic to numerals by "hardcoding" through infinite ifs and elses.... pretty ugly code i must say was just thinking if there was a method of converting while loops as if statements.
  Reply With Quote
Old 25th August 2004, 20:24     #20
Kudos
 
But i'm extremely grateful for the replies guyz
Samael is right though.
I'd guess the lecturer is forcing the students to thinking in a non-conventional way.
Which i'm finding pretty hard doh.
  Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



© Copyright NZGames.com 1996-2024
Site paid for by members (love you guys)