Kamis, 12 Januari 2012

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

#include

using namespace std;


int main ()
{
int i=0;


loop:
cout << i << endl;
i++;


    if (i<10)
goto loop;


    return 0;
}


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.



 

Related Posts:

  • 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 appl… Read More
  • Repetition Statement: The Do-While Loop in c++ We have already gone through The While Loop and The For Loop. Now its time for The Do-While Loop.// Program 1#include using namespace std;int main (){int i = 0;do{cout i endl;i++;} while (i 10);return 0;}The do-while loo… Read More
  • Repetition Statement: The For Loop in c++ To understand this you must first go through The While loop in c++. The below program, Program 1 does the same thing that the Program 1 of previous chapter The While Loop does. An integer i is declared and then three express… Read More
  • Selection Statement (if-else if-else) in c++ To understand this chapter you should know about Comparison Operators which is explained in previous chapter. Selection statements are very important in programming because we make decisions using it. // Program 1#include #i… Read More
  • Repetition Statement: The While Loop in c++ The fundamental to programming are control statements. You must learn to have good command over control statement, if you want to program. Sequence, selection and repetition are three types of control statement. It specifies… Read More

0 komentar:

Posting Komentar