![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 73
Rep Power: 4
![]() |
replace while-loop with if-statements
Is it possible to replace a while-loop by just using "if" statements?
I can't remember what was the problem to solve, but in one of my interviews last year, the interviewee asked "can you do that (the while loop) with just 'if' statements". I answered 'no', but apparently the answer was 'yes'. Forgot to ask her how though. I just remembered this scenario this morning (out of the blue), and i am curious how to do that. Is is possible? Thanks in advance. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
This strikes me as a homework exercise, so I won't say anything other than "yes, it is possible".
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2005
Posts: 73
Rep Power: 4
![]() |
oh yeah, "goto" eh...
it wasn't on top of my head since i was advised not to use goto since my first programming class... ah, should have thought of that! ![]() |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
On the subject of 'goto', I usually say that it is not evil, but is almost always a sign of a coder who hasn't mastered simple basics. Such a coder is usually headed for trouble in the form of deep doo doo.
I'm thinking of changing my position to this: it is evil, but occassionally we have to consort with the devil. Wear asbestos pants.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3
![]() |
well de he say you can't use recursion? if he didn't then you can use the if statement to test for an condition to stop recursion.
__________________
Quote:
|
|
|
|
|
|
|
#6 | |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3
![]() |
aaah the ol' goto's... remind me of my zx spectrum days
![]() |
|
|
|
|
|
#8 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3
![]() |
I think the reason why they are sort of frowned upon is that when reading code and you see a goto statement you have to look for that label, which is really sort of messy like”spaghetti". It would be much easier just to put that fragment of code into a function then call that function.
__________________
Quote:
|
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The 'goto' (jump, branch) is a staple of assembly language. Without branches and conditional constructs our computers just wouldn't work. Higher level languages (more abstraction) were invented to divorce us from having to deal with the Von Neumann architecture, and put us nearer the realm of straightforward human thought. Gotos generally destroy the comprehensible flow of a solution. The nature of the constructs available to us is that they provide the necessary gotos under the hood, in the guise of more obvious and rational transitions from one point to another. A person that hasn't seen the evils of a direct goto is not an experienced coder.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
By that definition (A person that hasn't seen the evils of a direct goto is not an experienced coder), DaWei, the majority of programmers today are not experienced coders. And Java programmers can never be experienced.:beard:
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Value of index incorrect after loop | aznluvsmc | C | 13 | Nov 6th, 2005 10:47 PM |
| Help in QBASIC (I think it's similar to VB) | phoenix987 | Visual Basic | 3 | May 9th, 2005 1:33 PM |
| Help with a QBASIC program | phoenix987 | Other Programming Languages | 4 | May 5th, 2005 1:27 PM |
| WinSock accept() hangs program in Do loop... | layer | C++ | 5 | Apr 29th, 2005 12:28 PM |
| Timing loop problems | badbasser98 | C++ | 11 | Mar 10th, 2005 9:30 PM |