December 07, 2018

Other Decision making Statements in C

I have previously written about if, nested if, else if statements in C. They fall under decision making statements in C programming. In this article, I've discussed goto and switch statements in C programming.

Other Decision making Statements in C
Other Decision making Statements in C

Overview:


Nature of goto statement in C

In certain programs, the flow of execution depends on statements which are not based on conditions that can control the flow. C supports the goto statement which helps to branch unconditionally within a program from one point to other. However, this statement is not much used, that doesn't mean it is never used.

Programmers with the help of goto statement can change the sequence of execution of a C program by shifting the control to a different part of that same program.

Syntax of goto statement

There are different ways of putting up the goto statement. We'll get to them one by one.

(a) General Structure

goto label;

What is a label?It is an identifier required by goto statement to identity the site for branching. Usually followed by a colon and is put immediately before the statement where the control needs to be transferred unconditionally.

(b) Forward jump goto statement

goto label;

 - - -- -   -
 - - - - - - - -

label:

statement - X;
/*Syntax of forward jump of goto statement */

(c) Backward jump goto statement

label:

 - - -- -   -
 - - - - - - - -

goto label;
/*Syntax of backward jump of goto statement */

Flowchart of goto statement:

Goto statement flowchart in C
Goto statement flowchart in C


Sample program using goto statement in C:

#include <stdio.h>
int main()
{
   int sum=0;
   for(int i = 0; i<=10; i++)
 {
    sum = sum+i;
    if(i==5)
    {
      goto addition;
    }
 }
   addition:
   printf("%d", sum);
   return 0;
}

Output:


Output of goto statement
Output of goto statement

Nature of switch statement in C

This is an advanced version of goto statement but unlike goto statement, in switch statement the transfer is choice based, depending on value input by user.

Syntax of switch statement

The syntax of switch statement is as follows:

switch(variable)
{
case 1:
   //codes under first case
break;

case n:
   //codes under nth case
break;

default:
   //default set of codes
break;
}

You'll notice at the end of each case we have a break; statement. But why?

It is necessary to insert a break statement because if a programmer does not use the break statement, all consecutive blocks of codes will be executed from every case onwards after matching the case block. To avoid such flaw, break statement is added.

Flowchart of switch statement:

Switch Break Statement Flowchart in C
Switch-Break Flowchart in C


Sample program using switch statement in C:

#include<stdio.h>
void main()
{
    int n;
    printf("Please enter a no between 1 and 5: ");
    scanf("%d",&n);    
    switch(a)
    {
    case 1:
    printf("Chosen I");
    break;    
    case 2:
    printf("Chosen II");
    break;
    case 3:
    printf("Chosen III");
    break;
    case 4:
    printf("Chosen IV");
    break;
    case 5:
    printf("Chosen V");
    break;
    default :
    printf("err. Enter a no between 1 and 5");
    break;
    }
}

Output:


Output of switch statement
Output of switch statement


All programs written in this post are compiled online.

Books I Prefer:



  

I hope this article was helpful to understand the types and nature of goto and switch-break statements in C! Comment below, if you've got any question. Head back soon for another interesting article on C Programming.