JUMPING STATEMENTS :
The jumping
statements are categorized into three different controls which are used to jump
a statement from one to another in a program and make the execution of he
programming procedure fast. The three jumping controls are:
1.
goto statement
2. break
statement
3. continue
statement
1. goto
statement :
The goto
statement moves the controls on a specified address called “label“. The goto is
classified into two types. They are conditional and unconditional which can be
either forward or backward direction depending on the program requirement.
The different types
of goto are:
1. forward goto
2. backward
goto
1.
forward goto statement :
a.
forward goto without condition:
The
syntax for forward goto without condition statement is:
Statement-1;
Statement-2;
goto label;
Statement-3;
Statement-4;
Label:
Statement-5;
Statement-6;
Ex: /*program
for unconditional forward goto */
#include<stdio.h>
#include<conio.h>
void
main()
{
int a,b,c;
printf(“Enter a and b values:”);
scanf(“%d%d”,&a,&b);
c=a+b;
goto add;
goto minus;
add:
printf(“\nsum is %d”,c);
subtract:
c=a-b;
printf(“\nminus is%d”,c);
}
b.
forward goto with condition:
The
syntax for forward goto with condition statement is:
Statement-1;
Condition()
goto label;
Statement-3;
Label:
Ex:
/*program for conditional forward goto */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf(“Enter a and b
values:”);
scanf(“%d%d”,&a,&b);
if(x>y)
goto output1;
else
goto output2;
output1:
printf(“\nmaximum is %d”,x);
output2:
printf(“\nmaximum is %d”,y);
goto stop;
stop:
getch();
}
2.
backward goto statement :
a. backward goto without condition:
The
syntax for backward goto without condition statement is:
Statement-1;
Statement-2;
Label:
Statement-3;
Goto Label;
Statement-4;
In this first the
statement-1, statement-2 and statement-3 will be executed. Then it will find a
goto label so that it will go to a specified label and again the statements are
executed repeatedly creating a infinite looping.
b.backward goto
with condition:
The
syntax for backward goto with condition statement is:
Statement-1;
Label:
Statement-2;
Condition()
goto label;
Statement-3;
Ex: /*program to find square
root of a number */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b;
clrscr();
start:
printf(“Enter value of a:”);
scanf(“%d”,&a);
if(a<0)
goto start;
b=sqrt(a);
printf(“\n square root of %d is
%d”,&a,&b);
In
forward and backward goto with condition statements, any control statements can
be used in a condition as:
forward
goto with condition:
Statement-1;
if(Condition)
goto label;
Statement-3;
Label:
Statement-4;
backward goto with condition:
Statement-1;
Label:
Statement-2;
If(condition)
goto label;
Statement-3;
2.
break statement:
Break
statement always used with the decision-making statements like if and switch
statements.
The statement will quit from the loop when the condition is true. The syntax
for break statement is:
break;
The
statement is used within any of the looping statements.
while statement :
while(condition-1)
{
statement;
if(condition-2)
{
break;
}
}
statement;
do-while
statement:
do
{
statement-1;
if(condition)
{
break;
}
}while(condition);
statement-2;
for statement:
for(initial
value; test condition; increment/decrement)
{
if(condition)
{
break;
}
}
statement;
Ex:/*program to print sum of n
positive numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int I,num,n,sum=0;
clrscr();
printf(“Enter a
number:”);
scanf(“%d”,&num);
printf(“Enter the
numbers:”);
for(i=0;i<=n;i++)
{
scanf(“%d”,&n);
if(n<0)
break;
sum+=sum;
}
printf(“Sum of positive numbers
is:%d”,sum);
getch();
}
Output:
enter a number 4
Enter
the numbers:4
5
-2
Sum
of positive numbers is 9
3.
continue statement :
The continue statement will skip
some part of iteration and comes to the next looping step. The syntax for
continue statement is:
continue;
The statement is used within any of the looping statements.
while statement :
while(condition-1)
{
Statement;
if(condition-2)
{
continue;
}
}
Statement;
do-while statement:
do
{
Statement-1;
if(condition)
{
continue;
}
Statement-2;
}while(condition);
Statement-3;
for statement:
for(initial
value; test condition; increment/decrement)
{
Statement-1;
if(condition)
{
continue;
}
Statement-2;
}
Statement-3;
Ex:/*program to print sum of n
positive numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int I,num,n,sum=0;
clrscr();
printf(“Enter a
number:”);
scanf(“%d”,&num);
printf(“Enter the
numbers:”);
for(i=0;i<=n;i++)
{
scanf(“%d”,&n);
if(n<0)
continue;
sum+=sum;
}
printf(“Sum of positive numbers
is:%d”,sum);
getch();
}
Output:
enter a number 4
Enter
the numbers:4
5
-2
1
Sum
of positive numbers is 10
program
to accept & display 5 students marks*/
#include<stdio.h>
#include<conio.h>
void main()
{
int m1=65,m2=55,m3=80,m4=90,m5=67;
printf("1st student marks are
:%d\n",m1);
printf("2nd student marks are
:%d\n",m2);
printf("3rd student marks are
:%d\n",m3);
printf("4th student marks are:
%d\n",m4);
printf("5th student marks are:
%d\n",m5);
getch();
}
Output:
1st student marks are :65
2nd student marks are :55
3rd student marks are :80
4th student marks are: 90
5th student marks are: 67
In the above program, to display 5 students marks we have
declared 5 variables and 5 printf statements to display it. Suppose assume it
for the students of 100, then we have to take 100 variables to store marks
obtained by 100 different students and 100 printf statements or a single printf
statement containing these all variables. One common organizing technique is to
use arrays in such situations. We can declare one variable, which is capable of
holding all the 100 students marks.