C++ (programming language)
C (programming language)
What is the use of switch statements in C programming?
, Experience of different programming languages
Else If Ladder:
else if statement can be defined as a control statement which controls the statement(s) to be executed on the basis of some conditions. Whenever the else if statement is used, the compiler or interpreter initially checks the condition whether it is true or false and if the condition is found to be true then, the corresponding statements are executed. If the condition is found to be false, it continues checking the next else if statement until the condition comes to be true or the control comes to the end of the else if ladder.
The syntax of else if ladder can be represented as:
else if (condition-2)
else if (condition-3)
else if (condition-4)
else if (condition-n)
- if( condition-1)
- else if (condition-2)
- else if (condition-3)
- else if (condition-4)
- else if (condition-n)
- default statement;
Below is a flowchart that represents else if ladder.
Flowchart for else if ladder
Features of else if ladder:
- It evaluates an expression and then, the code is selected based on the true value of evaluated expression.
- Each else if has its own expression or condition to be evaluated.
- The variable data type used in the expression of else if is either integer or character.
- The decision making of the else if is dependent on zero or non-zero basis.
The switch case statement is similar to the else-if ladder as it provides multiple branching or multi-conditional processing. But, the basic difference between switch case and else if ladder is that the switch case statement tests the value of variable or expression against a series of different cases or values, until a match is found. Then, the block of code with in the match case is executed. If there are no matches found, the optional default case is executed.
The syntax of switch case can be represented as:
- case constant-1
- case constant-2
- case constant-3
- case constant-n
Below is a flowchart that represents switch case.
Flowchart for switch case
Features of switch case:
- The switch case statement evaluates the value of an expression and a block of code is selected on the basis of that evaluated expression.
- Each case refers back to the original expression.
- The data type that can be used in switch expression is integer type only.
- Each case has a break statement.
- The switch case takes decision on the basis of equality.
Difference between switch case and else if ladder:
So, after going through the two control statements in brief, here are the main difference between switch case and else if ladder:
- In else if ladder, the control goes through the every else if statement until it finds true value of the statement or it comes to the end of the else if ladder. In case of switch case, as per the value of the switch, the control jumps to the corresponding case.
- The switch case is more compact than lot of nested else if. So, switch is considered to be more readable.
- The use of break statement in switch is essential but there is no need of use of break in else if ladder.
- The variable data type that can be used in expression of switch is integer only where as in else if ladder accepts integer type as well as character.
- Another difference between switch case and else if ladder is that the switch statement is considered to be less flexible than the else if ladder, because it allows only testing of a single expression against a list of discrete values.
- Since the compiler is capable of optimizing the switch statement, they are generally considered to be more efficient. Each case in switch statement is independent of the previous one. In case of else if ladder, the code needs to be processed in the order determined by the programmer.
- Switch case statement work on the basis of equality operator whereas else if ladder works on the basis of true false( zero/non-zero) basis.
sponsored by DigitalOcean
, Passionate Programmer
Each value in that list of possible value is called case.
When the value given in input matches the value in the case statement, the block of code below case gets executed until it reaches the break statement.
Break is optional. If break statement is not given, the next case statement (if any) will also get executed.
for example, consider the following code:
int main ()
char grade = B;
case A :
case B :
printf("Well done\n" );
case C :
case D :
printf("You passed\n" );
case F :
printf("Better try again\n" );
printf("Invalid grade\n" );
printf("Your grade is %c\n", grade );
The output of the above program is :
Your grade is B
The value of the variable passed to the switch statement is checked against the case statements.
Case B matches. Hence the printf() executed. (Well Done – printed).
As there is no break statement, the next case C also executed. (Good – printed). As there is break statement, the statement after the switch is executed. Therefore Your grade is B – printed.
An if statement can be followed by an optional else if…else statement, which is very useful to test various conditions. Once the condition is true, other conditions are not validated.
In switch case, mostly constant values are given in the condition while in if..else mostly boolean expressions are given.
int main ()
printf("Value of a is greater than 100");
printf("Value of b is 20");
printf("Value of a is less than 100");
printf("Value of a is %d", a);
Output of above program is:
Value of b is 20
Value of a is 10
The first if condition fails, so second if is checked.
The condition is true, so the statements inside that if gets executed.
The next if statements are not executed even if the condition is true.
sponsored by MongoDB
, Software Developer (2010-present)
Switch Statement in C language
switch statement in c
, C Programmer
- The complexity of the program increases as the number of constants to compare increases. Switch statement provides a way to write easy to manage and readable code.
- Switch statement  provides a default case handler when no case matches.
- We can nest switch statements like if else statements.
 Switch Statement in C Programming
