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.
·
Break
·
Continue
·
Goto
·
Return
1. Break Jump
Statement
A break statement is
used to terminate the execution of the rest of the block where it is present
and takes the control out of the block to the next statement.
It is mostly used in loops and
switch-case to bypass the rest of the statement and take the control to the end
of the loop. The use of the break keyword in switch-case has
been explained in the previous tutorial Switch – Control Statement.
Another point to be taken into
consideration is that the break statement when used in nested
loops only terminates the inner loop where it is used and not any of the outer
loops.
Example
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 15; i++) {
printf("%d\n", i);
if (i == 10)
break;
}
return 0;
}
Output:-
1
2
3
4
5
6
7
8
9
10
2. continue Jump
Statement
The continue jump
statement like any other jump statement interrupts or changes the flow of
control during the execution of a program. Continue is mostly
used in loops.
Rather than terminating the loop it
stops the execution of the statements underneath and takes control to the next
iteration.
Similar to a break statement, in the
case of a nested loop, the continue passes the control to the next iteration of
the inner loop where it is present and not to any of the outer loops.
example
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i
< 3; i++) {
for (j = 1; j
< 5; j++) {
if (j == 2)
continue;
printf("%d\n",
j);
}
}
return 0;
}
Output:-
1
3
4
1
3
4
3. goto Jump
Statement
goto jump statement is used to
transfer the flow of control to any part of the program desired. The programmer
needs to specify a label or identifier with the goto statement in the following
manner:
goto label;
This label indicates
the location in the program where the control jumps to.
Example”
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i < 5; i++) {
if (i == 2)
goto there;
printf("%d\n", i);
}
there:
printf("Two");
return 0;
}
Output:-
1
Two
In this program, we see that when the
control goes to the goto there; statement
when i becomes equal to 2 then the control
next goes out of the loop to the label(there: ) and
prints Two.
4. Return Jump
Statement
Return jump statement is usually used
at the end of a function to end or terminate it with or without a value. It
takes the control from the calling function back to the main function(main
function itself can also have a return).
An important point to be taken into
consideration is that return can only be used in functions
that is declared with a return type such as int, float, double, char, etc.
The functions declared with void type
does not return any value. Also, the function returns the value that belongs to
the same data type as it is declared. Here is a simple example to show you how
the return statement works.
Example
#include <stdio.h>
char func(int ascii) {
return ((char) ascii);
}
int main() {
int ascii;
char ch;
printf("Enter
any ascii value in decimal: \n");
scanf("%d", & ascii);
ch = func(ascii);
printf("The
character is : %c", ch);
return 0;
}
Output:-
Enter any ascii value in
decimal:
110
The character is : n
Happy
coding!! 😊
Comments
Post a Comment