answersLogoWhite

0

Why we avoid goto statement in c?

Updated: 8/11/2023
User Avatar

Wiki User

12y ago

Best Answer

Edsger Dijkstra said, excessive usage and unstructured usage of goto may:

1. lead to a very bad/un-readable/un-structrured/complicated code

2. increase the complexity of debugging and analyzing the code

.

But there are some hidden-gotos that we use in C language

1. break

2. continue

3. return

.

Rules of thumb:

It is not very bad to use goto in any language if we follow the following rules

1. Always use gotos to jump to a forward location. Gotos are very useful in cleanup activities

2. Backward jump can always be accomplished by using continue inside a loop. This doesn't mean that we shouldn't use backward gotos. Backward gotos can be introduced when the code is very legible and it is not disrupting the structure and readability of the code and there are only a few backward gotos.

3. Use do { .... if(xx) break; ... } while(0); - type of coding will suite to most of the places where we need to use goto.

4. Usage of goto to jump into another code-block/scope is always bad. Instead strucure the code in such a way that goto-lables are placed at the start of the code-block/scope.

5. The primary-advantage of using goto when compared to its alternative ways is, goto is fast.

6. Avoid as much gotos in your code and use goto only when you feel that usage of goto's alternative ways will slowdown the execution or they increase the complexity or decrease the readability.

7. Make sure that usage of goto/break/continue/return is not creating any unreachable code

8. Keep in mind that usage of goto is near to punishable offence w.r.t some organization's/intuition's coding standards.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

goto tends to get messy if you have to use more then one of them. It's very hard to upgrade as well as understand when goto is used in code. Some time ago it was a big discussion about it. Many professional programmers have agreed that goto caused a lot of problems. Nowadays goto is almost not used in professional projects.

