Jump Statement in C
In C programming language, jump statements are used to control the flow of a program by transferring the control of the program to a different part of the code.
There are three types of jump statements in C: goto, break, and continue.
The goto statement
A label is considered as an identifier followed by a colon and it can be placed ahead of a statement in the program. The goto statement transfers the control of the program to a labeled statement.
The syntax of the goto statement is:
Syntax:
goto label;
When the goto statement is executed, the control of the program jumps to the labeled statement.
However, the excessive use of the goto statement can make the code harder to read and understand, and can lead to difficult-to-maintain code. Therefore, the goto statement should be used judiciously.
Here's an example of the goto statement in C:
Example:
#include <stdio.h>
int main() {
int num = 1;
loop: // label for the goto statement
printf("%d ", num);
num++;
if(num<= 10) {
goto loop; // jumps to the label "loop"
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
In this example, the program prints the numbers 1 to 10 using a loop implemented with the goto statement. The loop label serves as a target for the goto statement to jump back to when num is less than or equal to 10. The program prints the value of num and increases it each time through the loop. Once the num is greater than 10, the program exits the loop and returns 0.
The break statement
The break statement terminates the execution of the loop or switch statement. When the break statement is executed, the control of the program is shifted to the next statement after the loop or switch statement.
For example, in for loop, the break statement is used to terminate the loop when a specific condition is met. The syntax of the break statement is:
Syntax:
break;
Here's an example of using the break statement in C:
Example:
#include <stdio.h>
int main() {
int i;
for(i = 1; i<= 10; i++) {
if(i == 5) {
break; // terminates the loop when i is equal to 5
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Output:
1 2 3 4
In this example, the program uses a for loop to print the numbers from 1 to 4 using printf function, and then terminates the loop when i is equal to 5 using the break statement. The program then exits the loop and continues executing the code that follows the loop.
The break statement is commonly used in loops such as for, while, and do-while to terminate the loop prematurely. When a break statement is encountered in the loop, the control immediately exits the loop and continues with the next statement outside of the loop.
The continue statement
The continue statement is used to skip the current iteration of a loop and move to the next iteration. When the continue statement is executed, the control of the program jumps to the loop's condition check.
For example, in a for loop, the continue statement is used to skip the current iteration when a specific condition is met, and move to the next iteration. The syntax of the continue statement is:
Syntax:
continue;
It is important to use these jump statements judiciously, as incorrect use of the break or continue statements can result in logical errors in the program. In addition, the excessive use of the goto statement can make the code harder to read and understand. It is generally recommended to use structured programming constructs, such as loops and conditional statements, as much as possible to make the code more readable and maintainable.
Here's an example of using the continue statement in C:
Example:
#include <stdio.h>
int main() {
int i;
for(i = 1; i<= 10; i++) {
if(i == 5) {
continue; // skips the current iteration when i is equal to 5
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Output:
1 2 3 4 6 7 8 9 10
In this example, the program uses a for loop to print the numbers from 1 to 10 using the printf function. However, when i is equal to 5, the continue statement is executed, which causes the loop to skip the rest of the statements in the current iteration and move on to the next iteration. This means that the number 5 is not printed, and the program continues to print the numbers 1 through 4, and then 6 through 10.
The continue statement is commonly used in loops to skip over certain iterations based on a condition.