Switch Case & goto
We often happen to select one out of many
selections….
Which month you were born?
Which place to go for a vacation?
A series of if statements would handle this
A much better option is there… switch-case-default
#include<stdio.h>
main()
{
int month;
printf("Enter a month 1-12\n");
scanf("%d",&month);
if(month==1)
printf("\nYou Entered January");
else if(month==2)
printf("\nYou Entered February");
else if(month==3)
printf("\nYou Entered March");
.
.
}
Decision using switch
switch (integer expression)
{ case constant 1:
do this;
case constant 2:
do this;
default :
do this;
}
integer expression
Any C expression that will yield an integer value
e.g. An integer constant such as 1,2, 3 or an
expression that evaluates to an integer
Entering a switch it evaluates the expression and the
value it gives is then matched against the constant
values that follow the case statements.
When a match is found :
Execute the statements coming under that matching
case;
all the other statements in the following cases;
finally statements under the default too
When a match is not found :
Execute the statements coming under the default
main()
{ int i=2;
I am in case 2
switch (i)
{ case 1: I am in case 3
printf(“I am in case 1\n”); I am in default
case 2:
printf(“I am in case 2\n”);
case 3:
printf(“I am in case 3\n”);
default :
printf(“I am in default\n”);
}
}
Question : Guess the output…
To stop at.. “I am in case 2” Only -> use break
switch (i)
{ case 1: printf(“I
am in case 1\n”);
break; case 2:
printf(“I am in case
2\n”);
break;
case 3:
printf(“I am in case 3\n”);
break;
default :
printf(“I am in default\n”);
break;
}
Useful Tips
~ Use of case integer expression values may not be in
either ascending or descending order
case 1:
case 101:
case 55:
~ Can use char too case `A`:
case `U`:
case `E`:
Tips Cntd…
~ Can mix integer and character
case 1:
case ‘A’:
case 2:
~ No statements in some cases
case `A`:
case `a`:
printf(‘You typed A or a’);
break;
case`B`:
case`b`:
Tips Cntd…
~ No need of braces to enclose multiple statements
~ default is optional
~ More or less the same as if, but not the same
~ disadvantage is …
Cannot use case i <=35;
~ advantage over if is…..
More strucured
GOTO statement
• goto statement is used to branch unconditionally from one point to
another in the program.
• The goto requires a label in order to identify the place where the
branch is to be made.
goto label; label:
----------- -----------
----------- -----------
----------- -----------
label: goto label;
statement; statement;
Avoiding goto
When goto is used many compilers generate a less efficient
code.
Makes the program logic complicated and renders program
unreadable.
If goto to absolutely necessary, then it has to be documented.
Conditional operators
? And :
sometimes called ternary operators
General form is :
expression 1 ? expression 2 : expression 3
If expression 1 is true, then the value returned
will be expression 2 otherwise expression 3
Example - 1
int x, y;
printf(”Enter value for x”);
Same as
scanf(“%d”, &x); if (X>5)
y=(x>5?3:4); y=3;
This statement will store 3 in y if x is else
greater than 5, otherwise it will store
4 in y y=4;
Example - 2
char a;
int y;
printf(”Enter a character”);
scanf(“%d”, &a);
Y = (a >= 65 && a<=95 ? 1 : 0);
This statement will store 1 in y if x is an uppercase
alphabet otherwise it will store 0 in y
Example -3 on Nested conditional operators
int largest, a, b, c;
printf(”Enter values for a, b and c”);
scanf(“%d %d %d”, &a , &b, &c);
largest = (a>b ?(a>c? a:c) : (b>c? b:c));
This statement will store the largest number
out of a, b and c