another view:
You shouldn't "avoid it" so much as know when to use it and when to use another way. Used improperly it makes code unreadable and hard to maintain, which is why beginners are taught not to use it (beginners would use it improperly and never learn to use loops :p ). Example of the proper usage would be exiting multiple levels of nested loops.
while(...){
while(...){

while(...){

if (something) goto end;


end:
//rest of the code

But it can still be avoided. In this example, by using a flag.
flag=1;
while(...&&flag){
while(...&&flag){

while(...&&flag){

if (something) flag=0;

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

you should avoid goto as much as you can.use only when using in large nested programs. otherwise use of goto makes the program unreliable,unreadable,and hard to debug.

one more big problem with goto is that when we do use them we can never be sure how we got to certain point in our code.they obscure the flow of control. so avoid them.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The goto statement is not recommended since it can lead to rapid destructuring of code, making it harder to maintain.

Frequent use of the goto statement usually reflects poor programming practice, since the same results can be achieved by using control statements like the if-else block or loops such as for and while.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

it obsecures flow of programme .as in ,once we use goto to jump to a label, all previous references to dynamically allocated memory is lost.

Plus: Some people's religion is against goto.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

There is no recommendation that states we must not use goto in C. There'd be no point in having it otherwise. The C language was specifically designed to map as closely as possible to the instruction set of the underlying hardware. The goto statement maps directly to the JMP assembly instruction and when used in conjunction with an if statement, goto maps to JNZ. So when writing low-level code, goto statements are nothing if not essential.

However, C is also a structured language that allows us to write code in a more abstract way. Instead of using goto we can use function calls which automatically return to the caller. A function call generates far more assembly instructions than a simple goto ever would (including at least two JMP instructions), however not all functions generate function call instructions in the assembly. Many functions can be inline expanded by the compiler, thus the overhead of actually making a function call is completely negated. The only reason for having the function at all is simply to make our code easier to read and thus easier to maintain. After all, if a function is invoked many times throughout our code, it makes far more sense to keep that same code in just one place (where its easy to change if we need to) and let the compiler handle the code duplication via inline expansion.

The difference between a function call and a goto is that a goto does not remember where the call actually came from and therefore cannot return to the point of that call, whereas a function call will automatically pick up from where it left off. A function call also has its own local scope whereas a goto is scoped to the function in which it was called.

However, there are still some practical cases where a goto might be preferred to a function call. Consider the following:

while (true) {

// ...

while (true) {

// ...

if (foo()) break;

// ...

}

// ...

}

// ...

This is just a technical example rather than a practical one. Here we have a nested loop where the inner and outer loops are both infinite. The idea is that when the foo() function returns true we want to break out of both loops. However, the break statement only breaks out of the nearest enclosing scope, so we can never actually get out of the outer loop. But we can with a goto:

while (true) { while (true) {

// ...

if (foo()) goto my_break;

// ...

}

}

my_break:

// ...

There are other ways of achieving the same thing of course. For instance, we could place the inner loop in a separate function:

bool inner_loop () {

while (true) {

// ...

if (foo()) break;

// ...

}

return true;

}

Now our outer loop becomes:

while (true) {

// ...

if (inner_loop()) break;

// ...

}

// ...

Whether this version is any more readable or maintainable than the goto version is more a matter of personal taste than anything else; a good compiler should generate the same assembly for either version. But if we believe that goto breaks the fundamental rules of structured programming, then surely the same should apply to the break, return and continue statements. After all, they all interrupt the normal flow of execution and are just as open to abuse as the goto statement.

If a function has more than one point of return, can it really be said to be structured? Of course it can't; a well-structured function should only have one point of return at the end of the function. And yet no-on ever complains when a function has many return points, just as no-one ever complains when a loop has multiple break points and/or continue points. It may not be recommended in the interest of structure, but we all use them. The trick is to use them when it is appropriate to do so. If code becomes less readable or more difficult to maintain, then it is clearly inappropriate.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

It makes your program hard to read and follow logic. Thus, it doesn't worth spending time to upgrade it. If you cannot upgrade your program nobody will buy it.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

1. It's easy to write unreadable code with it.

2. Some people's religion doesn't allow it.

3. There are languages that do not have goto.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

GOTO statements makes confusion in understanding the program.it makes the complexity of the program is high.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why we avoid goto statement in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a c for goto statement?

AGAIN: puts ("c"); goto AGAIN;


Which statement is not frequently used in C plus plus?

The goto statement.


Explain goto and return in c?

A return statement is used to transfer the program control flow to the function that has called the current function under execution. If the function is main, then the program returns the control flow to the operating system. The syntax for return statement is:return return-type;A goto statement is used to transfer the control flow to a particular labelled statement, not necessarily back to the calling program. There are somerestrictionson using a goto statement. For eg: the goto statement should not skip any variable declarations. The use of goto statement is usually considered as a bad programming practice. The syntax for goto statement is:goto label_name;....label_name: statements;


Is there any other keyword like goto in c plus plus?

The goto statement is a control flow statement that causes the CPU to jump to another spot in the code. This spot is identified through use of a statement label. The following is an example of a goto statement and statement label:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> #include <cmath> int main() { using namespace std; tryAgain: // this is a statement label cout << "Enter a non-negative number"; double dX; cin >> dX; if (dX < 0.0) goto tryAgain; // this is the goto statement cout << "The sqrt of " << dX << " is " << sqrt(dX) << endl; }


What is null statement?

in C: a semicolon in itself. Examples:1. while (*to++ = *from++);2. { goto END; ... END:; }

Related questions

Write a c for goto statement?

AGAIN: puts ("c"); goto AGAIN;


Which statement is not frequently used in C plus plus?

The goto statement.


What is looping statement in turbo c?

while, for, do-while (and perhaps goto)


Explain goto and return in c?

A return statement is used to transfer the program control flow to the function that has called the current function under execution. If the function is main, then the program returns the control flow to the operating system. The syntax for return statement is:return return-type;A goto statement is used to transfer the control flow to a particular labelled statement, not necessarily back to the calling program. There are somerestrictionson using a goto statement. For eg: the goto statement should not skip any variable declarations. The use of goto statement is usually considered as a bad programming practice. The syntax for goto statement is:goto label_name;....label_name: statements;


Which statement should normally be avoided in c programme?

The word your teacher wants to hear is 'goto'.


Is there any other keyword like goto in c plus plus?

The goto statement is a control flow statement that causes the CPU to jump to another spot in the code. This spot is identified through use of a statement label. The following is an example of a goto statement and statement label:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> #include <cmath> int main() { using namespace std; tryAgain: // this is a statement label cout << "Enter a non-negative number"; double dX; cin >> dX; if (dX < 0.0) goto tryAgain; // this is the goto statement cout << "The sqrt of " << dX << " is " << sqrt(dX) << endl; }


What is null statement?

in C: a semicolon in itself. Examples:1. while (*to++ = *from++);2. { goto END; ... END:; }


Why goto in c is a bad command?

A 'goto' statement immediately moves the execution of code to another part of the program. This makes the code difficult to follow and to debug. It is better practice to use If-then-else constructs to structure the program code.


How many arithmetic statement in c?

Only one: expression. Yes, in C expression is one of the statements. Some other statements are: if, do, goto, while, for, switch, break, continue, return, NULL-statement, compound-statement.


Difference between goto and continue statement in c language?

In continue statement, we immediately continue next step through loop In go to statement, we go to in perfect label which we call.


Why is goto statement harmful?

It isn't. True, you can write bad code with goto, but you can do that without goto as well.


Why goto is not a good structured programming style?

goto is a statement, not a programming style.