GOTO command

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

GOTO command

 
0
  #1
Aug 7th, 2009
I am new to C programming and have been using the GOTO all over the place until today when I was informed it is not preferred practice, and due to its adverse affects on debugging and maintenance should only be used in the rarest of cases.

I can understand that I guess,.

Would an "acceptable" use of GOTO be to rerun the entire main loop? If I make it all the way through my logic, and simply want to start over at the top, would not a simple GOTO back to the top be the best option?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 58
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: GOTO command

 
1
  #2
Aug 7th, 2009
Try to avoid GOTO. Put main loop inside one more loop or try some other logic. Use of GOTO will create all the mess at last.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,410
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: GOTO command

 
2
  #3
Aug 7th, 2009
Originally Posted by no1zson View Post
Would an "acceptable" use of GOTO be to rerun the entire main loop?
No.
Originally Posted by no1zson View Post
If I make it all the way through my logic, and simply want to start over at the top, would not a simple GOTO back to the top be the best option?
Try to think about the real loop control and a better loop construct will come to mind.
---
http://en.wikipedia.org/wiki/Goto#Cr..._of_goto_usage
Last edited by Dave Sinkula; Aug 7th, 2009 at 1:34 am.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 36
Reputation: 9868 is on a distinguished road 
Solved Threads: 7
9868 9868 is offline Offline
Light Poster

Re: GOTO command

 
0
  #4
Aug 7th, 2009
goto makes the code unreadable and hard to debug. The only place where goto is advisable is coming out of a nested loop.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: GOTO command

 
0
  #5
Aug 7th, 2009
Originally Posted by Dave Sinkula View Post
No.
Try to think about the real loop control and a better loop construct will come to mind.
---
http://en.wikipedia.org/wiki/Goto#Cr..._of_goto_usage
This suggestion intrigues me. Putting my main loop inside another loop. I am not sure I understand how to do it, and it sounds almost more complicated than the problems it might potentially avoid, but if anyone can expand on this thought I would appreciate it.
Again, my limited experience hinders my understanding at this point.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: GOTO command

 
0
  #6
Aug 7th, 2009
Originally Posted by no1zson View Post
This suggestion intrigues me. Putting my main loop inside another loop. I am not sure I understand how to do it, and it sounds almost more complicated than the problems it might potentially avoid, but if anyone can expand on this thought I would appreciate it.
Again, my limited experience hinders my understanding at this point.
Perhaps if you provide a code snippet on where you think the goto should be used, you may get responses that satisfy your needs.

I'm not totally against the goto, but I haven't used one - well, I can't remember when. Up to about 5 years ago, a language I use on the System i (aka AS400) known as CL had very few good looping constructs (actually none) and your only choice was to use a goto.

Most languages these days (decent ones) provide adequate constructs with regards to sequence, iterations and decisions that would really make you think long and hard as to why you would need to use a goto.

9868 makes a valid observation regarding breaking out of a nested loop, but it would have to be a really deeply nested loop and if that's the case, then it's more than likely a design issue. However, "modern" languages usually provide a "psuedo" goto (e.g. Java with break and a label) for these situations.

Cheers,
JD
Last edited by yellowSnow; Aug 7th, 2009 at 2:10 pm.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,410
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: GOTO command

 
0
  #7
Aug 7th, 2009
Originally Posted by no1zson View Post
This suggestion intrigues me. Putting my main loop inside another loop. I am not sure I understand how to do it, and it sounds almost more complicated than the problems it might potentially avoid, but if anyone can expand on this thought I would appreciate it.
Again, my limited experience hinders my understanding at this point.
In this post, what is it that breaks the loop -- entering an 'n'? How easy is it to spot loop conditions? Might it be easier to tell if it were something like this?
  1. do {
  2. /* show menu */
  3. /* take user input */
  4. /* show result or error */
  5. /* prompt to repeat */
  6. } while ( toupper(cAgain) != 'N' );
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: GOTO command

 
0
  #8
