switch case in c programming questions
Hello
friends,
Today we
will read what is switch case in c programming, how to use it in c programming,
and will also see its excellent example. In the previous class, we had learned
about the while-loop in c programming. If you have not read it yet, then please first read it,
then read this post.
Let’s start
What is switch case in c programming
switch Statement: When we use a lot of if conditions in a c program,
the program becomes very complex. Because of this, understanding and reading
become very difficult. To avoid this difficulty, we use another Control A statement which is a switch. if Condition is a Two-way Condition Statement, the
switch is a Multi-way Condition Statement. It works exactly as if-else
statement.
Syntax of switch case in c language
Switch(expression)
{
Case1 constant: code1; break;
Case2 constant: code2; break;
Case3 constant: code3; break;
…
…
Case n constant: code n; break;
default case; code; break;
}
sequential statement a
s
Explanation: -
Function -
This structure contains case1, case2 …… ...case n expression or Variable, which
is called case label. Colon (:) infusion is necessary after these. All these
values should be different. code 1, code 2, ……… code n is a group of n
Statements. The middle brackets are not needed even if there is more than one
statement in the groups of these statements. Even if these brackets are
placed, the C Compiler does not cause any problems.
For
execution of switch statement, first the value of the variable or expression is
compared one by one with the case1, case2 …… case n in sequence and wherever
these values are found, execution of the statements written according to the
case label it happens. In this structure, break; is written after all the code.
It marks the end of a group of Statements and after the execution of the code moves
the program control out of the switch statement to a sequential statement a. If
this break; is not written, even after execution of the code, the program
control switch remains inside the code and continues to execute the further
statements. Until it finds a break; statement, it keeps executing the code of
all the labels.
The default
case label in the structure provided for the switch statement is an Optional Label.
If it occurs in the switch statement and the value of Expression or Variable
does not match any of the statements given in the switch statement, then
Program Control goes to the default case label and executes the given statement
under it. We can put the switch case labels in any order, that is, the case of
the fourth position in the first place, the case in the first place in the
third place. You can also place the default statement at any place.
Let’s see some best examples
Example 1:
Write a menu
driven program with the following options:
·
Addition
·
Multiplication
·
Subtraction
·
Division
#include
<stdio.h> #include<conio.h> void main() { float x,y,z; int choice; printf("\n1.
click 1 for Addition");
printf("\n2. click 2 for multiplication");
printf("\n3. click 3 for subtraction");
printf("\n4. click 4 for division");
printf("\n Enter your choice");
scanf("%d",&choice); switch(choice) { case 1: printf("Enter any two
numbers");
scanf("%f%f",&x,&y); z=x+y; printf("your sum is %f",z); break; case 2: printf("Enter any two
numbers");
scanf("%f%f",&x,&y); z=x*y; printf("your multiple is
%f",z); break; case 3: printf("Enter any two
numbers");
scanf("%f%f",&x,&y); z=x-y; printf("your sum is %f",z); break; case 4: printf("Enter any two
numbers");
scanf("%f%f",&x,&y); z=x/y; printf("your sum is %f",z); break; default : printf("your choice is
invelid"); } getch(); } |
only for mobile user
sorry for this blank
next example is given below
Example 2:
In this coding, Write a menu driven program with the following options:
- ·
To
find odd-even number
- ·
To
find first n natural numbers
- ·
To
find numbers are positive or negative
#include
<stdio.h> #include<conio.h> void main() { int x,y,z; int choice; printf("\n1. click 1 To find odd-even
numbers"); printf("\n2. click 2 To find first n
natural numbers "); printf("\n3. click 3 To find numbers
are positive or negative"); printf("\n Enter your choice"); scanf("%d",&choice); switch(choice) { case 1: printf("Enter your
number"); scanf("%d",&y); x=y/2; (x=y)?printf("the number is
even"): printf("The number is
odd"); break; case 2: printf("Enter your
number"); scanf("%d",&x); for(y=1;y<=x;y++) { printf("%d",y); } break; case 3: printf("Enter your
number"); scanf("%d",&y); (0<=y)?printf("the number is
positive"): printf("The number is
negative"); break; default : printf("your choice is
invelid"); } getch(); } |
Output
When you choose
1 option then it will tell you which number is odd and which number is even.
When you choose
2 option then it will tell you the first n natural numbers.
When you choose
3 option then it will tell you which number is positive and which number is negative.
|
Example 3.
In this program, I set a password and an ID. When I enter the correct iD then it asks for
password and when I type the correct password then it prints welcome dear programmer.
#include
<stdio.h> #include<conio.h> int main() { int ID = 500; int password = 000; printf("Plese Enter Your ID:\n
"); scanf("%d", & ID); switch (ID) { case 500: printf("Enter your
password:\n "); scanf("%d", &
password); switch (password) { case 000: printf("Welcome
Dear Programmer\n"); break; default:
printf("incorrect password"); break; } break; default: printf("incorrect ID"); break; } } |
Output
Plese Enter Your ID: 100 Enter your password: 111 Welcome Dear Programmer |
If you want to check all the coding online then you can go on this link (https://www.onlinegdb.com/online_c_compiler)
I hope, you
understand the use of switch case in c
programming very well. If you have any question related to switch case then
you can ask in the comment
Also, Read
(https://grapsvs.blogspot.com/2020/08/for-loop-in-c-do-while-loop-in-c.html)
(https://grapsvs.blogspot.com/2020/07/while-loop-in-c-programming-example.html)
1 Comments
Mast he
ReplyDeletePlease do not enter any spam link in the comment box and please follo me.