Skip to main content

Prgoram transfer control(Jump) statements, break, continue ,goto and exit function

break Statement

The break statement is used with in switch statement to transfer program control out of switch statement after executing a set  of statements mentioned in a particular case.

break statement can also be used in looping statements to transfer program control out of the loop. This will help to terminate a loop when a particular condition is met.

Example:

This program will check whether the given number contain digit 0.It is noted  that when the number contain first 0, we can stop doing the process and print that the number contains 0.There is no point in continuing the loop and hence computation time can be saved.

#include <stdio.h>
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;
                      }
          }
     }

o/p
Enter an integer:23045
number contains zero

continue statement

continue statement is used inside loop to skip the execution of remaining statement and continue the iteration.

Example:

The following program will print numbers from 1 to 15 except the  numbers which are divisible by 3. When the number is divisible by 3, the loop is continued skipping the print statement. The continue statement will make the  program control to jump to the beginning of the loop .

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

o/p
1
2
4
5
7
8
10
11
13
14

Difference Between break and continue in C
S.No.breakcontinue
1.break statement is used in switch and loops.continue statement is used in loops only.
2.When break is encountered the switch or loop execution is immediately stopped.When continue is encountered, the statements after it are skipped and the loop control jump to next iteration.
3.Example:

#include<stdio.h>

main()

{

   int i;

  for(i=0;i<5;++i)

 {

       if(i==3)

               break;

       printf(“%d “,i);

  }

 Output:

0 1 2

Example:

#include<stdio.h>

main(){

                int i;

                for(i=0;i<5;++i)

              {

                     if(i==3)

                             continue;

                  printf(“%d “,i);

                }

   }

 Output:

0 1 2 4



goto statement

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

NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Code can get confusing very quickly when it takes arbitrary paths and jumps from place to place.Any program that uses a goto can be rewritten to avoid them. 
Syntax of goto statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
 
The label is an identifier. When goto statement is encountered, control of the program jumps to label: and starts executing the code from there. The label can be set above or below the goto statement.


Example:
The following will find sum of numbers entered by user till the user enters a negative number.When a negative number is entered, the program control is transferred to the statement with label end, otherwise the number will be added to sum and control will be transferred to the statement labelled begin.

#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
 
exit() is useful for terminating a program upon having discovered some error which prevents the program from continuing to execute normally. It’s prototype as follows, 

void exit(int status);

status argument passed to exit() is returned to O.S. to inform that whether or not program succeeded normally. Notice that this status argument is as same as status main() passes to O.S. Pre defined symbols EXIT_SUCCESS and EXIT_FAILURE are used to pass successful and failure status to O.S respectively. EXIT_SUCCESS is represented with 0 and EXIT_FAILURE with value 1.exit() function is prototyped in stdlib.h header file.
 
Example
the following program will find root of a number.If the given number is -ve the program is terminated with exit(1) function.
 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
  int n;
  printf("enter a number\n");
  scanf("%d",&n);
  if (n<0) {
printf("cant 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

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboar

Arrays in C-single and multi dimensional arrays- list and matrix

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list(vector); A two dimensional array is like a table(matrix). We can have more dimensions. Always, Contiguous (adjacent) memory locations are used to store array elements in memory. Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element. Declaring  Single Dimensional Arrays Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for mentioning dimension of the array.  Dimensions used when declaring arrays in C must be positive integral constants or constant expressions.  In C99, dimensions must still be positive integers, but variables can be used, so long as the variable has a positive value at the time the array is declared. ( Space is allo