Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 14th, 2007, 10:31 AM   #1
kurt
Programmer
 
Join Date: Oct 2005
Posts: 73
Rep Power: 4 kurt is on a distinguished road
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.
kurt is offline   Reply With Quote
Old Apr 14th, 2007, 11:02 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
This strikes me as a homework exercise, so I won't say anything other than "yes, it is possible".
grumpy is offline   Reply With Quote
Old Apr 14th, 2007, 11:04 AM   #3
kurt
Programmer
 
Join Date: Oct 2005
Posts: 73
Rep Power: 4 kurt is on a distinguished road
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!
kurt is offline   Reply With Quote
Old Apr 14th, 2007, 11:38 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 14th, 2007, 2:09 PM   #5
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Apr 14th, 2007, 5:47 PM   #6
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
Quote:
Originally Posted by DaWei View Post
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.
What is the problem with Goto statements? I've dabbled in a little assembly code, where gotos are your bread and butter. Is it just considered terrible form to use them in higher languages?
Twilight is offline   Reply With Quote
Old Apr 14th, 2007, 5:52 PM   #7
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3 Seif is on a distinguished road
aaah the ol' goto's... remind me of my zx spectrum days
Seif is offline   Reply With Quote
Old Apr 14th, 2007, 5:52 PM   #8
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Apr 14th, 2007, 6:06 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 14th, 2007, 7:36 PM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
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:
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC