Professional Documents
Culture Documents
Lecture 8
Lecture 8
Topics Covered
SKASC 1
18CSS26 & ARTIFICIAL INTELLIGENCE
Switch Statement
Switch Statement
• Each of these values should be unique within a switch
statement.
• Block1, block2 are statement list and may contain zero
or more statements.
• The break statement at the end of each block signal the
end of a particular case and causes an exit from the
switch statement, transferring the control to the
statement x following switch.
SKASC 3
18CSS26 & ARTIFICIAL INTELLIGENCE
Switch Statement
• The default is an optional case, when present it will be
executed if the value of the expression does not match
with any of the case values.
• When the switch is executed,
– The value of the expression is compared against the
values and
– If a case is found whose value matches with the value
of the expression,
– Then the block of statements that follows the case are
executed. SKASC 4
18CSS26 & ARTIFICIAL INTELLIGENCE
Switch Statement
Syntax:
switch (expression)
{
case value1 : block1;
break;
case value2: block2;
break;
…….
default:
default block;
break;
}
SKASC 5
18CSS26 & ARTIFICIAL INTELLIGENCE
Switch Statement
SKASC 6
18CSS26 & ARTIFICIAL INTELLIGENCE
default:
printf(“Invalid");
break;
}
return 0;
} SKASC 9
Switch Statement - Example
18CSS26 & ARTIFICIAL INTELLIGENCE
#include <stdio.h>
int main () { Output
/* local variable definition */ Enter a Grade A,B,C,D in CAPS:
char grade; A
printf ("Enter a Grade A,B,C,D in CAPS: \n"); Excellent!
scanf("%c",&grade);
switch(grade) {
Your grade is A
case 'A' :
printf("Excellent!\n" ); Output
break;
Enter a Grade A,B,C,D in CAPS:
case 'B' :
case 'C' : B
printf("Well done\n" ); Well done
break; Your grade is B
case 'D' :
printf("You passed\n" );
break; Output
case 'F' : Enter a Grade A,B,C,D in CAPS:
printf("Better try again\n" );
break;
D
default : You passed
printf("Invalid grade\n" ); Your grade is D
}
printf("Your grade is %c\n", grade );
return 0;
} SKASC 10
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 11
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 12
18CSS26 & ARTIFICIAL INTELLIGENCE
Syntax:
Conditional expression ? expression 1: expression 2;
• The conditional expression is evaluated first.
• If the result is nonzero, expression 1 is evaluated and
is returned as the value of the conditional expression.
• Otherwise expression 2 is evaluated and its value is
returned.
SKASC 13
18CSS26 & ARTIFICIAL INTELLIGENCE
Output
y =2
SKASC 14
18CSS26 & ARTIFICIAL INTELLIGENCE
Output
Enter a integer number:
112 56
Largest number between 112 and 56 is 112.
SKASC 15
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 16
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 17
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 18
18CSS26 & ARTIFICIAL INTELLIGENCE
Backward jump:
– If the label is before the statement goto label; a loop
will be formed and some statements will be executed
repeatedly. Such a jump is known as a backward jump.
Forward jump:
forward jump.
SKASC 19
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 21
18CSS26 & ARTIFICIAL INTELLIGENCE
Summary
SKASC 23