Program Transfer Control Statements- break, continue, goto, exit

break Statement

The break statement is used:

  • In a switch statement to transfer program control out of the switch after executing a particular case.

  • In loops to immediately terminate the loop when a specific condition is met.

This helps save computation time by stopping unnecessary iterations.


Example: Check whether a number contains digit 0

This program stops checking as soon as the first 0 is found in the number.

#include <stdio.h> int main() { int n, digit; printf("Enter an integer: "); scanf("%d", &n); while (n != 0) { digit = n % 10; n = n / 10; if (digit == 0) { printf("number contains 0\n"); break; } } return 0; }

Output

Enter an integer: 23045 number contains 0

continue Statement

The continue statement is used inside loops to:

  • Skip the remaining statements in the current iteration

  • Move immediately to the next iteration of the loop


Example: Print numbers from 1 to 15 except those divisible by 3

When a number is divisible by 3, the loop skips printing it.

#include <stdio.h> int main() { int i; for (i = 1; i <= 15; i++) { if (i % 3 == 0) continue; printf("%d\n", i); } return 0; }

Output

1 2 4 5 7 8 10 11 13 14

Comparison Between break and continue Statement in C

Featurebreak Statementcontinue Statement
PurposeUsed to terminate the loop or switch immediately.Used to skip the remaining statements in the current iteration and continue with the next iteration.
Where it is usedUsed in loops (for, while, do-while) and switch statements.Used in loops only (for, while, do-while).
Effect on loopStops the entire loop execution.Does not stop the loop; only skips the current iteration.
Control flowControl moves outside the loop or switch.Control moves to the beginning of the loop for the next iteration.
Use caseWhen a condition is met and no further looping is needed.When a condition is met and you want to skip just that iteration.
Execution after statementStatements after break inside the loop are not executed, and the loop ends.Statements after continue inside the loop are not executed, but the loop continues.


Simple Example
// break example 
for(int i=1;i<=5;i++){ 
 if(i==3) break; printf("%d ",i); 
// Output: 1 2 
 // continue example 
for(int i=1;i<=5;i++)
{ if(i==3) continue; printf("%d ",i); 

// Output: 1 2 4 5






goto Statement in C

Definition

A goto statement in C programming provides an unconditional jump from the goto statement to a labeled statement within the same function.


Note

  • The use of the goto statement is highly discouraged in any programming language.

  • It makes the control flow of a program difficult to trace, causing the program to become:

    • Hard to understand

    • Hard to modify

  • Code can quickly become confusing when it jumps arbitrarily from one place to another.

  • Any program that uses goto can usually be rewritten without it.


Syntax of goto Statement

goto label; /* ... statements ... */ /* ... statements ... */ label: statement;

Explanation

  • A label is an identifier followed by a colon (:).

  • When the program encounters a goto label;, control immediately jumps to the labeled statement.

  • The label can be placed:

    • Above the goto statement, or

    • Below the goto statement.



Example: Using goto to Sum Numbers

The following program finds the sum of numbers entered by the user until a negative number is entered.

  • When a negative number is entered, program control jumps to the label end.

  • Otherwise, the number is added to sum, and control jumps back to begin to continue input.

#include <stdio.h> main() { int number, sum = 0; begin: printf("Enter numbers, give -ve number to stop\n"); scanf("%d", &number); if (number < 0) goto end; sum = sum + number; goto begin; end: printf("sum = %d\n", sum); }

exit() Function in C

The exit() function is used to terminate a program immediately, especially when an error prevents normal execution.

Prototype

void exit(int status);

Explanation

  • The status value is returned to the Operating System.

  • It indicates whether the program ended:

    • SuccessfullyEXIT_SUCCESS (value 0)

    • With failure/errorEXIT_FAILURE (value 1)

  • The exit() function is declared in the stdlib.h header file.


Example: Using exit() to Stop Program for Negative Input

The following program finds the square root of a number.
If the number entered is negative, the program prints a message and terminates using exit(1).

#include <stdio.h> #include <stdlib.h> #include <math.h> main() { int n; printf("Enter a number\n"); scanf("%d", &n); if (n < 0) { printf("Can't find root of -ve numbers\n"); printf("Terminating the program\n"); exit(1); } printf("sqrt = %f\n", sqrt(n)); }

Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Single and Multi Dimensional Arrays