Important alert: (current site time 5/25/2013 3:12:04 PM EDT)
 

VB icon

Goto, Continue, Break and Exit

Email
Submitted on: 9/14/2012 11:04:57 AM
By: Sorrow_Dog 
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general)
Views: 1372
(About the author)
 
     Actually, I'm a beginner to C++. By the moment I'm reading "The C++ Language Tutorial" (www.cplusplus.com) and I'm writting some short sources to understand the lessons. This one is a simple console source about flow control using labels, GOTO, CONTINUE, BREAK and EXIT. As you probably know GOTO is not recommend to use, but it's part of C++. Change the system call "clear" to "cls" when running on windows.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Goto, Continue, Break and Exit
// Description:Actually, I'm a beginner to C++.
By the moment I'm reading "The C++ Language Tutorial" (www.cplusplus.com) and I'm writting some short sources to understand the lessons.
This one is a simple console source about flow control using labels, GOTO, CONTINUE, BREAK and EXIT. As you probably know GOTO is not recommend to use, but it's part of C++.
Change the system call "clear" to "cls" when running on windows.
// By: Sorrow_Dog
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=13846&lngWId=3//for details.//**************************************

// GOTO, BREAK, CONTINUE and EXIT use sample.
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
int main()
{
 string option;
 int opt = 0, c;
 system("clear");
 // Init Label
 init:
 if (opt == 1)
 {
 system("clear");
 cout << "You have jumped to the beginning with a GOTO.\n";
 cout << "if (opc == 1) goto inicio;\n" << endl;
 }
 cout << "- GOTO order allows to jump to a label." << endl;
 cout << "- CONTINUE order allows to avoid a iteration in a loop." << endl;
 cout << "- BREAK order skips a loop o code block." << endl;
 cout << "- EXIT order stops program execution." << endl << endl;
 cout << "Enter 1 ... To GOTO sample." << endl;
 cout << "Enter 2 ... To CONTINUE sample." << endl;
 cout << "Enter 3 ... To BREAK sample." << endl;
 cout << "Enter 4 ... To EXIT sample." << endl;
 cout << "Option ?:";
 getline(cin, option);
 stringstream(option) >> opt;
 // Selection code using GOTO
 if (opt == 1) goto init;
 if (opt == 2) goto use_continue;
 if (opt == 3) goto use_break;
 if (opt == 4) goto use_exit;
 if ((opt < 0)||(opt > 3)) goto init;
 // CONTINUE label
 use_continue:
 system("clear");
 cout << "We are going to use CONTINUE to avoid 5 to be printed." << endl;
 cout << "for (c=1; c<11; c++)" << endl;
 cout << "{" << endl;
 cout << " cout << c << ',';" << endl;
 cout << " if (c == 5) continue;" << endl;
 cout << "}" << endl;
 
 for (c=1; c<11; c++)
 {
 if (c == 5) continue;
 cout << c << ',';
 }
 cout << endl << endl;
 goto init; // Jump to the beginning
 // BREAK label
 use_break:
 system("clear");
 cout << "We are going to use BREAK to stop a loop." << endl;
 cout << "for (c=1; c<11; c++)" << endl;
 cout << "{" << endl;
 cout << " cout << c << ',';" << endl;
 cout << " if (c == 5) break;" << endl;
 cout << "}" << endl;
 
 for (c=1; c<11; c++)
 {
 cout << c << ',';
 if (c == 5) break;
 }
 cout << endl << endl;
 goto init; // Jump to the beginning
 // EXIT label
 use_exit:
 system("clear");
 cout << "We are stopping the execution with EXIT." << endl;
 cout << "exit(0);" << endl;
 exit(0);
 return(0);
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.