Problem from C Programming goto/continue

Discussion in 'Mac Programming' started by cybrscot, Feb 28, 2011.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    My Book...

    12. Show how to replace a continue statement by an equivalent goto statement.

    Am I correct?

    Code:
     while (expr) {
    	   statement continue;
    	
    	
    	//continue jups to here, just before end of loop
    	
    	}
    	
    	
    	//replaced by equivalent goto statement
    	
    	
    	while (expr) {
    		statement goto done;
    		
    		
    		done: statement   //goto jumps to here, my labled statement
    		
    		}
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    You can think of continue has either jumping to the immediate end of a while loop or the very beginning. I tend to think it jumping to the beginning.

    So you are right, except there can't actually be any statements after the done label to be truly equivalent to a continue.

    Either of these are right:

    Code:
    while (expr) {
       /* ... */
       goto done;
       /* ... */
       done: ;  /* empty statement */
    }
    
    /* or */
    
    beginning:
    while (expr) {
       /* ... */
       goto beginning;
       /* ... */
    }
    
     
  3. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    Wow. While I know that's probably correct it makes my skin crawl remembering old QB45/BASIC7 code where someone thought it would be cute to use a GOTO inside a SUB to jump out of it and totally blew the call stack, but only sometimes.

    B
     
  4. holmesf, Feb 28, 2011
    Last edited: Feb 28, 2011

    holmesf macrumors 6502a

    Joined:
    Sep 30, 2001
    #4
    Looks right to me. This is sort of a dumb textbook problem, however, because using a continue statement is almost always favorable compared to using a goto statement. Using gotos can represent the removal of any structure to the control flow of a program and so most frown upon it. When I took C programming the professor would actually have you fail the assignment if you used a goto statement.

    http://en.wikipedia.org/wiki/Considered_harmful

    I guess the problem does have you demonstrate that you understand what 'continue' is actually doing, but this exchange of 'continue' in favor of 'goto' would never happen in practice.
     

Share This Page