Aug 7th, 2009
Here is the type of program I am talking about. It is not a nested loop or anything like that, but once I get to the end, if they user enters Y I would like to send them back to the top and start all over.
I am still not even sure how I would do this with the loop-inside-loop suggestion.
  1.  
  2. #include <stdio.h> // standard input output library
  3.  
  4. //defines tax value for calculations
  5. #define DelMar 7.25
  6. #define Encinitas 7.5
  7. #define LaJolla 7.75
  8.  
  9. char cAgain; //variable for Y/N questions
  10.  
  11. float user_input() // week 4 addition for sales amount
  12. { //prompt, collect, and store sale amount
  13. float fAmount; //defines amount type as float
  14. printf("Enter Amount of Sale $ ");
  15. scanf ("%f", &fAmount);
  16. return fAmount;
  17. }
  18.  
  19. int iMenu() //defines menu variable
  20. { // menu which prompts, collects, and stores store number
  21. int iSelection; //defines selection type as integer
  22. printf("Tax Calculation for Purchase\n\n"); //changed to reflect week 4 updates
  23. printf("1. Del Mar \n2. Encinitas \n3. La Jolla\n");
  24. printf("\n\nPlease Select Number of Store for Which to Calculate: ");
  25.  
  26. scanf("%d",&iSelection);
  27. return iSelection;
  28. }
  29.  
  30. int main() //main loop
  31. {
  32. STARTOVER://start again point
  33. {
  34.  
  35. float fSales = 0.00; // Sales value for calculations
  36. int iStorenum; //defines store for main loop, menu will not work without
  37.  
  38. fSales = user_input(); //new week 4 loop for sales total
  39. while (fSales <0.0) //new week 4 loop to validate sales amount
  40. {
  41. printf("SALE AMOUNT CANNOT BE LESS THAN $0.00. Re-Enter: $ ");
  42. {
  43. scanf ("%f",&fSales); // scans new input for validity
  44. }
  45.  
  46. } // ends while loop
  47.  
  48. iStorenum = iMenu(); //displays menu created above
  49.  
  50. while (iStorenum <1 || iStorenum >3) //if store number is not 1, 2 or 3
  51. {
  52. printf("INVALID NUMBER ENTERED! Please enter 1,2 or 3: ");
  53. {
  54. scanf("%d", &iStorenum); //scans input at INVALID prompt
  55. }
  56. } // end while loop
  57.  
  58. if (iStorenum == 1)
  59. //Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
  60. printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, DelMar, fSales*DelMar/100, DelMar*fSales/100+fSales);
  61. if (iStorenum == 2)
  62. //Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
  63. printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, Encinitas, fSales*Encinitas/100, Encinitas*fSales/100+fSales);
  64. if (iStorenum == 3)
  65. //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
  66. printf("\nLa Jolla \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, LaJolla, fSales*LaJolla/100, LaJolla*fSales/100+fSales);
  67.  
  68.  
  69.  
  70.  
  71. printf("\n\tWould you like to calculate for another store? (y/n) ");//prompts for repeat
  72. scanf("%c", &cAgain);//scans input at store number prompt
  73.  
  74. if (cAgain == 'y'|| cAgain == 'Y')//repeats calculator
  75. {
  76. goto STARTOVER; //back to top
  77. }
  78. else if (cAgain == 'n' || cAgain == 'N')//end
  79.  
  80. return 0; // successful loop end
  81. }
  82. }
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: GOTO command

 
0
  #9
Aug 8th, 2009
You would use a do-while loop. Essentially you'll keep asking for user input in the body of the do loop while the user keeps answering 'y' or 'Y' for more calculations.
  1. int main() //main loop
  2. {
  3.  
  4. do {
  5.  
  6. float fSales = 0.00; // Sales value for calculations
  7. int iStorenum; //defines store for main loop, menu will not work without
  8.  
  9. fSales = user_input(); //new week 4 loop for sales total
  10. while (fSales < 0.0) //new week 4 loop to validate sales amount
  11. {
  12. printf("SALE AMOUNT CANNOT BE LESS THAN $0.00. Re-Enter: $ ");
  13. {
  14. scanf("%f", &fSales); // scans new input for validity
  15. }
  16.  
  17. } // ends while loop
  18.  
  19. iStorenum = iMenu(); //displays menu created above
  20.  
  21. while (iStorenum < 1 || iStorenum > 3) //if store number is not 1, 2 or 3
  22. {
  23. printf("INVALID NUMBER ENTERED! Please enter 1,2 or 3: ");
  24. {
  25. scanf("%d", &iStorenum); //scans input at INVALID prompt
  26. }
  27. } // end while loop
  28.  
  29. if (iStorenum == 1)
  30. //Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
  31. printf(
  32. "\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",
  33. fSales, DelMar, fSales * DelMar / 100, DelMar * fSales
  34. / 100 + fSales);
  35. if (iStorenum == 2)
  36. //Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
  37. printf(
  38. "\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",
  39. fSales, Encinitas, fSales * Encinitas / 100, Encinitas
  40. * fSales / 100 + fSales);
  41. if (iStorenum == 3)
  42. //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
  43. printf(
  44. "\nLa Jolla \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",
  45. fSales, LaJolla, fSales * LaJolla / 100, LaJolla * fSales
  46. / 100 + fSales);
  47.  
  48. printf("\n\tWould you like to calculate for another store? (y/n) ");//prompts for repeat
  49. scanf("%c", &cAgain);//scans input at store number prompt
  50.  
  51. } while (cAgain == 'y' || cAgain == 'Y');
  52.  
  53. return 0;
  54. }
