Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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
		
		}
 
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;
   /* ... */
}
 
Code:
beginning:
while (expr) {
   /* ... */
   goto beginning;
   /* ... */
}

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
 
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
		
		}

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.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.