BRANCHING STATEMENT(Break, Continue and Goto )

Page Contents:

Break statement:
Continue Statement :
goto Statement:

Branching in C Programming Language

  • Branching in C defines the transfer the control to a different place in the program.we will see the operation of each of these. We have already seen the use of break statement in switch case.

Break statement:

  • The break statement neglects the statements after it in the loop and transfers the control outside the loop
    The operation of break statement in each of the loop statements i.e. for, while and do-while statements
    A break is usually associated with an if
    We often come across situations where we want to jump out of a loop instantly, without waiting to get back to the conditional test. the keyword break allows us to do this
for(intitialization;condition;increment/decrement)
{
-
-
break;
-
-
-
}

2. Write a program to accept 10,2-digit numbers from the user and add them.if the user enters a three-digit number stop accepting the number and display the sum.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,total=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("Enter the number:");
scanf("%d",&n);
if(n>99)
break;
total=total+n;
}
printf("Total=%d",total);
getch();
}
OUTPUT:
Enter the number:45
Enter the number:54
Enter the number:22
Enter the number:59
Enter the number:121
Total=180

Continue Statement :

  • The continue statement also neglects the statements after it in the loop and transfers the control back to the starting of the loop for the next iteration
  • The operation of continue statement in each of the loop statements i.e. for, while and do-while statement
  • We want to take the control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed
  • The keyword continue allows us to do this Branching in C
  • When continue is encountered inside any loop, control automatically passes to the beginning of the loop
Syntax for Continue statement
for(intitialization;condition;increment/decrement)
{
-
-
continue;
-
-
} 

Write a program to demonstrate the use of continue statement

#include<stdio.h>
#include<conio.h>
void main()
{
int n,total=0,i;
clrscr();
for(i=1;i<=5;i++)
{
printf("Enter the number");
scanf("%d",&n);
if(n>99)
{
printf("number is greater than 99\n");
i--;
continue;
}
total=total+n;
}
printf("total=%d",total);
getch();
}

OUTPUT:
Enter the number:12
Enter the number:121
Enter the number:99
Enter the number:22
Enter the number:11
Enter the number:89
Enter the number:99
total=233

goto Statement:

  • The goto statement transfers the control to the label specified with the goto statement
  • A label is any name given to a particular part in the program
  • The label is to be followed with a colon(:).
  • Thus it shows that the label can be before or after the goto statement and hence the control can be transferred to earlier or next statements using a goto statement
syntax for goto statement 
for(intitialization;condition;increment/decrement)
{
-
-
goto label1
-
-
label1;
-
}

Write a program to accept a number from users and add them .stop accepting the number if the sum is greater than or equal to 100 and display the sum.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,total=0;
clrscr();
again;
printf("Enter the number");
scanf("%d",&n);
total=total+n;
if(total<100)
goto again;
printf("Total=%d",total);
getch();
}
OUTPUT:
Enter the number:56
Enter the number:12
Enter the number:33
total=101

Translate »