By the way, your code doesn't work quite right at the moment as it only prompts for one set of input data. When prompting for the answer to more calculations, the scanf will pick up the '\n' character from the previous scanf of the store number. You'll have to "consume" that character from the input buffer before prompting for character input.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: GOTO command

 
0
  #10
Aug 9th, 2009
Thanks Yellow Snow, I am going to give that a try.
Can you help me on something stupid real quick? I have two copies of my code saved, one works and one does not (as you pointed out). For the life of me I cannot see the difference.
Can you look as this working version and tell me what is different?
I have gone through line by line and both versions look identical to me.
  1. #include <stdio.h> // standard input output library
  2.  
  3. //defines tax value for calculations
  4. #define DelMar 7.25
  5. #define Encinitas 7.5
  6. #define LaJolla 7.75
  7.  
  8. char cAgain; //variable for Y/N questions
  9.  
  10. float user_input() // week 4 addition for sales amount
  11. { //prompt, collect, and store sale amount
  12. float fAmount; //defines amount type as float
  13. printf("Enter Amount of Sale $ ");
  14. scanf ("%f", &fAmount);
  15. return fAmount;
  16. }
  17.  
  18. int iMenu() //defines menu variable
  19. { // menu which prompts, collects, and stores store number
  20. int iSelection; //defines selection type as integer
  21. printf("Tax Calculation for Purchase\n\n"); //changed to reflect week 4 updates
  22. printf("1. Del Mar \n2. Encinitas \n3. La Jolla\n");
  23. printf("\n\nPlease Select Number of Store for Which to Calculate: ");
  24.  
  25. scanf("%d",&iSelection);
  26. return iSelection;
  27. }
  28.  
  29. int main() //main loop
  30. {
  31. STARTOVER://start again point
  32. {
  33.  
  34. float fSales = 0.00; // Sales value for calculations
  35. int iStorenum; //defines store for main loop, menu will not work without
  36.  
  37. fSales = user_input(); //new week 4 loop for sales total
  38. while (fSales <0.0) //new week 4 loop to validate sales amount
  39. {
  40. printf("SALE AMOUNT CANNOT BE LESS THAN $0.00. Re-Enter: $ ");
  41. {
  42. scanf ("%f",&fSales); // scans new input for validity
  43. }
  44.  
  45. } // ends while loop
  46.  
  47. iStorenum = iMenu(); //displays menu created above
  48.  
  49. while (iStorenum <1 || iStorenum >3) //if store number is not 1, 2 or 3
  50. {
  51. printf("INVALID NUMBER ENTERED! Please enter 1,2 or 3: ");
  52. {
  53. scanf("%d", &iStorenum); //scans input at INVALID prompt
  54. }
  55. } // end while loop
  56.  
  57. if (iStorenum == 1)
  58. //Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
  59. printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, DelMar, fSales*DelMar/100, DelMar*fSales/100+fSales);
  60. if (iStorenum == 2)
  61. //Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
  62. printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, Encinitas, fSales*Encinitas/100, Encinitas*fSales/100+fSales);
  63. if (iStorenum == 3)
  64. //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
  65. printf("\nLa Jolla \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, LaJolla, fSales*LaJolla/100, LaJolla*fSales/100+fSales);
  66.  
  67.  
  68.  
  69.  
  70. printf("\n\tRun Calculator Again? (y/n) ");//prompts for repeat
  71. scanf("%c", &cAgain);//scans input at store number prompt
  72.  
  73. if (cAgain == 'y'|| cAgain == 'Y')//repeats calculator
  74. {
  75. goto STARTOVER; //back to top
  76. }
  77. else if (cAgain == 'n' || cAgain == 'N')//end
  78.  
  79. return 0; // successful loop end
  80. }
  81. }
Last edited by no1zson; Aug 9th, 2009 at 12:53 am.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC