Note on The Switch Statement

  • Note
  • Things to remember

INTRODUCTION

The switch statement causes a particular group of statements to be chosen from several available groups. The switch statement is useful when a variable is to compare with different constants, and in case it is equal to a constant, a set of the statement are to be executed. The constants used in the case can be of char or int data type only.

Syntax:

Switch (n)

{

Case 1:

Statement 1;

Statement 2;

Break;

Case 2:

Statement 1;

Statement 2;

Break;

Case 3:

Statement 1;

Statement 2;

Break;

…………

………….

default:

statement ;

statement;

}

Note: If you are using a character in place of n in above syntax example, the case should be enclosed in a single quote as case ‘1’ :, case ‘2’:, etc.

# Write a program to accept choice to find the cost price for traveling from Kathmandu to different destination.

#include <stdio.h>

#include <conio.h>

void main ()

{

int choice, num;

float rate, total=0;

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

printf(“\n 1. Pokhara “);

printf(“\n 2. Biratnagar “);

printf(“\n 3. Birgunj“);

printf(“\n 4. Exit“);

scanf(“%d”, &choice);

switch (choice);

{

case1:

rate =350;

printf(“\n Number of persons: “);

scanf(“%d”,&num);

total = rate * num;

printf(“\n The cost is : %5.2f”, total);

break;

case 2;

rate =300;

printf(“\n Number of persons : “);

scanf(“%d”, &num);

total = rate *num;

printf(“\n The cost is %5.2f”, total);

break;

case 3;

rate =250;

printf(“\n Number of persons : “);

scanf(“%d”, &num);

total = rate *num;

printf(“\n The cost is %5.2f”, total);

break;

case 4:

break;

default:

puts (“Thank you”);

break;

}

Design a program to input two integer numbers from keyboard. On the basis of choice, find out product, sum and difference.

#include <stdio.h>

#include <conio.h>

main ()

{

clrscr ();

/* use of case, switch and getch () */

int a, b, t=0;

char ch;

printf(“\n Enter first number :”);

scanf(“%d”,&a);

printf(“\n Enter second number :”);

scanf(“%d”,&b);

again:

printf(“\n Enter choice : 1, 2 or 3”);

ch = getch ();

switch (ch)

{

printf(“\n Enter choice : 1, 2 or 3”);

ch = getch ();

switch (ch)

{

case ‘1’:

t=a*b;

printf(“\n Product is: %d”,t);

break;

case ‘2’:

t=a+b;

printf(“\n Sum is : %d,t);

break;

case ‘3’ :

t=a-b;

printf(“\n Difference is: %d”, t);

break;

default:

printf(“\n Wrong entry.”);

goto again;

}

}

# Write a program to find area and perimeter of a triangle.

#include <stdio.h>

#include <math.h>

main ()

{

float a, b, c, base, height, area, s, p;

int k;

printf(“\n Enter 1 for 3 sides of triangle & 2 for height and base of triangle :”);

scanf(“%d”, &k);

switch (k)

{

case 1:

printf(“\n Enter 3 sides a, b & c:”);

scanf(“ %f %f, &a, &b, &c );

p = a+b+c;

s=p/2;

area = sqrt ((double) (s* (s-a)* (s-b)* (s-c) ) );

printf( “\n Perimeter = %6.2f \n”,p);

printf(“\n Area = %7.2f\n”, area);

break;

case 2:

printf(“\n Enter height and base of triangle:”);

scanf(“ %f %f, &height, &base);

area = (base*height) /2;

printf(“\n Area = %7.2f\n”, area);

break;

}

Looping Constructs

A loop is structured construct which repeats for given number of times. Looping makes a program more flexible. The number of times the loop passes is known as iteration. The iteration is initialized at the beginning whereas the last value of the loop is known as the sentinel value.

The loop repeats till the sentinel value. For the construction of the loop, the loop variable should be initialized, incremented or decremented and assigned the last value of the loop. Basically, there are two types of loops.

  1. Finite Loop and
  2. Infinite Loop

A finite loop has a certain number of repetitions. It stops when the last value is reached. But, the infinite loop does not stop because it does not have the last value assigned. The infinite loop, in this case, is useless. But, some system programmers use infinite loops for system delay and operational delay inside the processor.

A loop consists of two segments known as control statement and the body of the loop. There are three kinds of loops. They are:

  1. For loop
  2. While loop
  3. do …while loop

For loop: Before using for loop, let use to examine the structure of statement.

Syntax:

for ( initialization expression; test expression; updated expression)

{

statement 1;

statement 2;

………………...3;

…………………..;

statement n;

}

Initialization expression is the expression which initiates the first value of the loop.

Test expression determines the last value of the expression.

The updated expression is the increment or decrement from the initialization so that the last value or test expression could be reached.

The infinite loop, where beginning and ending are not expressed there is no initialization, text and update expressions.

# Write a program to find sum of given three numbers using for loop construct.

/* Program to find sum of given three numbers using for loop */

#include <stdio.h>

#include <conio.h>

int main ()

{

int i=1, a[3];

float sum=0;

float avg=0;

for (i=1; i<=3; i++)

{

printf(“\n Enter %d number: “i);

scanf (“%d, &a [i];

}

avg=sum/3;

printf(“Sum of given numbers is : %4.2f”, sum);

printf(“\n Average is %4.2f”, avg);

}

# Write a program to print first ten even numbers.

#include <stdio.h>

main ()

{

int n=2,c =1;

for (c=1; c<=10; c++)

{

printf(“%d\t”,n);

n+=2;

}

}

# Write a program to find the factorial of a given number.

/* finding factorial of a number */

#include <stdio.h>

#include <math.h>

void main ()

{

int n, i, fact;

printf(“\n Enter a number:”);

scanf(“%d”,&n);

if n= =0)

{

printf(“\n Factorial of %d is 1”,n);

}

else

{

fact =1;

for (i=1; i<n; i++)

{

fact =fact *I;

}

printf(“Factorial of M is ]”, n, fact);)

}

# Write a program to print first 10 number of Fibonacci series of order 2.

#include <stdio.h>

#include <conio.h>

#include <math.h>

void main ()

{

int a=1, b=1, c-=0, d;

for (d=1; d<=10; d++)

{

printf(“%d”,a);

c=a+b;

a=b;

b=c;

}

Looping With while statement

The while statement is used to carry out looping operation, in which group of statements is executed repeatedly until some condition has been satisfied.

Syntax:

while (expression)

{

statement 1;

statement 2;

………………….;

statement n;

}

The statement will continue as long as the expression is true. The statement can be simple or compound though it is usually compound statement. It should be noted that within the compound statement, the expression should be changed for stopping the loop. Let us examine a program below.

# Write a program to print the numbers is prime number. A prime number is divisible by 1 and itself.

/* check prime number */

#include <stdio.h>

#include <conio.h>

main ()

{

int a, b=2, r, n;

printf(“\n Enter a number :”);

scanf(“%d”, &a);

n =a;

if (a==1 || a==2 || a==3)

printf(“The num %d is prime number”, n);

while (b!=(a-1))

{

aa:

r=a%b;

b=b+1;

if (r==0)

{

printf(“\n The num %d is not a prime number “,);

break;

}

else if (b!= (a-1))

{

goto aa;

}

else

printf(“\n The num %d is a prime number”,n);

}

}

# Write a program to check whether given number is Armstrong number. Armstrong number is the number which is equal to the sum of the cubes of its individual digits.

/* Check for Armstrong number */

#include <stdio.h>

void main ()

{

int a, n, r, sum=0;

printf(“\n Enter a number:”);

scanf(“%d”,&a);

n=a;

while (n!=0)

{

r=n;

if (r!=0)

{

sum +=r*r*r;

}

n =n/10;

}

printf(“\n Sum of cubes of given digits = %d”, sum);

if (sum==a)

{

printf(“\n Given number %d is equal to sum of cubes of individual digits %d”, a, sum);

printf(“\n The number is Armstrong number”);

}

else

printf(“\n The number is not a an Armstrong number”);

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 171-181.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd

  1. The switch statement is useful when a variable is too compared with different constants, and in case it is equal to a constant, a set of a statement are to be executed.
  2. A loop is structured construct which repeats for given number of times. Looping makes a program more flexible.
  3. A finite loop has a certain number of repetitions. It stops when the last value is reached. But, the infinite loop does not stop because; it does not have the last value assigned.
  4. The while statement is used to carry out looping operation, in which group of statements is executed repeatedly until some condition has been satisfied. 
.

Very Short Questions

0%

DISCUSSIONS ABOUT THIS NOTE

No discussion on this note yet. Be first to comment on this note