UNCONDITIONAL JUMP STATEMENTS
Loop performs a set of statements, till the condition becomes false. The number of times a loop
is to be repeated is decided in advance and a condition is set. But sometimes it becomes
necessary to skip certain part of the loop or terminate the loop if a particular condition is true
then jump statements come to action:
C supports four jump statements:
i. break.
ii. continue.
iii. goto.
iv return.
1.break statement
The break statement is used inside a loop, to directly come out of the loop.
The break statement ends the loop immediately when it is encountered.
Its syntax is: break;
The break statement is almost always used with if...else statement inside the loop.
Can be used to jump out of while, do or for loop.
A break causes an exit from only the innermost while or for loop.
For example, we are writing a program that checks whether a number “n” is prime.
It is useful for writing loops in which the exit point is in the middle of the body rather
than at the beginning or end.
How break statement works?
Example no -1 to find a number is prime or not
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 1) printf("1 is neither prime nor composite.");
for (i = 2; i <= n / 2; ++i)
{
// condition for non-prime
if (n % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0) printf("%d is a prime number.", n);
elseprintf("%d is not a prime number.", n);
return 0;
}
Output
Enter a positive integer: 56
56 is not a prime number.
Example -2 Program to calculate the sum of numbers (10 numbers max) using break;
// If the user enters a negative number, the loop terminates
#include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
for (i = 1; i <= 5; ++i)
{
printf("Enter a number %d: ", i);
scanf("%lf", &number);
// if the user enters a negative number, break the loop
if (number < 0.0)
{
break;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf", sum);
return 0;
}
output
Enter a number 1: 34
Enter a number 2: 23
Enter a number 3: 12
Enter a number 4: 67
Enter a number 5: 89
Sum = 225.00
2. continue statement
The continue statement in C language is used to bring the program control to the
beginning of the loop. The continue statement skips some lines of code inside the loop
and continues with the next iteration.
syntax : continue;
In for loop, continue brings the control to the next increment or decrement but in while or
do while loop continue causes the control of the loop to jump directly to the condition.
Break transfers control just past the end of a loop, while continue transferes control to a
point just before the end of hte loop body. With break, control leaves the loop, with
continue, control remains inside the loop.
Break can be used in switch statements and loops wherea continue is limited to loops.
Continue statement example 1
#include<stdio.h>
void main ()
{
int i = 0;
while(i!=10)
{
printf("%d", i);
continue;
i++;
}
}
Output
infinite loop
example-2 //continue statement
#include <stdio.h>
int main ()
{
int a = 10;
do
{
if( a == 15)
{
a = a + 1;
continue;
}
printf("value of a: %d\n", a);
a++;
} while( a < 20 );
return 0;
}
output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
3.goto statement
It is also used to jump from one part of the program to the other or exit from the deeply
nested loop. But using goto is not a good practice because it makes the logic complicated
and long programs unreadable.
A goto statement in C programming provides an unconditional jump from the 'goto' to a
labeled statement in the same function.
NOTE − Use of goto statement is highly discouraged in any programming language because it
makes difficult to trace the control flow of a program, making the program hard to understand
and hard to modify. Any program that uses a goto can be rewritten to avoid them.
Syntax
The syntax for a goto statement in C is as follows −
goto label;
..
.
label: statement;
Here label can be any plain text except C keyword and it can be set anywhere in the C program
above or below to goto statement.
example
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 1) printf("1 is neither prime nor composite.");
for (i = 2; i <= n / 2; ++i)
{
// condition for non-prime
if (n % i == 0)
{
flag = 1;
goto lab; // the loop will again run the for loop – though flag Is set.
}
}
if (flag == 0) printf("%d is a prime number.", n);
lab : if (flag==1) printf("%d is not a prime number.", n);
return 0;
}
Output
Enter a positive integer: 6
6 is not a prime number.
4.return statement
Used in a function to return some value and to jump from called function definition to calling
function definition.
Syntax
return;
Returning control from function that returns value:
return <value>;
The return value could be any valid expression that returns a value:
a constant
a variable
a calculation, for instance (a + b) * c
call to another function that returns a value
how the return statement works?
First you need to understand how the function call works. When you call a function two things
happen
1. The execution of the current function is paused.
2. The function that you call executes.
This is what we call a transfer of control. When you call a function the control of the
program is transferred from the calling function to the called function.
The return statement returns the control to the previous routine. That function will
continue its execution from the point where it was paused.
On the contrary, a function that calls the return statement is finished. It is not in a paused
state and it cannot resume.
Example-1
#include <stdio.h>
// void method
void Print()
{
printf("Welcome");
return; // void method using the return statement
}
// Driver method
int main()
{
// Calling print
Print();
return 0;
}
Output
Welcome
Example-2
#include<stdio.h>
void main()
{
int sum=sumDigits();
printf("%d\n", sum);
return;
}
int sumDigits()
{
int sum = 0;
int digit;
for(digit=0;digit<=9;++digit)
{
sum += digit;
}
return sum;
}
Output
45
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@