What are the Jumping Statements ???
Jump Statements are the Control Structure Statements that let you change the way of the program typically execute. They enable you to move the control between different areas of the code, usually inside of loops, conditional expressions, or functions.
In Simple Words, we also says that Jumping Statements are used to transfer the control from one part of the program to another part.
Types of Jumping Statemets
The Various Jumping Statements used in C are as follows:
- The break Statement
- The continue Statement
- The goto Statement
1. The break Statement
- The break Statement is always used inside the body of the switch statement.
- It can also be used in any of the loop statement for transfer of control from the loop to the statement immediately.
- In switch Statement, it is used as the last statement of the statement block of every case except the last one.
- It transfer the control out of the switch statement and the program continues its execution from the statement following switch.
- In while, for, do-while loops, it is always used in conjunction with if statement.
- When executed it transfers the control out of the particular loop and the executions of the program continues after looping block.
Example:
Let’s take an example for the break Statement for understand it briefly,
#include <stdio.h> int main() { // Break statement example for (int i = 1; i <= 10; i++) { if (i == 5) { printf("Loop terminated at %d\n", i); break; } printf("Iteration %d\n", i); } return 0; }
Explanation About Example:
In this example program, we iterate 1 to 10 numbers. But when “i” reaches at value 5 then we used break Statement which terminate the loop and out from the loop.
C Language Notes
2. continue Statements
- The continue statement is used inside the body of the loop Statement.
- The break statement terminates the loop, while the continue statement does not terminate the loop but transfer the control back to the first statement.
- The continue statement is also usually associated with if Statement.
- The continue statement transfers the control to the beginning of the loop, bypassing the statements which are not yet executed.
Example:
Let’s take an example of the continue Statement for understand it briefly,
#include <stdio.h> int main() { // Continue statement example for (int i = 1; i <= 5; i++) { if (i == 3) { printf("Iteration %d skipped\n", i); continue; } printf("Iteration %d\n", i); } return 0; }
Explanation about Example:
In this example program, we iterate a 1 to 5 numbers but when the value of ‘i’ becomes 3 then it will execute continue statement and skip one iteration.
3. go to Statement
- In C every program can be written without the use of goto Statement.
- Sometimes a situation may occur, where we have to use the goto Statement.
- It is an unconditional transfer of control which means that the sequence of execution will be broken without performing any test and the control will be transferred to some statement other than the immediate next one.
- The goto Statement causes the control to be transferred to the statement whose label is specified in the goto statement.
Exmaple:
Let’s take an example to understand the goto Statement briefly,
#include <stdio.h> int main() { int i = 1; start: printf("Iteration %d\n", i); i++; if (i <= 5) { goto start; } return 0; }
Explanation about Example:
Here we use goto Statement, in this we define a ‘start’ label. When the loop starts the program go back to the ‘start’ label if the condition is true.