Jump statement in C
In the C programming language, a jump statement is used to alter the normal flow of execution of a program. It allows the program to transfer control to a different part of the code, such as a different function or a different block of code within the same function. There are three types of jump statements in C: goto, break, and continue.
The goto statement is utilized to transfer control to a labeled statement in the same function. It is often considered bad practice to use goto statements as they can make the code difficult to read and understand. However, there are some events where they may be useful, such as when implementing error handling.
The goto statement transfers control to the labeled statement, which is defined elsewhere in the function.
1 2 3 4 5 6 7 8 9 10
In this example, the program uses a goto statement to jump back to the loop label until the condition is met.
The break statement is utilized to exit a loop or switch statement before its normal termination. It is commonly utilized in loops to exit early if any specific condition is met.
1 2 3 4
In this example, the program uses the break statement to exit the loop early when i is equal to 5.
The continue statement is utilized to skip the remaining code in a loop for the current iteration and move on to the next iteration. It is commonly used in loops to skip over certain elements.
1 3 5 7 9
In this example, the program uses the continue statement to skip over even numbers in the loop and only print odd numbers.
The goto statement is often considered a controversial feature of C programming as it can lead to spaghetti code, which is difficult to understand and maintain. However, there are situations where the use of goto statement can be appropriate, such as when handling errors in a function. In such cases, the goto statement can be used to jump to a common error-handling section of code.
On the other hand, the break and continue statements are commonly utilized in loops to control the flow of execution. The break statement is utilized to exit a loop prematurely when a certain condition is met, while the continue statement is utilized to skip over certain iterations of a loop.
It is important to note that the excessive use of break and continue statements can make the code difficult to read and understand. Therefore, it is suggested to utilize them judiciously and only when necessary. In some cases, it may be better to use a flag variable or a conditional statement to control the loop flow instead of using a break or continue statement.
In addition to the three jump statements, C also provides the return statement, which is used to exit a function and return a value to the calling function. The return statement can also be used to terminate a program in the main() function.
Error: Negative number
In this example, the square() function returns -1 when it receives a negative number as input. The main() function checks the return value and terminates the program with a non-zero value if the result is -1.
In conclusion, jump statements in C are powerful tools that can alter the normal flow of execution of a program. While the goto statement is considered controversial, the break and continue statements are commonly used in loops to control the flow of execution. It is important to use these statements judiciously and with care to avoid creating code that is difficult to understand and maintain. Additionally, the return statement is mainly utilized to exit a function and return a value to the calling function or to terminate a program in the main() function.
Next TopicMultidimensional array in C