You are on page 1of 7

Switch statement -

Introduction

• The C switch case statement allows you to choose from many


statements based on multiple selections by passing control to one of the
case statements within its body.

• The switch statement executes the case


corresponding to the value of the expression.

• The switch statement can include any number of case instances.


Syntax
switch(Character or Integer expression)
{
case condition 1:
// Statement sequence(block 1);
break;
case condition 2:
// Statement sequence(block 2);
break;
default:
// Statement sequence(default block);
}
Break and Default Statement
● You can use the break statement to end processing of a particular case within the
switch statement and to branch to the end of the switch statement. Without break, the
program continues to the next case, executing the statements until a break or the end
of the statement is reached. In some situations, this continuation may be desirable.
● The default statement is executed if no case constant-expression is equal to the value
of switch (expression). If the default statement is omitted, and no case match is
found, none of the statements in the switch body are executed. There can be at most
one default statement. The default Statement need not come at the end; it can
appear anywhere in the body of the switch statement. A case or default label can only
appear inside a switch statement.
Break and Default Statement

● The type of switch expression and case constant- expression must


be integral. The value of each case constant-expression must be
unique within the statement body.

● The case and default labels of the switch statement body are
significant only in the initial test that determines where execution
starts in the statement body. Switch statements can be nested. Any
static variables are initialized before executing into any switch
statements.
SOME SWITCH QUESTIONS

1. Write a C program to print day of week name using switch case.


2. Write a C program print total number of days in a month using switch case.
3. Write a C program to check whether an alphabet is vowel or consonant using
switch case.
4. Write a C program to find maximum between two numbers using switch case.
5. Write a C program to check whether a number is even or odd using switch case.
6. Write a C program to check whether a number is positive, negative or zero using s
witch case.
7. Write a C program to find roots of a quadratic equation using switch case.
8. Write a C program to create Simple Calculator using switch case.
Write a C program to take time between 6:00 AM to 8:00 PM from
the user and prints “Hello good morning” / “Good AfterNoon” /
“Good Evening” / “Good Night” as per entered time.( use Switch
statement somewhere in the program ).

You might also like