Break Statement in C


break statement allows you to:
(1) Transfer the control of the program immediately after the loop either conditionally or unconditionally.
It allows you to transfer the control of the program outside the loop in which it exist.
You can break the execution of the loop either conditionally or conditionally.

If you write break statement inside the nested loop then it will transfer program control to the outside loop in which it exist.

#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
clrscr ();
for (i=1;i<=10;i++)
{
if (i % 5 == 0)
break;
else
printf(“%d ”,i);
}
getch ();
}
Output:
1 2 3 4

     

(2) Transfer the control of the program immediately after the switch case statement.
It allows you to transfer the control of the program outside the switch case statement after executing matching case statement.
You need to write break statement at the end of each case block. If you don’t specify the break statement in case block then it will executes all case block after the matching case block.

#include<stdio.h>
#include<conio.h>
void main ()
{
int ch;
clrscr ();
printf(“Enter Choice”);
scanf(“%d”, &ch);
switch (ch)
{
        case 1:
        printf(“Red”);
        break;
        case 2:
        printf(“Green”);
        break;
        case 3:
        printf(“Blue”);
        break;
}
getch ();
}
Output:
Enter Choice:
2
Green


Continue Statement in C


Sometimes it is required to skip the execution of some statements inside the loop. Continue statement allows you to skip the statements that appear immediately after it and transfer the control of the program to the next iteration in the loop.
It must appear inside for, while or do while loop.

#include<stdio.h>
#include<conio.h>
void main ()
{
       int i;
       clrscr ();
       for (i=1;i<=10;i++)
       {
                if (i % 2 == 0)
                continue;
                else
                printf(“%d ”,i);
        }
        getch ();
}
Output:
1 3 5 7 9

Download Projects


Download Programs