Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Feb 21st, 2006, 9:34 AM   #31
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
breaking out of loops
Oddly enough, this is the general province of a "break" statement.
__________________
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 Feb 21st, 2006, 11:12 AM   #32
master007
Newbie
 
master007's Avatar
 
Join Date: Feb 2006
Posts: 8
Rep Power: 0 master007 is on a distinguished road
Using multiple goto statements would however add a routed break from the loop instead of restarting right from end of the loop statement.

example, the following psudocode:-

start loop
  EvaluationCode()
  if errortype=1 then goto error1
  if errortype=2 then goto error2
  if errortype=3 then goto error3
end loop

error1:
  statements
  goto finish

error2:
  statements
  goto finish

error3:
  statements
  
finish:
  end program
:banana:

I know the CASE statement can be used in the above code, I never said using GOTO is good coding or the best solution, in all situations I would agree that it would be best to code it in another way.
master007 is offline   Reply With Quote
Old Feb 21st, 2006, 11:15 AM   #33
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
And you can't figure out how to do that properly? Shame on you. Hit the books and make sure potential interviewers don't see your post.
__________________
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 Feb 21st, 2006, 11:19 AM   #34
master007
Newbie
 
master007's Avatar
 
Join Date: Feb 2006
Posts: 8
Rep Power: 0 master007 is on a distinguished road
wow you're fast. I was editing the post above, sorry. I posted it and then said to myself. hmmm Da wei is gonna shoot you down, you better state the obvious. sigh so said so done :p

Last edited by master007; Feb 21st, 2006 at 11:32 AM.
master007 is offline   Reply With Quote
Old Feb 21st, 2006, 12:07 PM   #35
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I apologize if the response seems a little strong. I have some concerns about novices being misdirected or instructed in gross error. It seems inappropriate to me (personal opinion, of course, but shared by a large majority of competent practitioners) that an example of bad programming practice would be deliberately provided.
__________________
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 Feb 21st, 2006, 12:09 PM   #36
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Quote:
Originally Posted by master007
example, the following psudocode:-
As DaWei rightly points out, there are better ways to get the same functionality. Refactoring your code to use the common "break" statement is simple enough:
while True:
    EvaluationCode()
    if errortype == 1:
        statements
        break
    if errortype == 2:
        statements
        break
    if errortype == 3:
        statements
        break
Although for code that seems to handle errors, exceptions would seem the best tool to use:
try:
    while True:
        EvaluationCode()
except Exception, errortype:
    if errortype == 1:
        statements
    elif errortype == 2:
        statements
    elif errortype == 3:
        statments
Arevos is offline   Reply With Quote
Old Feb 21st, 2006, 3:33 PM   #37
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Hmm, I read an article a while back which advocated the use of gotos in a lot of cases -- I can't say I agree with the article, but it was a well-written article nevertheless. I can't seem to find it though, if I find it I'll link it.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 21st, 2006, 3:52 PM   #38
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I feel compelled to add that there are televangelists that make well-presented arguments to seemingly rational people and are successful enough at it that they commute between mansions in Learjets.

I didn't exactly write a well-thought-out and immaculately presented treatise in post #29, but I think it hints at the very few situations where the use of a goto might be advocated. It's not a LOT of cases, by any stretch of the imagination. I would look askance at one who preaches otherwise, however eloquently.

One may make a case that in a language without exception handling, the equivalent functionality might be implemented using 'goto.' I reiterate that the task should be undertaken by one who is intimately familiar with what happens to system state before and after such machinations.
__________________
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 Feb 21st, 2006, 3:58 PM   #39
Ęthereal Haze
Newbie
 
Join Date: Feb 2006
Location: -=()N-=()=-C()=-
Posts: 7
Rep Power: 0 Ęthereal Haze is on a distinguished road
Send a message via AIM to Ęthereal Haze Send a message via MSN to Ęthereal Haze Send a message via Yahoo to Ęthereal Haze
ACK!
As I Said, . It All Depends On WHat You Use It For.
-End of Story- Gah. >.<;
Now If You Will Excuse Me I Need To Finish A Project In Vb6.
Ęthereal Haze is offline   Reply With Quote
Old Feb 21st, 2006, 4:02 PM   #40
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You keep popping things into the thread that indicate you STILL have no idea what we're talking about. "As I said..." indeed! Good of you to dictate the end of the story, though, we were waiting on an authoritarian figure to do that.

You're excused.
__________________
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
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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:03 PM.

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