Jumping Statements: The jump statement unconditionally transfer program control within a
function. C language provides us ...
You can see in the given example that the program is supposed to count the numbers from 1 to
20 using for loop. As per the...
skips the value and jumps to the do while loop without terminating the loop. Otherwise, it will
print the entered number.
...
#include<stdio.h>
void main()
{
int a;
start:
printf("enter any no.");
scanf("%d", &a);
if(a<0)
goto start; a=a*a;//Goto s...
Upcoming SlideShare
Loading in …5
×

Jumping statements

3,332 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Jumping statements

  1. 1. Jumping Statements: The jump statement unconditionally transfer program control within a function. C language provides us multiple statements through which we can transfer the control anywhere in the program. There are basically 3 Jumping statements: 1. break jumping statements. 2. continue jumping statements. 3. goto jumping statements. break statement: Sometimes, it is necessary to exit immediately from a loop as soon as the condition is satisfied. The break statement terminates the execution of the enclosing loop or conditional statement. In a for loop statement, the break statement can stop the counting when a given condition becomes true. The break statement is a jump instruction and can be used inside a switch construct, for loop, while loop and do-while loop. The execution of break statement causes immediate exit from the concern construct and the control is transferred to the statement following the loop. In the loop construct the execution of break statement terminates loop and further execution of the program is reserved with the statement following the body of the loop When break statement is used inside a loop, then it can cause to terminate from a loop. The statements after break statement are skipped. Syntax: The syntax for a break statement in C is as follows: break; The break statement in C programming language has the following two usages: 1. When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. 2. It can be used to terminate a case in the switch statement (covered in the next chapter). If you are using nested loops (i.e., one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.
  2. 2. You can see in the given example that the program is supposed to count the numbers from 1 to 20 using for loop. As per the condition defined the break statement stop the execution of the loop as soon as it detects the number 5 . #includes <stdio.h> #include <conio.h> void main () { clrscr(); int n; for (n=1; n<20; n++) { printf("%dn", n); getch(); if (n==5) break; } } continue statement The continue statement in C programming language works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, continue statement causes the program control passes to the conditional tests. Syntax: The syntax for a continue statement in C is as follows: continue; The continue statement provides a convenient way to force an immediate jump to the loop control statement. The break statement terminates the execution of the loop. As you can see in the given example, we have used both the statements within the do while loop. The program prompts the user to enter any number. If the number is less than 0, the break statement terminates the execution of the loop. If the number is greater than 10, the continue statement
  3. 3. skips the value and jumps to the do while loop without terminating the loop. Otherwise, it will print the entered number. #include <stdio.h> #include <conio.h> void main() { int i; clrscr(); for(i=1; i<=10; i++) { if(i==6) continue; printf("nt %d",i); // 6 is omitted } getch();} goto Statement : It is a well known as 'jumping statement.' It is primarily used to transfer the control of execution to any place in a program. It is useful to provide branching within a loop. When the loops are deeply nested at that if an error occurs then it is difficult to get exited from such loops. Simple break statement cannot work here properly. In this situations, goto statement is used. A goto statement in C programming language provides an unconditional jump from the goto to a labeled statement in the same function. 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.
  4. 4. #include<stdio.h> void main() { int a; start: printf("enter any no."); scanf("%d", &a); if(a<0) goto start; a=a*a;//Goto statement A printf("n %d", a); getch(); }

×