What is the purpose of the ‘goto’ statement in C?
a) To define a loop
b) To terminate a program
c) To jump to a specific labeled statement
d) To return a value from a function
Answer:
c) To jump to a specific labeled statement
Explanation:
The goto
statement in C is used to jump to a specific labeled statement within a function. It is generally discouraged because it can make the control flow of a program difficult to follow, leading to “spaghetti code.” However, in certain cases, such as breaking out of deeply nested loops, it can be useful. For example, goto label;
jumps to the statement marked by label:
.
Understanding the use of the goto
statement is important for managing control flow, though it should be used sparingly.