Back to: C Tutorials For Beginners and Professionals
Jump Statements in C with Examples
In this article, I will discuss Jump Statements in C Language with Examples. Please read our previous articles discussing the Entry Controlled Loop vs Exit Controlled Loop in C Language with Examples.
What are Jump Statements in C Language?
Jump statements in C programming alter the flow of control in a program. Jump statements allow you to exit a loop, start the next iteration of a loop, or explicitly transfer program control to a specified location in your program. C provides the following jump statements:
- break
- continue
- return
Break Statement in C:
This statement is used to terminate the nearest enclosing loop or switch statement. In loops, it causes the control to exit the loop immediately and resume at the next statement following the loop. In switch statements, it’s used to exit the switch and prevent the execution from falling through to subsequent cases.
Key Features:
- Used within loops or switch statements.
- Causes an immediate exit from the loop or switch block.
- It is commonly used to terminate a loop when a condition is met or skip the remainder of a switch block after a case is matched.
Example:
#include <stdio.h> int main() { for (int i = 0; i < 10; i++) { if (i == 5) { break; // Exits the loop when i is 5 } printf("%d\n", i); } return 0; }
Points to Remember:
- Purpose: Used to terminate the loop (for, while, do-while) or switch statement in which it appears. It causes the control flow to exit the loop or switch block and continue executing the code that follows immediately after the block.
- Usage Scenario: Commonly used in loops for ending iteration when a certain condition is met or in switch statements to terminate a case.
- Effect: In loops, it causes the program to exit the loop and continue executing the statement following the loop. In switch cases, it prevents the “fall-through” to subsequent cases.
- Example: In a for loop, if a specific condition is met, the break statement will terminate the loop immediately.
Continue Statement in C:
This statement is used inside loops. When a continue statement is encountered, the current iteration of the loop is terminated, and the control jumps to the beginning of the loop for the next iteration. This effectively skips the remaining code in the loop body for the current iteration.
Key Features:
- Used within loops.
- This causes the loop to skip the remaining portion of its body and immediately retest its condition before reiterating.
- It is useful for skipping the current iteration of the loop under certain conditions without terminating the loop entirely.
Example:
#include <stdio.h> int main() { for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; // Skips the rest of the loop body for even numbers } printf("%d\n", i); // Prints only odd numbers } return 0; }
Points to Remember:
- Purpose: Used within loops (for, while, do-while). It skips the remaining code in the current iteration of the loop and moves the control back to the beginning of the loop for the next iteration.
- Usage Scenario: Useful for skipping specific iterations within a loop when a certain condition is met without terminating the entire loop.
- Effect: Any statements in the loop that follow the continue statement are skipped for the current iteration. In the case of for loops, the update statement is still executed before checking the condition again.
- Example: In a for loop, if a certain condition is met, the continue statement will skip to the next iteration of the loop, ignoring the remaining code in the current iteration.
Goto Statement in C:
The goto statement provides an unconditional jump from the goto to a labeled statement in the same function. It’s generally advised not to use goto statement, as it can make the code less readable and harder to maintain.
Key Features:
- Allows the program to jump to a labeled statement elsewhere in the code.
- The label is a user-defined identifier followed by a colon (e.g., label:).
- The goto statement is generally avoided in modern programming due to its potential to make code less readable and harder to maintain.
Example:
#include <stdio.h> int main() { for (int i = 0; i < 10; i++) { if (i == 5) { goto end; // Jumps to the label 'end' } printf("%d\n", i); } end: printf("Loop is terminated\n"); return 0; }
Points to Remember:
- Purpose: Provides an unconditional jump from the goto to a labeled statement in the same function.
- Usage Scenario: It’s generally advised to avoid using goto as it makes the program hard to understand and maintain. However, it can be used for breaking out of deeply nested loops or in cases where other control structures are insufficient.
- Effect: The control jumps to a specified label. This label must be within the same function as the goto statement.
- Example: The goto statement can jump to a specific part of the program marked by a label.
Key Differences Between Break, Continue, and Goto in C:
- Scope of Effect: break and continue affect the loop or switch statement they are in, whereas goto can jump to any part of the function.
- Control Flow: break terminates the loop or switch case, continue skips to the next iteration of the loop, and goto can jump to any labeled statement.
- Usage Best Practices: break and continue are generally considered safer and more readable. goto is often discouraged due to the potential for creating spaghetti code, which is complex and tangled.
- Readability and Maintenance: break and continue are considered safe and enhance readability. The use of goto can lead to spaghetti code, making it difficult to trace the flow of the program; thus, it’s generally avoided.
While break, continue, and goto are similar in controlling the execution flow, their usage and impact on code readability and structure vary significantly. break and continue are more structured and are widely accepted in programming practice, whereas goto is typically discouraged due to its potential negative impact on code structure.
In the next article, I will discuss Break Statements in C Language with examples. In this article, I explain Jump Statements in C Language with examples. I hope you enjoy this article on Jump Statements in C Language with Examples. I would like to have your feedback. Please post your feedback, questions, or comments about this article.