answersLogoWhite

0


Best Answer

The purpose of goto is to skip over code and gotoa named label.

Goto is not as bad to use as you will read in many places, goto has a bad name from BASIC where gotoproduced spaghetti code that was impossible follow and understand.

In C you can only goto go to a named label in the same function that the goto statement is in. The named label can be prior or post of the goto statement.

Goto is very useful (in a limited scope) to jump over a bunch of if statements that are redundant because it has already been established that a particular section of code in the function needs to be executed. Judicious use of goto can improve the human readability of the code and also improve the programs performance.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the purpose of 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.


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; }


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;


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)


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; }


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


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.


What is the purpose of Continue and break statement in C?

The break statement is frequently used to terminate the processing of a particular case within a switch statement. Lack of an enclosing iterative or switch statement generates an error.Within nested statements, the break statement terminates only the do, for, switch, or whilestatement that immediately encloses it. You can use a returnor goto statement to transfer control elsewhere out of the nested structure.This example illustrates the break statement:#include int main() { char c; for(;;) { printf_s( "\nPress any key, Q to quit: " ); // Convert to character value scanf_s("%c", &c); if (c == 'Q') break; } } // Loop exits only when 'Q' is pressed