0% found this document useful (0 votes)
37 views14 pages

Jump Statement

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views14 pages

Jump Statement

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CSE101-Lec#8

• Jump Statements

©LPU CSE101 C Programming


Jump statements
• You have learn that, the repetition of a loop is controlled by
the loop condition.
• C provides another way to control the loop, by using jump
statements.
• There are four jump statements:

©LPU CSE101 C Programming


break statement
• break is a keyword.
• break allows the programmer to terminate
the loop.
• A break statement causes control to transfer
to the first statement after the loop or block.
• The break statement can be used in nested
loops. If we use break in the innermost loop
then the control of the program is terminated
only from the innermost loop.
©LPU CSE101 C Programming
break statement
##include<stdio.h>
int main()
Program to
{ show use of
int n; break
for (n=10; n>0; n=n-1)
{
statement.
if (n<8)
break;
printf(“%d ”, n);
}
return 0;
}

©LPU CSE101 C Programming


continue statement
• continue statement is exactly opposite to
break.
• continue statement is used for continuing
the next iteration of the loop statements
• When it occurs in the loop, it does not
terminate, but skips the statements after this
statement

©LPU CSE101 C Programming


continue statement
• In while and do…while loops, the continue
statement transfers the control to the loop condition.
• In for loop, the continue statement transfers the
control to the updating part.

©LPU CSE101 C Programming


continue statement
#include<stdio.h>
Program to
int main()
{
show the use
int n; of continue
for (n=10; n>0; n=n-1) statement in
{ for loop
if (n%2==1)
continue;
printf(“%d ”, n);
}
return 0;
}

©LPU CSE101 C Programming


continue statement
#include<stdio.h> Program to
int main()
show the use
{
int n = 10; of continue
while(n>0) statement in
{ for loop
printf(“%d”, n);
if (n%2==1)
continue;
n = n –1;
}
return 0;
The loop then prints
} number 9 over and over
again. It never stops.
©LPU CSE101 C Programming
continue statement
#include<stdio.h> Program to
int main()
show the use
{
int n = 10; of continue
while(n>0){ statement in
printf(“%d”, n); for loop
if (n%2==1) For n=9, loop goes to infinite
execution
continue;
n = n –1;
}
}
The loop then prints
number 9 over and over
again. It never stops.
©LPU CSE101 C Programming
goto
• Unconditionally transfer control.
• goto may be used for transferring control from one
place to another.
• The syntax is:
goto identifier;
Control is unconditionally transferred to the location of a
local label specified by identifier. For example,
Again:
...
goto Again;
©LPU CSE101 C Programming
goto statement

n=10;

A:
n=10 printf(“%d “, n);
n = n -1;
A

Print if (n>0)
n
goto A;
n = n -1

Output:
is n>0? True A

10 9 8 7 6 5 4 3 2 1
False

©LPU CSE101 C Programming


#include<stdio.h>
int main() Program to
{
int x;
show goto
printf(“enter a number: ”); statement.
scanf(“%d”,&x);
if(x%2==0)
goto even;
else
goto odd;
even:
printf(“ %d is even”, x);
return;
odd:
printf(“%d is odd”, x);
}

enter a number: 18
18 is even

©LPU CSE101 C Programming


return statement
• Exits the function.
• return exits immediately from the currently
executing function to the calling routine,
optionally returning a value. The syntax is:
• return [expression];
• For example,
int sqr (int x){
return (x*x);
}
©LPU CSE101 C Programming
Next Class: Formatted and
Unformatted Input/Output functions

©LPU CSE101 C Programming


[email protected]

You might also like