, works at Decline To State
In the example I have used Java GUI and have created a text field that user will enter a number 1 or 2 so if he enters 1hello will be printed and if he types 2 hi will be printed in the text field.
If is also used for the same thing, but there are advantages – you can enter range.
Eg- 80-90 etc.
And switch cases can only be applied on integer values only. String value cannot be used and I think it is very important data type.
So switch case has its limitations, and if-else case can overcome it.
I hope it helps , if it doesnt shoot a reply.
Switch statement is a control statement that allows us to choose only one choice among the many given choices. The expression in switch evaluates to return an integral value, which is then compared to the values present in different cases. It executes that block of code which matches the case value. If there is no match, then default block is executed.
- Q & A Forum
- HTML Course
- Curious NEW
- Sign in
- Basics of C Language
- Overview of C
- Features of C
- My First C program
- Compile and Run C program
- C Syntax Rules
- Keywords and Identifier
- Operators in C Language
- Data Types in C
- Variables in C
- C Input / Output
- Decision Making
- Switch Statement
- string and character array
- Storage classes
- Functions in C
- Introduction to Functions
- Types of Functions and Recursion
- Types of Function calls
- Passing Array to function
- Structures in C
- All about Structures
- Pointers in C
- Pointers concept
- Declaring and initializing pointer
- Pointer to Pointer
- Pointer to Array
- Pointer to Structure
- Pointer Arithmetic
- Pointer with Functions
- Advanced Topics in C
- File Input / Output
- Error Handling
- Dynamic memory allocation
- Command line argument
- C Programs
- 100+ C Programs with explanation and output
Switch statement in C
When you want to solve multiple option type problems, for example: Menu like program, where one value is associated with each option and you need to choose only one at a time, then,
switch statement is used.
Switch statement is a control statement that allows us to choose only one choice among the many given choices. The expression in
switch evaluates to return an integral value, which is then compared to the values present in different cases. It executes that block of code which matches the case value. If there is no match, then default block is executed(if present). The general form of
switch statement is,
switch(expression) case value-1: block-1; break; case value-2: block-2; break; case value-3: block-3; break; case value-4: block-4; break; default: default-block; break;
Rules for using
- The expression (after switch keyword) must yield an integer value i.e the expression should be an integer or a variable or an expression that evaluates to an integer.
- The case label values must be unique.
- The case label must end with a colon(:)
- The next line, after the case statement, can be any valid C statement.
Points to Remember
- We don’t use those expressions to evaluate switch case, which may return floating point values or strings or characters.
breakstatements are used to exit the switch block. It isn’t necessary to use
breakafter each block, but if you do not use it, then all the consecutive blocks of code will get executed after the matching block.
int i = 1; switch(i) case 1: printf("A"); // No break case 2: printf("B"); // No break case 3: printf("C"); break;
A B C
The output was supposed to be only A because only the first case matches, but as there is no
breakstatement after that block, the next blocks are executed too, until it a
breakstatement in encountered or the execution reaches the end of the
- default case is executed when none of the mentioned case matches the
switchexpression. The default case can be placed anywhere in the
switchcase. Even if we don’t include the default case,
- Nesting of
switchstatements are allowed, which means you can have
switchstatements inside another
switchblock. However, nested
switchstatements should be avoided as it makes the program more complex and less readable.
#include<stdio.h> void main( ) int a, b, c, choice; while(choice != 3) printf("\n 1. Press 1 for addition"); printf("\n 2. Press 2 for subtraction"); printf("\n Enter your choice"); scanf("%d", &choice); switch(choice) case 1: printf("Enter 2 numbers"); scanf("%d%d", &a, &b); c = a + b; printf("%d", c); break; case 2: printf("Enter 2 numbers"); scanf("%d%d", &a, &b); c = a - b; printf("%d", c); break; default: printf("you have passed a wrong key"); printf("\n press any key to continue");
ifstatements can evaluate
switchstatements cannot evaluate
ifstatement can evaluate relational operators.
switchstatement cannot evaluate relational operators i.e they are not allowed in
- ← Prev
- Next →
What is Studytonight?
DBMS & SQL
Learn to Code
Java Interview Question
C++ Interview Question
OS Interview Question
DBMS Interview Question
© 2018 Studytonight. All rights reserved.