In C programming language, jump statements are used to transfer control from one part of the program to another. There are three types of jump statements in C: break, continue, and goto.
1. break Statement: The break statement is used to terminate the current loop or switch statement. When the break statement is encountered inside a loop or switch statement, the program control is transferred to the statement immediately following the loop or switch. Here’s an example of how to use the break statement in C:
#includeint main() { int i; for (i = 1; i <= 5; i++) { if (i == 3) { break; } printf("%d\n", i); } return 0; }
In this example, we use the break statement to terminate the for loop when the value of i is 3. When the break statement is encountered, the program control is transferred to the statement immediately following the for loop.
2. continue Statement: The continue statement is used to skip the current iteration of a loop and continue with the next iteration. When the continue statement is encountered inside a loop, the program control is transferred to the beginning of the loop. Here's an example of how to use the continue statement in C:
#includeint main() { int i; for (i = 1; i <= 5; i++) { if (i == 3) { continue; } printf("%d\n", i); } return 0; }
In this example, we use the continue statement to skip the iteration when the value of i is 3. When the continue statement is encountered, the program control is transferred to the beginning of the for loop.
3. goto Statement: The goto statement is used to transfer control to a labeled statement in the same function. The labeled statement is identified by a label followed by a colon. Here's an example of how to use the goto statement in C:
#includeint main() { int i = 1; loop: printf("%d\n", i); i++; if (i <= 5) { goto loop; } return 0; }
In this example, we use the goto statement to transfer control to the labeled statement "loop" inside the same function. The labeled statement is identified by the label "loop" followed by a colon. The value of i is printed, and then incremented by 1. The program control is transferred to the labeled statement as long as the value of i is less than or equal to 5.