Monday, December 29, 2008

Jump statement

GOTO statement-
A other type of statement is the ‘goto’ statement. This statement is used to jump to a position in a program. This statement takes the control to the position given in the ‘goto’ statement. The use of this statement is avoided or it is never used. This is because the statement takes the control to other position which can be tedious. Due to this the understanding of the program becomes difficult to other person reading it. It is advised not to use this statement.
e.g.
void main()
{
Int x,y;
L: Printf(“Enter number greater than 5”);
Scanf(“%d”,&x);
If(x<=y)
{
Printf(“The number is not valid”);
Goto L;
}
else
{
Printf(“the number is %d”,x);
}
Getch();
}
In this program if the condition is not satisfied then the control will go again to the starting of the program otherwise it will print the number and exit.
Break statement-
This statement is used to break the loop i.e. it takes the control out of the loop. The statement is used to jump out of the loop when a condition is not satisfied.
This statement is normally used in the switch statement. The use of break is very essential in the switch statement.
Eg.
Void main()
{
Int x;
For(x=0;x<10;x++)
{
Printf(“%d”,x)
If(x==5)
Break;
}
Getch();
}
In the above program the ‘for’ loop continues its function of printing and incrementing and it also checks if x is five or not. When it reaches five it jumps out of the loop due to the break statement. It doesnot take the control out of the program.
Exit() statement-
This statement has similar function as the break statement but the difference is that it takes the control out of the program. It jumps out of the program when the condition is satisfied.
e.g.
void main()
{
Int x,y;
Y=3;
For(x=0;x<5;x++)
{
Printf(“%d”,x);
If(x==y)
Exit();
}
Getch();
}
In the above program when x and y become equal the program exits. i.e. when x becomes three program exits.
Continue statement-
The continue statement takes the control of the program to the beginning of the loop. Hence it is used in continuous loops where it is necessary to repeat the loop.
e.g.
void main()
{
Int x.y;
For(x=0;;x++)
{
Printf(“%d”,x);
If(x!=6)
Continue;
}
Getch();
}
In the program the ‘continue’ statement gives the condition upto which number the loop should continue.
Return statement-
The return statement is used mainly in the function which will be seen further. It is used to take the control out of the function to the main program. It can be assigned a value which can be returned to the main program. The function for example calculates a value. This value is taken to the main program by the return statement.
e.g. return x;
int xyz()
{
……
……
Return x;
}
void main(0
{
Int x;
…..
…..
Int xyz();
}
In the above example the function returns the value of x. the return statement is used in the function which has a return type. It is not used in the function with return type void.

No comments:

Watch tv!