|
22nd August 2004, 19:45 | #1 |
|
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. |
22nd August 2004, 19:50 | #2 |
|
That doesn't make any sense to me - what's the point in teaching you to not use a fundamental programming er, thing...?
|
22nd August 2004, 20:10 | #3 |
|
You could emulate the behaviour of a while by using a goto or similar. But it really defeats the purpose.
Yeeech. |
22nd August 2004, 20:20 | #4 |
|
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.
|
23rd August 2004, 05:14 | #5 |
Wireless Slacker
|
no amount of if statements will do the same thing as any loop
they're completely different things |
23rd August 2004, 11:15 | #6 |
|
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 |
23rd August 2004, 11:46 | #7 |
|
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 |
23rd August 2004, 12:10 | #8 |
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.
|
23rd August 2004, 12:15 | #9 |
|
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. |
23rd August 2004, 12:31 | #10 | |
|
Quote:
|
|
23rd August 2004, 12:48 | #11 | |
|
Quote:
__________________
@madlep I'm not Australian, I just live there ubercharged.net - Tales of Team Fortress 2 pwnage and other hilarity |
|
23rd August 2004, 13:01 | #12 | |
|
Quote:
__________________
Fuck you... I've been to the Moon! |
|
23rd August 2004, 13:14 | #13 | |
Word To Your Motherboard!
|
Quote:
|
|
23rd August 2004, 13:16 | #14 |
|
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. |
23rd August 2004, 20:39 | #15 | |
|
Quote:
|
|
23rd August 2004, 20:59 | #16 | |
|
Quote:
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. |
|
23rd August 2004, 22:03 | #17 |
|
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 |
23rd August 2004, 22:24 | #18 | |
|
Quote:
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. |
|
25th August 2004, 20:22 | #19 |
|
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. |
25th August 2004, 20:24 | #20 |
|
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. |