Jumping Statement/Branching
In C Language branching is to jump from one segment of program to another segment. Branching can be upward and downward depends on requirement of program,sometimes Branching is used to skip some segment of code.due to branching program flow is not sequential.
C language support branching by following statement.
1) Break 2) Continue 3) Goto
1)Break Statement:- The break is a keyword in C. In C the break statement is used to breaks loop or switch statement.It breaks the control flow of the program at specified condition.We can use the break statement all the types of loops such as for ,while, do while.
Syntax:-
jump statement;
break;
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i; // initialization the local variable
for(i=1;i<=10;i++)// starting a loop from 1 to 10
{
if(i==6)
{ // if value of i is equal to 4, it will continue the loop
break;
}
printf("%d",i);
}// end of loop
getch();
}
output:
1 2 3 4 5
2. Continue Statement:-In C continue statement is used to continue the loop.It continues the control flow of program and the skips the remaining code at the specified condition. We can use the continue statement all the types of loops such as for ,while, do while.continue is also a keyword in c language.
Syntax:
jump statement;
continue;
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i; //initialization the local variable
for(i=1;i<=10;i++) // starting a loop from 1 to 10
{
if(i==6) //if value of i is equal to 4, it will continue the loop
{
continue;
}
printf("%d",i);
}// end of the loop
getch();
}
output:
1 2 3 4 5 7 8 9 10
3. Goto:-In C language goto statement is used to transfer control to the other part of the program.It can also be used to break the multiple loops which can't be done by using a single break statement.and in c language goto statement is used to provide an unconditional jump from the 'goto' to a labeled statement in the same function.
Syntax:-
label:
//code here
goto label;
Example :-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,i=1; //initialization the variable
printf("enter a value");
scanf("%d",&a);
value: //label name
printf("%d*%d=%d\n",a,i,a*i);
i++;
if(i<=10)
goto value;
getch();
}
Output:
enter a value 3
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30
Next Tutorial Available Soon As...
In C Language branching is to jump from one segment of program to another segment. Branching can be upward and downward depends on requirement of program,sometimes Branching is used to skip some segment of code.due to branching program flow is not sequential.
C language support branching by following statement.
1) Break 2) Continue 3) Goto
1)Break Statement:- The break is a keyword in C. In C the break statement is used to breaks loop or switch statement.It breaks the control flow of the program at specified condition.We can use the break statement all the types of loops such as for ,while, do while.
Syntax:-
jump statement;
break;
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i; // initialization the local variable
for(i=1;i<=10;i++)// starting a loop from 1 to 10
{
if(i==6)
{ // if value of i is equal to 4, it will continue the loop
break;
}
printf("%d",i);
}// end of loop
getch();
}
output:
1 2 3 4 5
Syntax:
jump statement;
continue;
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i; //initialization the local variable
for(i=1;i<=10;i++) // starting a loop from 1 to 10
{
if(i==6) //if value of i is equal to 4, it will continue the loop
{
continue;
}
printf("%d",i);
}// end of the loop
getch();
}
output:
1 2 3 4 5 7 8 9 10
3. Goto:-In C language goto statement is used to transfer control to the other part of the program.It can also be used to break the multiple loops which can't be done by using a single break statement.and in c language goto statement is used to provide an unconditional jump from the 'goto' to a labeled statement in the same function.
Syntax:-
label:
//code here
goto label;
Example :-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,i=1; //initialization the variable
printf("enter a value");
scanf("%d",&a);
value: //label name
printf("%d*%d=%d\n",a,i,a*i);
i++;
if(i<=10)
goto value;
getch();
}
Output:
enter a value 3
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30
Next Tutorial Available Soon As...
Wonderful
ReplyDelete