Jump Statements
Jump Statements
1. break: is used to terminate the control structures (for, while and do while, switch-case).
2. continue: (for, while and do while) .
3. goto: we can use anywhere in the code.
4. return: in functions only
//break
#include <stdio.h>
int main()
{
for(int i=1; i<=10; i++)
{
printf("%d ", i);
break; // unconditional jump
}
}
https://onlinegdb.com/2EK51Dxq5
#include <stdio.h>
int main()
{
for(int i=1; i<=10; i++)
{
printf("%d ", i);
if(i==7)
break; // conditional jump
}
}
#include <stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("%d ", i);
if(i>5)
break; // conditional jump
i++;
}
int main()
{
int i=1;
while(i<=10)
{
printf("%d ", i);
if(i==5)
break; // jump from
i++;
}
// break will jump here
}
#include <stdio.h>
int main()
{
int i=1;
while(i<=10)
{
if(i>5)
break; // jump from
printf("%d ", i);
i++;
}
// break will jump here
}
// CONTINUE
It will continue with next iteration by skipping all the staements written below this in a loop.
#include <stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("%d ",i);
i++;
continue;
printf(" hello \n");
}
}
#include <stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("%d ",i);
i++;
if (i==5)
continue;
printf(" hello \n");
}
}
#include <stdio.h>
int main()
{
int i=0;
while(i<10)
{
i++;
if (i==5)
continue;
printf("%d ",i);
}
}
#include <stdio.h>
int main()
{
int i=0;
while(i<45) // jump back here
{
i++;
if (i==13)
continue;
printf("%d ",i);
}
}
#include <stdio.h>
int main()
{
int i=0;
do
{
i++;
if (i==13)
continue;
printf("%d ",i);
} while (i<45); // jump here
}
#include <stdio.h>
int main()
{
int i;
for( i=0; i<45; i++) // jump back at updation
{
if (i==13)
continue;
printf("%d ",i);
}
}
#include <stdio.h>
int main()
{
int i;
for( i=1; i<=50; i++)
{
if (i%2!=0)
continue;
printf("%d ",i);
}
}
#include <stdio.h>
int main()
{
int i;
for( i=1; i<=50; i++)
{
if (i%2==0)
continue;
printf("%d ",i);
}
}
goto statement:
By using goto statement, we can jump anywhere in the code.
1. forward jump (will skip statements)
2. backword jump (will repeat statements)
it can be conditional or unconditional jump.
#include <stdio.h>
int main()
{
goto a;
printf("A");
printf("B");
a: //label
printf("C");
}
#include <stdio.h>
int main()
{
goto a; // forward jump
printf("A");
a: // label
printf("B");
printf("C");
}
#include <stdio.h>
int main()
{
l: // label
printf(" hello ");
goto l; //backward jump
// a infinite loop, due to unconditional jump
}
#include <stdio.h>
int main()
{
int i=1;
l:
printf(" %d", i);
i++;
if(i<=100)
goto l;
}
#include <stdio.h>
int main()
{
int fact, n;
int ch;
repeat:
printf("enter a number to find factorial:");
scanf("%d", &n);
fact=1;
for(int i=1; i<=n; i++)
{
fact=fact*i;
}
printf("The factorial of %d = %d", n, fact);
printf("\n\n Do you want to check factorial of another number (1/0):");
scanf("%d", &ch);
if(ch==1)
goto repeat;
}
#include <stdio.h>
int main()
{
int fact, n;
char ch;
repeat:
printf("Enter a number to find factorial:");
scanf("%d", &n);
fact=1;
for(int i=1; i<=n; i++)
{
fact=fact*i;
}
printf("The factorial of %d = %d", n, fact);
printf("\n\n Do you want to check factorial of another number (y/n):");
fflush(stdin); // to clean input buffer
scanf("%c", &ch);
if(ch=='y')
goto repeat;
}