In some programming cases, we want to take control to the start of the loop, skipping the statements inside the loop, and sometimes we want to jump out of the loop. For these purposes, we have some special statements known as the jump statement in C. They are :
The break statement allows us to jump out of the loop instantly, without waiting to get back to the conditional test. When the break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if.
Example: Write a program to check whether a number is prime or not.
#include <stdio.h>
void main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
output:
Enter a positive integer: 23
23 is a prime number.
continue statement is used when we want to take control to the start of the loop, skipping the statements inside the loop, which have not yet been executed. When continue is encountered inside any loop, control automatically passes to the beginning of the loop. A continue is also usually associated with an if.
Example:
#include <stdio.h>
void main() {
int i,j;
for(i=1;i<=2;i++)
{
for( j=1;j<=2;j++)
{
if(i==j)
continue;
printf("\n%d %d",i,j);
}
}
}
Output:
1 2
2 1
Here, when the value of i equals that of j, the continue statement takes the control to the for loop
(inner) skipping the rest of the statements pending execution in the for loop(inner).
This statement is used to transfer control to the tagged statement in the program. The tag is the valid identifier and placed just before the statement from where the control is transferred. The usage of the goto keyword should be avoided as it usually violates the normal flow of execution.
Example:
#include <stdio.h>
void main() {
int marks;
printf("enter marks: ");
scanf("%d",&marks);
if(marks<38)
goto sos;
else
printf("You have passed");
sos:printf("you have failed");
}
Output:
enter marks: 12
you have failed
Note: also read about do-while loop in c & while loop examples in c
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…