C – Jump statements

C jump statements can be either conditional or unconditional statement. The conditional statements generally related to if statement. The examples of jump conditional statements are break statement and continue statement. The example of a jump unconditional statement is goto statement.

break statement

C programming break statement often used with the conditional statement. The break statement immediately terminates the loops (like do-while, for and while) or switch statement in which it appears.

General Form (Syntax):

The syntax of the break statement is given below,

break;

break statement flowchart:

The flow chart of the break statement is shown below,

Example

Program (1): To demonstrate the use of break statement.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int i;
for(i=1;i<20;++i)
{
if(i==5)
break;     //break the for loop at i=5
printf("\n %d",i);
}
getch();
}

Output (1)

 1
 2
 3
 4

Program (1): To check enter number is prime or not using break statement.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int i,n,r;
printf("enter a number :- ");
scanf("%d",&n);
for(i=2;i<n;++i)
{
r=n%i;
if(r==0)
{
break;
}
}
if(i==n||r!=0)
printf("\n number %d is prime",n);
else
printf("\n number %d is not prime",n);
getch();
}

Output (1)

enter a number - 32
number 32 is not prime

continue statement:

C programming continue statement generally used with conditional statements such as if statement within the loop (like do-while, for and while). The continue statement skips all statements written after it as well as transfer the program control to the next iteration of the loop.

General Form (Syntax):

The syntax of continue statement is given below,

continue;

continue statement flowchart:

The flow chart of continue statement is shown below,

Example

Program (1): To demonstrate the use of continue statement.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int i;
for(i=1;i<20;++i)
{
if(i==5) //skip the loop value at i=5 only
continue;
printf("\n %d",i);
}
getch();
}

Output (1)

 1
 2
 3
 4
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19

Program (1): To print all combinations of digits 1,2,3 with condition no digit should repeat in any combination using continue statement.

#include<stdio.h>
#include<conio.h>
void main(void)
{
int i,j,k;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
{
if(i==j||j==k||k==i)
continue;
printf("\n %d %d %d",i,j,k);
}
}
}
getch();
}

Output (1)

 1 2 3
 1 3 2
 2 1 3
 2 3 1
 3 1 2
 3 2 1

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *