![]() |
What is the purpose of default in the switch case statement? |
Switch Statement in C Language with Example
Switch Statemate: A switch statement permits a variable to be tested for equality against a list of values. each value is termed a case, and so the variable being switched on is checked for each switch case.
A switch statement is used to check the condition. The switch statement is the very most useful statement.
switch statements are multiple condition checks using a switch case.
Some Others Programming Language Switch Statement
👉C++ Switch Statement
👉Java Switch Statement
A switch statement is thought to have an associate integral or enumerated kind, or be of a class kind inside that the class includes one conversion performed to associate integral or enumerated kind.
1. you'll need any variety of case statements within a switch. each case is followed by the value to be compared to and a colon.
2. The constant expression for a case ought to be a similar info kind as a result of the variable inside the switch, and it ought to be relentless or literal.
3. once the variable being switched on is adequate for a case, the statements following that case will execute until a break statement is reached.
4. once a break statement is reached, the switch terminates, and so the flow of management jumps to a sequent line following the switch statement.
5. Not every case should contain a break. If no break appears, the flow of management will fall through to sequent cases until a possibility is reached.
6. A switch statement can have an associate optional default case, that ought to appear at the tip of the switch.
7. The default case could also be used for liberal arts a task once none of the cases is true. No break is needed inside the default case.
Some Important Points of Switch Statement in C Language
2. Switch case value must be of switch expression type only.
3. Switch Statement does not allow variables. A switch case value must be uniquely identified. In case of duplicate value, compile-time error in switch program.
4. Switch Every case statement can have a break statement that can be used in the switch program.
6. Switch Statement case value can have a default value is optional.
Syntax switch Statement
switch(expression)
{
case '1':
//case one execute ;
break;
case '3':
//case three execute;
break;
default:
//no case match is default case is executed;
}
How To Use of switch statement in C?
#include<stdio.h>
int main()
{
int x=2;
switch(2)
{
case 1:
printf("case one executed");
break;
case 2:
printf("case two execuetd");
break;
case 3:
printf("case three executed");
default:
printf("value are not match");
}
}
*****OUTPUT*****
case two executed
Function for bank operation in C language for creating deposit withdrawal and balance void
#include<stdio.h>
int main()
{
float amt,creditamt,debitamt;
char ch;
printf("Enter initial Amount:");
scanf("%f",&amt);
printf("Enter\nC For Credit\n D For Debit\nB For Balance\n");
scanf("\n%c",&ch);
switch(ch)
{
case 'c':
printf("Enter Credit Amount:");
scanf("%f",&creditamt);
amt=amt+creditamt;
printf("New Amount:%f",amt);
break;
case 'd':
printf("Enter Debit Amount:");
scanf("%f",&debitamt);
if(amt>=debitamt)
{
amt=amt-debitamt;
printf("New Amount:%f",amt);
}
else
{
printf("Insufficient Amount");
}
break;
case 'b':
printf("Amount in Your Account:%f",amt);
break;
default:
printf("Invalid input!!");
}
}
*****OUTPUT*****
Enter initial Amount:10000
Enter
C For Credit
D For Debit
B For Balance
D
Enter Debit Amount:4000
New Amount:6000.0