Computer Programming
and Utilization
NAME:RAJ PAREKH
BRANCH:MECHANICAL, SEM-2
ENROLLMENT NO-140990119029
TOPIC:
SWITCH CASE STATEMENT
BREAK STATEMENT
GO TO STATEMENT
Multiple Selection:
The switch Statement
value1
action 1
value2
action 2
value3
action 3
value4
action 4
multiway
expression
Multiple Selection:
The switch Statement
Syntax:
switch (<selector expression>) {
case <label1> : <sequence of statements>;
break;
case <label2> : <sequence of statements>;
break;
case <labeln> : <sequence of statements>;
break;
default : <sequence of statements>;
}
Multiple Selection:
The switch Statement
Meaning:
 Evaluate selector expression.
 The selector expression can only be: a bool, an
integer, an enum constant, or a char.
 Match case label.
 Execute sequence of statements of matching label.
 If break encountered,
go to end of the switch statement.
 Otherwise continue execution.
switch ( day )
{
case0:printf(“Sunday”);
break;
case1:printf(“nMonday”);
break;
case2:printf(“nTuesday”);
break;
case3:printf(“nWednesday”);
break;
case4:printf(“nThursday”);
break;
case5:printf(“nFriday”);
break;
case6:printf(“nSaturday”);
break;
default:printf(“nError -- invalid day.”);
break;
Is this structure more
efficient than the
equivalent nested if-else
structure?
The break statement
 Syntax:
Effect:
break;
• When the break statement is executed inside a loop-
statement, the loop-statement is terminated immediately
• The execution of the program will continue with the
statement following the loop-statement
The break statement (cont.)
 Schematically:
 Syntax:
The go to statement
Sample: …………..; //label
……………….;
If()
{
………………;
break;
}
else
goto sample; //goto statement
Go to Example
 #include<stdio.h>
main()
{
aa:goto cc;
hum:printf(“the limit”);
goto end;
switch Example”);
goto dd;
cc:printf(“n n The”);
goto bb;
dd:printf(“tis”);
goto hum;
end:printf(“for the programmer”);

Switch statement, break statement, go to statement

  • 1.
    Computer Programming and Utilization NAME:RAJPAREKH BRANCH:MECHANICAL, SEM-2 ENROLLMENT NO-140990119029 TOPIC: SWITCH CASE STATEMENT BREAK STATEMENT GO TO STATEMENT
  • 2.
    Multiple Selection: The switchStatement value1 action 1 value2 action 2 value3 action 3 value4 action 4 multiway expression
  • 3.
    Multiple Selection: The switchStatement Syntax: switch (<selector expression>) { case <label1> : <sequence of statements>; break; case <label2> : <sequence of statements>; break; case <labeln> : <sequence of statements>; break; default : <sequence of statements>; }
  • 4.
    Multiple Selection: The switchStatement Meaning:  Evaluate selector expression.  The selector expression can only be: a bool, an integer, an enum constant, or a char.  Match case label.  Execute sequence of statements of matching label.  If break encountered, go to end of the switch statement.  Otherwise continue execution.
  • 5.
    switch ( day) { case0:printf(“Sunday”); break; case1:printf(“nMonday”); break; case2:printf(“nTuesday”); break; case3:printf(“nWednesday”); break; case4:printf(“nThursday”); break; case5:printf(“nFriday”); break; case6:printf(“nSaturday”); break; default:printf(“nError -- invalid day.”); break; Is this structure more efficient than the equivalent nested if-else structure?
  • 6.
    The break statement Syntax: Effect: break; • When the break statement is executed inside a loop- statement, the loop-statement is terminated immediately • The execution of the program will continue with the statement following the loop-statement
  • 7.
    The break statement(cont.)  Schematically:
  • 8.
     Syntax: The goto statement Sample: …………..; //label ……………….; If() { ………………; break; } else goto sample; //goto statement
  • 9.
    Go to Example #include<stdio.h> main() { aa:goto cc; hum:printf(“the limit”); goto end; switch Example”); goto dd; cc:printf(“n n The”); goto bb; dd:printf(“tis”); goto hum; end:printf(“for the programmer”);