C

break, continue and goto statements

The break; continue; and goto; statements are used to alter the normal flow of a program.
Loops perform a set of repetitive task until text expression becomes false but it is sometimes desirable to skip some statement/s inside loop or terminate the loop immediately without checking the test expression. In such cases, break and continue statements are used.

break statement

In C programming, break statement is used with conditional if statement.
The break is used in terminating the loop immediately after it is encountered.
it is also used in switch...case statement. which is explained in next topic.

Syntax:

break;

The break statement can be used in terminating loops like for, while and do...while

break-flow-chart

Example:

//Write a C Program Which use of break statement.
#include<stdio.h>
#include<conio.h>
void main(){
    int num, sum=0;
    int i,n;
    printf("Note: Enter Zero for break loop!\n");
    printf("Enter Number of inputs\n");
     
    scanf("%d",&n);
    for(i=1;i<=n;++i){
        printf("Enter num%d: ",i);
        scanf("%d",&num);
        if(num==0) {
            break;      /*this breaks loop if num == 0 */
            printf("Loop Breaked\n");
        }
        
        sum=sum+num;
    }
    printf("Total is %d",sum);
    getch();
}

Output:

Command Prompt
Note: Enter Zero for break loop!
Enter Number of inputs
5
Enter num1: 5
Enter num2: 10
Enter num3: 0
Loop Breaked
Total is 15

continue statement

It is sometimes desirable to skip some statements inside the loop. In such cases, continue statement is used.

Syntax:

continue;

Just like break, continue is also used with conditional if statement.

Example:

//Write a C Program Which use of continue statment.
#include<stdio.h>
#include<conio.h>
void main(){
    int i, n=20;
    clrscr();
    for(i=1;i<=n;++i){
        if(i % 5 == 0) {
            printf("pass\n");
            continue;      /*this continue the execution of loop if i % 5 == 0 */
        }
        printf("%d\n",i);
    }
    getch();
}

Output:

Command Prompt
1
2
3
4
pass
6
7
8
9
pass
11
12
13
14
pass
16
17
18
19
pass

goto statement

In C programming, goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.

Syntax:

goto label;
.............
.............
.............
label:
statement;

In this syntax, label is an identifier.
When, the control of program reaches to goto statement, the control of the program will jump to the label: and executes the code below it.

goto-statement

Example:

//Write a C Program Which Print 1 To 10 Number Using goto statement.
#include<stdio.h>
#include<conio.h>
void main()
{
    int i=1;
    clrscr();
    count:              //This is Label

    printf("%d\n",i);
    i++;
    if(i<=10) {
        goto count;     //This jumps to label "count:"
    }
    getch();
}

Output:

Command Prompt
1
2
3
4
5
6
7
8
9
10
Note:

Though goto statement is included in ANSI standard of C, use of goto statement should be reduced as much as possible in a program.

Reasons to avoid goto statement

  • Though, using goto statement give power to jump to any part of program, using goto statement makes the logic of the program complex and tangled.
  • In modern programming, goto statement is considered a harmful construct and a bad programming practice.
  • The goto statement can be replaced in most of C program with the use of break and continue statements.
  • In fact, any program in C programming can be perfectly written without the use of goto statement.
  • All programmer should try to avoid goto statement as possible as they can.



Subscribe us on Youtube

Share This Page on

Questions


D.Manideep | 02-Nov-2017 08:40:19 pm

To write a c program in goto statement with flow chart?


sankalp | 28-Nov-2017 11:39:05 pm

the output is wrong in goto statement example. it should printf from 1 to 9, not 1 to 10. please correct it.


Soumya Jan rout | 27-Dec-2017 06:56:57 pm

How to shutdown windows 8.1 pc by using c program


Ask Question