Explain syntax of switch case statement in ‘C’ language. Also compare switch case with if else statement. 5m Dec 2009

By | August 2, 2016

Explain the syntax of switch case statement in ‘C’ language. Also compare the performance of switch case with if else statement. 5m Dec 2009

 

The Switch Statement

Its objective is to check several possible constant values for an expression, something similar to what we had studied in the earlier sections, with the linking of several if and else if statements. When the actions to be taken depending on the value of control variable, are large in number, then the use of control structure Nested if…else makes the program complex.

There switch statement can be used. Its form is the following:

switch (expression){

case expression 1:

block of instructions 1

break;

case expression 2:

block of instructions 2

break;

.

.

default:

default block of instructions

}

 

It works in the following way: switch evaluates expression and checks if it is equivalent to expression1. If it is, it executes block of instructions 1 until it finds the break keyword, moment at finds the control will go to the end of the switch. If expression was not equal to expression 1 it will check whether expression is equivalent to expression 2. If it is, it will execute block of instructions 2 until it finds the break keyword.

Finally, if the value of expression has not matched any of the previously specified constants (you may specify as many case statements as values you want to check), the program will execute the instructions included in the default: section, if it exists, as it is an optional statement.

 

Let us consider a program to illustrate Switch statement,

Example 5.5

Write a program that performs the following, depending upon the choice selected by the user.

  1. calculate the square of number if choice is 1
  2. calculate the cube of number if choice is 2 and 4
  3. calculate the cube of the given number if choice is 3
  4. otherwise print the number as it is

 

main()

{

int choice,n;

printf(“\n Enter any number:\n “);

scanf(“%d”,&n);

printf(“Choice is as follows:\n\n”);

printf(“1. To find square of the number\n”);

printf(“2. To find square-root of the number\n”);

printf(“3. To find cube of a number\n”);

printf(“4. To find the square-root of the number\n\n”);

printf(“Enter your choice:\n”);

scanf(“%d”,&choice);

switch (choice)

{

case 1 : printf(“The square of the number is %d\n”,n*n);

break;

case 2 :

case 4 : printf(“The square-root of the given number is %f”,sqrt(n));

break;

case 3: printf(“ The cube of the given number is %d”,n*n*n);

default : printf(“The number you had given is %d”,n);

break;

}

}

 

OUTPUT

Enter any number:

4

Choice is as follows:

1. To find square of the number

2. To find square-root of the number\n”);

3. To find cube of a number

4. To find the square-root of the number

Enter your choice:

2

The square-root of the given number is 2

 

In a C program, a decision causes a one-time jump to a different part of the program, depending on the value of an expression. Decisions in C can be made in several ways. The most important is with the if…else statement, which chooses between two alternatives. This statement can be used without the else, as a simple if statement.

Another decision control statement, switch, creates branches for multiple alternative sections of code, depending on the value of a single variable.

 

(c) Draw a flowchart and write a program in ‘C’ to convert a decimal number to its octal equivalent. 10m

Similar Solved program can be found on this link http://cssimplified.com/c-programming/design-an-algorithm-and-draw-corresponding-flowchart-to-convert-a-decimal-number-to-its-hexadecimal-equivalent-10m-dec2005