Explain Briefly about Jumping Statements in C?
https://www.computersprofessor.com/2016/06/explain-briefly-about-jumping.html?m=0
C permits a jump from one statement to another with in a
loop as well as a jump out of a loop.
The statements that are used to transfer the execution
control from one location to another in the program without checking any
condition are called unconditional (or) jumping statements.
The following are such type of statements:
1. Break
2. Continue
3. goto
1.break: (or)
jumping out of a loop:
The keyword is used to terminate the execution of a block
(or) loop. An early exit from a loop can be accomplished by using the break
statement.
Syntax:– break;
When a break statement is encountered inside a loop, the
loop is immediately exited & the program continues with the statement immediately
following the loop.
When the loops are nested, the break would n exit from
the loop containing it i.e., the break will exit only a single loop.
Ex:–Program to check whether the given number is prime or not
#include<stdio.h>
#include<conio.h>
main()
{
int i,n,count=0;
printf(“entre n value”);
scanf(“%d”,&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
count++;
break;
}
}
if(count==0)
printf(“given no is not a prime”);
getch();
}
2.Continue
(or) skipping a part of a loop:–
During the loop operations, it may be necessary to
skip a part of the body of the loop
under certain conditions.
Like the break statement, C supports another similar
statement called the continue statement. However unlike the break which causes
the loop to be terminated, the continue, as the name implies, causes the loop
to be continued with the next iteration after skipping any statements in
between.
The continue statement tells the compiler, “skip the
following statements & continue with the next iteration.
Syntax:–continue;
In while & do loops, continue causes the control to
go directly to the test condition & then to continue the iteration process.
In the case of for loop, the increment section of the loop is executed before
the test condition is evaluated.
3.Goto
statement :–
C supports the goto statement to branch unconditionally
from one point to another in the program.
The goto requires a label inorder to identify the place
where the branch is to be made. A label is any valid variable name, & must
be followed by a colon.
The label is placed immediately before the statement
where the control is to be transferred. The general forms of goto & label
statements are
Syntax:
The label cab be anywhere in the program either before or
after the goto label; statement.
If the label: is before the statement goto label; a loop
will be formed & some statements will be executed repeatedly. Such a jump
is known as a backward jump.
On the other hand, if the label: is placed after the goto
label; some statements will be skipped & the jump is known as forward jump.