Jump Statements:
Jump Statement makes the control jump to another section of the program unconditionally when encountered. It is usually used to terminate the loop or switch-case instantly. It is also used to escape the execution of a section of the program. There are mainly three types of jump statements break, continue, or goto statement.
Types of Jump Statement:
1.’break’ Statement:
The ‘break‘ statement is used to exit from the loop constructs. When a ‘break‘ statement is encountered then the control is exited from the loop construct immediately. The ‘break‘ statement is usually used with the Switch Statement, and it can also use it within while loop, do-while loop, or for-loop.
Syntax:
for( ; ; )
{
…
break; ——
….. –
} – Break transfer the control outside the loop.
…. – cause of break become unnatural death of loop
…. <——- emergency exit from loop
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;
clrscr();
printf(“nenter a number”);
scanf(“%d”,&num); // 5, 9
for(i=2;i<num;i++) // 2, 3, 4, 5 ,2,3
{
if(num%i==0) //5%2==0=>F, 5%3==0=>F, 5%4==0>F, 9%2==0>F, 9%3==0=>T
{
break;
}
}
if(i==num) //5==5, 3==9
{
printf(“n%d is prime number”,num);
}
else
{
printf(“n%d is not prime number”,num);
}
getch();
}
Output:
enter a number: 37
37 is prime number
Explanation of Example: Certainly! The break statement in this code is used to exit the for loop prematurely when the condition num % i == 0 is true.
Let’s go through the code step by step to understand the use of break:
- The for loop is designed to iterate from i = 2 to i = num – 1.
- Inside the loop, there is an if statement that checks if num is divisible by the current value of i (i.e., num % i == 0).
- If the condition num % i == 0 is true, it means that num is divisible by i, and the break statement is executed. This causes an immediate exit from the for loop, even if the loop hasn’t reached its normal termination condition (i < num).
- After the loop, the program checks if i is equal to num. If i is equal to num, it means the loop terminated because of the condition num % i == 0, indicating that num is not a prime number.
So, in this specific code, the break statement is used to optimize the loop by stopping the iteration as soon as a factor of num is found, making the program more efficient for checking whether a number is prime or not.
2.’continue’ Statement:
When a ‘continue‘ statement is encountered then the control is automatically passed from the beginning of the loop statement. The ‘continue‘ statement is usually used within the while loop, do-while loop, or for-loop.
Syntax:
for( ; ; ) –
{ – transfer the control to next interation of loop
….. – when it work its below statements skip for that time
…. –
continue;——
……….
……
}
Example:
#include<conio.h>
#include<stdio.h>void main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==5 || i==7)
{
continue;
}
printf(“n%d”,i);
}
getch();
}
Output:
1
2
3
4
6
8
9
10
Explanation of Example: Certainly! The continue statement in this code is used to skip the remaining code inside the loop when a specific condition is met.
Let’s go through the code step by step to understand the use of continue:
- For Loop: The program uses a for loop to iterate from i=1 to i<=10.
- If Statement: Inside the loop, there’s an if statement that checks if i is equal to 5 or 7 (i==5 || i==7).
- Continue Statement: If the condition i==5 || i==7 is true, the continue statement is executed. The continue statement causes the program to skip the remaining code inside the loop for the current iteration and jump to the next iteration of the loop.
- Print Statement: If the condition in the if statement is false, meaning i is not equal to 5 or 7, the program proceeds to the printf statement, which prints the value of i with a newline character.
- Loop Execution: The loop continues to iterate until i reaches 10, and for each iteration where i is not equal to 5 or 7, it prints the value of i.
- getch(): The getch() function is used to wait for a key press before closing the console window.
So, in this code, the continue statement is utilized to skip printing the value of i for the specific values of 5 and 7, and it directly moves to the next iteration of the loop. As a result, the program prints the numbers from 1 to 10, excluding 5 and 7.
3.’goto’ Statement:
- ‘goto‘ statement as name indicate goes to particular location in program. It is used to transfer control of program to a given location. The location where control is to transfer is given using label.
- But use of ‘goto‘ statement should be avoided because transferring control to forward or backward location is bad programmimg practice. The label used in ‘goto‘ statement can be a constant or variable. The label is followed by :(colon)
- Different type of ‘goto‘ can be:
a. Conditional ‘goto‘: if it is used ‘if‘ statement.
b. Unconditional ‘goto‘: if it is not used with ‘if‘ statement.
Syntax:
goto label;
statement 1;statement 2;
.
.
label: statement n;
Example:
#include <stdio.h>int main() {int i = 0;// Using goto to jump to a labeled statementstart_loop:if (i < 5) {printf(“%d “, i);i++;goto start_loop;}return 0;}
Output:
0 1 2 3 4
Explanation of Example: Certainly! The ‘goto’ statement in C allows you to transfer control to a labeled statement within the same function. However, it’s important to use ‘goto’ with caution, as its misuse can lead to code that is difficult to understand and maintain.
Let’s go through the code step by step to understand the use of continue:
- start_loop: is a label that marks a specific point in the code.
- The goto start_loop; statement transfers control back to the labeled statement, creating a loop that prints values of i from 0 to 4.
It’s crucial to note that the use of ‘goto’ is generally discouraged in modern programming because it can lead to “spaghetti code” and make the code less readable and maintainable. In most cases, structured control flow constructs like loops (for, while, do-while) and conditional statements (if, switch) are preferred over ‘goto‘.