P. 1
Handout_Problem_Solving_and_C_Programming_v1[1].0

Handout_Problem_Solving_and_C_Programming_v1[1].0

|Views: 3,122|Likes:
Published by karthikJegan

More info:

Published by: karthikJegan on Mar 05, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

02/07/2013

pdf

text

original

This is a conditional control statement that allows some particular group of statements to be
chosen from several available groups. It is a multi-way conditional statement generalizing the ‘if-
else’ statement. A switch statement allows a single variable to be compared with several possible
case labels, which are represented by constant values. If the variable matches with one of the
constants, then an execution jump is made to that point. A case label can not appear more than
once and there can only be one default expression.

General Form:

switch (expression)

{

case item1: statement 1;

break;

case item2: statement 2;

break;

case itemn: statement n;

break;

default : statement;

}

Expression in the switch statement, must be an integer valued expression. Expression may be a
constant value, variable, array variable, pointer variable, relational expression, logical expression,
and/or arithmetic expression. Items which represent the case labels must be an integer constant or
character constant. Default case is optional and if specified, default statements will be executed, if
there is no match for the case labels.

The break is needed to terminate the switch after the execution of particular choice. Otherwise the
next cases get evaluated.

Page 45

©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected

Problem Solving and C Programming

Example 5.7

switch (op)

{

case ‘+’:

c=a+b;

break;

case ‘-’:

c=a-b;

break;

case ‘*’:

c=a*b;

break;

case ‘/’:

c=a/b;

break;

default:

printf (“Invalid operator”);

}

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->