เคธ्เคตिเค เคेเคธ เคธ्เคेเคเคฎेंเค
เคिเคธी เคช्เคฐोเค्เคฐाเคฎ เคฎे เคกीเคธीเคเคจ เคฎेเคिंเค เคो เคนैंเคกเคฒ เคเคฐเคจे เคे เคฒिเค เค
เคเคฐ เค्เคฏाเคฆा if else เคธ्เคेเคเคฎेंเค เคเคชเคฏोเค เคिเค เคाเค เคคो เคช्เคฐोเค्เคฐाเคฎ เคฌเคนुเคค เคเคिเคฒ เคฌเคจ เคाเคเคा। เคिเคธเคธे เคช्เคฐोเค्เคฐाเคฎ เคे เคฎेंเคीเคจेंเคธ เคे เคธเคฎเคฏ เคช्เคฐोเค्เคฐाเคฎเคฐ เคो เคช्เคฐोเค्เคฐाเคฎ เคธเคฎเคเคจे เคฎे เคเค िเคจाเค เคा เคธाเคฎเคจा เคเคฐเคจा เคชเฅเคคा เคนैं। เคो เคी เค
เค्เคे เคช्เคฐोเค्เคฐाเคฎเคฎिंเค เคตाเคคाเคตเคฐเคฃ เคे เคฒिเค เคธเคนी เคจเคนीं เคนैं।
เคเคธ เคธเคฎเคธ्เคฏा เคธे เคจिเคाเคค เคชाเคจे เคे เคฒिเค เคธ्เคตिเค เคेเคธ เคธ्เคेเคเคฎेंเค เคा เคเคชเคฏोเค เคिเคฏा เคा เคธเคเคคा เคนैं। เคธ्เคตिเค เคेเคธ เคธ्เคेเคเคฎेंเค เคเค เคธเคฐเคฒ เคกीเคธीเคเคจ เคฎेเคिंเค เคธ्เคेเคเคฎेंเค เคนैं। เคिเคธเคा เคเคชเคฏोเค เคเคธाเคจी เคธे เคเคตं เคธเคนเคเคคा เคे เคธाเคฅ เคिเคฏा เคा เคธเคเคคा เคนैं เคคเคฅा เคเคธเคฎे if else เคธे เคเคค्เคชเคจ्เคจ เคนोเคจे เคตाเคฒी เคเคिเคฒเคคा เคा เคธाเคฎเคจा เคจเคนीं เคเคฐเคจा เคชเฅเคคा เคนैं।
เคธ्เคตिเค เคेเคธ เคธ्เคेเคเคฎेंเค เคा เคเคชเคฏोเค เคเคฐเคจे เคे เคฒिเค switch, case, default, break เคी-เคตเคฐ्เคก เคा เคเคธ्เคคेเคฎाเคฒ เคिเคฏा เคाเคคा เคนैं। switch เคฎे เคเค เคกीเคธीเคเคจ เคฎेเคिंเค เคนैं เคिเคธเคฎे เคช्เคฐोเคाเคฎเคฐ เคोเค เคตैเคฒ्เคฏू เคฏा เคเค्เคธ्เคช्เคฐेเคถ्เคจ เคो เคชाเคธ เคเคฐเคคा เคนैं। เคฏเคน เคเค्เคธ्เคช्เคฐेเคถ्เคจ เคธिเคฐ्เคซ เคंเคीเคเคฐ เคเคฐ เคैเคฐेเค्เคเคฐ เคे เคฐूเคช เคฎे เคนी เคนो เคธเคเคคे เคนैं।
เคเคธเคे เคฌाเคฆ case เคीเคตเคฐ्เคก เคा เคเคชเคฏोเค เคนोเคคा เคนैं। เคिเคฎเคธे case เคे เคธाเคฅ เคเค เคोंเคธ्เคेंเค เคตैเคฒ्เคฏू เคा เคเคชเคฏोเค เคนोเคคा เคนैं। เคฏเคน เคोंเคธ्เคेंเค เคตैเคฒ्เคฏू เคंเคीเคเคฐ เคฏा เคैเคฐेเค्เคเคฐ เคนी เคนो เคธเคเคคे เคนैं।
เคธ्เคตिเค เคธ्เคेเคเคฎेंเค เคा เคा syntax เคจिเคฎ्เคจ เคช्เคฐเคाเคฐ เคนैं।
switch (expression) { case constant-expression : statement 1 break; case constant-expression 2: statement 2 ; break; case constant-expression 3: statement 3 ; break ; default : default statement ; break; }
เคเคชเคฐ เคฆिเคฏे syntax เคो เค
เคฌ เคธเคฎเคเคจे เคा เคช्เคฐเคฏाเคธ เคเคฐเคคे เคนैं switch(expression) เคฎे เคนเคฎे เคिเคธी เคเค्เคธ्เคช्เคฐेเคถ्เคจ เคा เคเคธ्เคคेเคฎाเคฒ เคเคฐเคจा เคนोเคคा เคนैं เคชเคฐ เคเค्เคธ्เคช्เคฐेเคถ्เคจ เคंเคीเคเคฐ เคฏा เคैเคฐेเค्เคเคฐ เคนी เคนोเคจा เคाเคนिเค।
เคเคธเคे เคฌाเคฆ case เคीเคตเคฐ्เคก เคा เคเคธ्เคคेเคฎाเคฒ เคिเคฏा เคाเคคा เคนैं, เคเคธเคे เค ीเค เคฌाเคฆ เคฎे constant- expression เคฒिเคा เคाเคคा เคนैं เคैเคธा เคिเคฏा เคเคช เคเคชเคฐ เคฆिเคฏे syntax เคฎे เคฆेเค เคธเคเคคे เคนैं। เคเคธเคे เคฌाเคฆ case เคो : เคॉเคฒเคจ เคธे เคฌंเคฆ เคเคฐเคจा เคนोเคคा เคนैं। เคैเคธा เคी เคिเคे เคฆिเคฏा เคเคฏा เคนैं।
case constant-expresson :
เคเคธเคे เคฌाเคฆ เคเคช เคธ्เคेเคเคฎेंเค เคฒिเค เคธเคเคคे เคนैं, เคเคธเคฎे เคธ्เคेเคเคฎेंเค เคे เคฐूเคช เคฎे printf เคा เคเคธ्เคคेเคฎाเคฒ เคिเคฏा เคाเคคा เคนैं। เคिเคธเคा เคाเคฎ เคตैเคฒ्เคฏू เคे เคเคเคเคชुเค เคो เคธ्เค्เคฐीเคจ เคฎे เคช्เคฐिंเค เคเคฐเคจे เคे เคฒिเค เคเคชเคฏोเค เคिเคฏा เคाเคคा เคนैं। เคเคฐ เคंเค्เคฐोเคฒ เคो เค
เคเคฒे case เคฏा switch เคธे เคฌाเคนเคฐ เคเคฐเคจे เคे เคฒिเค break เคธ्เคेเคเคฎेंเค เคा เคเคธ्เคคेเคฎाเคฒ เคिเคฏा เคाเคคा เคนैं। เคैเคธा เคी เคเคช เคจीเคे เคฆिเคฏे เคเค เคोเคก เคฎे เคฆेเค เคธเคเคคे เคนैं।
case constant-expression1 :
printf(“Here i am printing my output”);
break ;
Flow chart of switch case statement
เค เคฌ เคเค เคธ्เคตिเค case stetment เคो เคธเคฎเคเคจे เคे เคฒिเค เคเค เคช्เคฐोเค्เคฐाเคฎ เคฆेเคเคคे เคนैं। Example: Simple Calculator using switch Statement
// Performs addition, subtraction, multiplication or division depending the input from user
# include
int main() {
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
break;
// operator doesn't match any case constant (+, -, *, /)
default:
printf("Error! operator is not correct");
}
return 0;
}
# include
int main() {
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
break;
// operator doesn't match any case constant (+, -, *, /)
default:
printf("Error! operator is not correct");
}
return 0;
}
Output
Enter an operator (+, -, *,): *
Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8