Tuesday, November 15, 2011

The goto statement in c++

The goto statement in c++

We have gone through the while loop, for loop and do-while loop. The another way to do loop in c++ is through using the goto statement. The goto statement was used in olden days but it is not suitable for creating modern applications. But since c++ supports it, you should have knowledge about it, as you may encounter a c++ source code containing goto statements, in that case you will know what it is and how it works.

How the goto statement works?

It consist of label and statements that comes under that label. A label is named by you and it is followed by a colon sign (:). During execution of program, when goto is encountered, it jumps to the statements that comes under the label specified by goto statement. To get a clear idea, analyze the below program example.

/* A c++ program example that uses goto statement to display number from 0 to 9 */

#include <iostream>

using namespace std;

int main ()
    int i=0;

    cout << i << endl;

    if (i<10)
        goto loop;

    return 0;

Why goto should not be used?

The use of goto should be avoided to make the program more readable and reliable. The goto statement can cause the program execution to jump to any location in source code and in any direction backward or forward. This makes the program hard to read and understand and also makes it difficult to find bugs.
Since now we have more tightly controlled and sophisticated loops like while loop, for loop and do-while loop, the use of obsolete statement like goto is not at all recommended in creating loops.

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters


  1. create a program that will compute for employees daily salary using military time concept requirements rate=70.00 per hour ..to be display tax=0.5 ..grosspay : netpay:
    help me plssssss

  2. create a program that will compute for employees daily salary using military time concept requirements rate=70.00 per hour ..to be display tax=0.5 ..grosspay : netpay:
    help me plssssss

  3. And I m waiting for your next chapters
    It should be
    USER DEFINED DATA TYPES (struct,enum,union,class)
    OOPs Thory
    Using namespace statement
    String classes
    Standard C++ Library
    CGI(Common Gateway Interface) Programming

    1. http://clearnerz.blogspot.com/p/c-learnerz.html

  4. Great,.. Just add me guys :) crazymuffin0789@yahoo.com, i am biggner to c++, i love to program :) thanks for the info mr.homan

  5. very useful post for C++ beginners
    i am a beginner of C++

  6. Please keep on posting such quality articles as this is a rare thing to find these days. I am always searching online for articles that can help me. Looking forward to another great blog. Good luck to the author! all the best!
    Celabright information

  7. hallo sir i m hassan munir from pakistan and i m doing bscs in comsats university and i m little week in programming my problem is that i cant make the algorithm in programming plz tell me how i can improve the logic in programming beacuse i m the bigner in programming this is my first samester

    1. Go through the books/blogs related programming basics. Do your basics well. Practice as much as you can. Don't just read the code, manually type it, compile it and run it and try to understand code by looking at the code.
      Don't make haste, while you learn basics. Take your time, and practice well.
      When i say do your basics well, i mean do "Control Statement" well. Control statement include : loops (for, while, do-while), if-else, switch-case, break, continue.
      These are fundamentals of programming. And you will find these in all programming languages, with minor syntax differences. So if you understand these concept in one, you can get them in all.

  8. The Goto Statement is not advised in modern programming languages. But, It is a good article explaining the subject

  9. http://clearnerz.blogspot.com/p/c-learnerz.html

  10. I'm sure that this post is useful for all young learners learning programming languages from scratch! Just have in mind that the best online writing services are 24/7 to help you with the ideas for learning!Good luck!

  11. Read online Urdu Digests,Urdu Books,Novels,Magazines,Safarnama,Islamic Books,http://bookspoint.net/