goto

This is a discussion on goto within the C Programming forums, part of the General Programming Boards category; Code: void Function() { if (!function1) goto cleanup; if (!function2) goto cleanup; if (!function3) goto cleanup; if (cond) { if ...

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void Function() {
    	if (!function1) goto cleanup;
    	if (!function2) goto cleanup;
    	if (!function3) goto cleanup;
    	if (cond) {
    		if (!function4) goto cleanup;
    Here's an amusing modification to Hammer's original idea:
    Code:
    void Function( void )
    {
        	fun1(1)?fun2(1)?fun3(1)?condition&&!fun4(0)?cleanup():1:cleanup():cleanup():cleanup();
    }
    My test code had the functions take a paremeter, which they simply returned, for easy testing. Feel free to omit the arguments to the functions, the end result will be the same.

    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    i e-mailed the gooking, the c-for-dummies guy, here is what he sent me:
    Originally my inbox
    I'm still looking, but I did just find a reference where a programmer
    used goto in several spots to avoid calling a subroutine in the code.
    Basically goto was used in several "case" statements to have
    execution fall through to right after the cast statement -- kind of a
    way of building in multiple "default" statements.

    case 'd':
    base = 10;
    goto pnum;
    case 'x':
    base = 16;
    goto pnum;
    case 'b':
    base = 2;
    goto pnum;
    case 'o':
    base = 8;

    pnum:

    /* program continues */

    I'm not sure if this is what I was referring to (I don't think it
    is), so I'll keep looking.

    DAN

    >You said in volume two this
    >----
    > Most C language books don't teach anything about the goto, other
    >than a passing mention. One programmer, however, set out to find
    >virtue for the vilified goto. He conccted a program that used a goto
    >and could not be written any other way. Keep in mind that this guy
    >was a top-notch C programmer and he had to work at it to get a
    >justifiable goto program. For us mere mortals, this isn't an option.
    >----
    >At cprogramming.com we were wonder, could you send us the source to
    >the program in mention? Thanks a lot.
    >
    >_______________________________________________
    >Eliminate pop-ups before they appear!
    >Visit www.PopSwatter.com now - It's FREE.


    --
    Cheers!
    DAN

  3. #18
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,854
    What about

    'continue'

    Is it goto's little brother?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by chrismiceli
    Basically goto was used in several "case" statements to have
    execution fall through to right after the cast statement -- kind of a
    way of building in multiple "default" statements.
    Um yeah. See that's what 'break' is for. To get out of a case statement.

    But as they always say, it's easy to avoid goto:
    Code:
    while(!done)
    {
        switch( state )
        {
            case 1: /*stuff*/ state = -1; break;
            case 2: /*stuff*/ state = -1; break;
            ...
            default: /*stuff*/ done=1;
        }
    }
    Or even simpler:
    Code:
    switch( state )
    {
            case 1: /*stuff*/ state = -1; break;
            case 2: /*stuff*/ state = -1; break;
            ...
    }
    if( state < low || state > high )
    {
        dodefaultstuff();
    }
    Where 'low' is the lowest valid state, and 'high' is the highest valid state. Pretty simple. No goto needed. See how much more cleaner the code would be without the gotos in it if you just add an if check?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    edit: wait. I see what the deal is. pnum exists inside case 'o'. This means that no additional compares are necessary to get there which is essentially the goal.
    Last edited by FillYourBrain; 08-26-2003 at 07:19 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #21
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    here we go:
    Code:
    case 'x':
         base += 6;
    case 'd':
         base += 2;
    case 'o':
         base += 6;
    case 'b':
         base += 2;
         //pnum:
    no gotos needed. no additional compares needed. I win. Send that to your author chrismiceli
    Last edited by FillYourBrain; 08-26-2003 at 07:42 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 11:32 AM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 09:40 AM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21