How can we simplify the code when more than two if's are required ?
We use switch statements
Syntax:
Example:
/* Program to create a simple calculator for addition, subtraction, multiplication and division */
Output :
Enter operator +, -, * or / :
/
Enter two operators:
12
5
num2/num1=2.4;
Description:
This example shows how to use switch case.
Switch can be user when we need two or more if conditions.
Line 1,2 - Here we include two header files.
Line 5,6 - Declares variables.
Line 8 - Gets a character input from console. Important thing is to be noted that if anything except +,-,*,/ is typed then console returns error.
Actual working of switch case:
Line 11 - switch(operator): operator is a variable in which we have already taken input.
Now what "switch" does is it switches to particular case character as present in "operator" variable. Suppose operator has value + (plus) then switch() will be switched to first case.
Similarly others work.
Description in marathi: เคฎเคฐाเค ीเคค เคตเคฐ्เคฃเคจ:
เคน्เคฏा เคเคงाเคฐ्เคจाเคฅ เคธ्เคตीเค เคेเคธ เคเคธा เคตाเคชเคฐเคคाเคค เคคे เคฆिเคฒेเคฒा เคเคนे. เคธ्เคตीเค เคेเคธ เคเคชเคฃ เคตाเคชเคฐू เคถเคเคคो เคेเคต्เคนा เฅจ เคชेเค्เคทा เคाเคธ्เคค " เคเคซ เคเคฒ्เคธ " เคตाเคชเคฐाเคฏเคे เค เคธेเคฒ .
เคเคณ เฅง,เฅจ - เคเคฅे เคเคชเคฃ เคฆोเคจ เคนेเคกเคฐ เฅाเคเคฒ เคตाเคชเคฐเคฃ्เคฏा เคเคฐिเคคा เคคी เคเคณ เคฒिเคนिเคฒेเคฒी เคเคนे.
เคเคณ เฅซ,เฅฌ - เคช्เคฐोเค्เคฐाเคฎ เคฎเคง्เคฏे เคे เคตेเคฐिเคเคฌ्เคฒ เคตाเคชเคฐเคฒे เคाเคฃाเคฐ เคคे เคเคฅे เคฒिเคนिเคฒेเคฒे เคเคนे.
เคเคณ เฅฎ - เคเคจ्เคธोเคฒ เคตเคฐूเคจ เฅง เคाเคฐाเค्เคेเคฐ เคेเคฃ्เคฏाเคธाเค ी เคนि เคเคณ เคฒिเคนिเคฒेเคฒे เคเคนे.เคคो เคเคช्เคฐेเคคเคฐ เคจाเคตाเคा เคตเคฐिเค เคฌ्เคฒ เคฎเคง्เคฏे เคเคฎा เคนोเคคो.เคเคฅे "getch" เคตाเคชเคฐเคคांเคจा เคเคฐ +-*/ เคน्เคฏा เคชेเค्เคทा เคฆुเคธเคฐा เคाเคนी เคเคจ्เคธोเคฒเคตเคฐ เคฒिเคนिเคฒे เคคเคฐ เคเคจ्เคธोเคฒเคตเคฐ เคเคฐ्เคฐोเคฐ เคฆिเคธेเคฒ.
เคธ्เคตीเค เคेเคธ เคช्เคฐเคค्เคฏเค्เคทाเคค เคเคธा เคाเคฎ เคเคฐเคคो ?:
เคเคณ เฅงเฅง - Switch(operator) :เคเคช्เคฐेเคคเคฐ เคนा เคเค เค เคธा เคตเคฐिเค เคฌ्เคฒ เคเคนे เค्เคฏाเคค เคเคชเคฃ เคเคงीเค เคฏुเคธเคฐ เคเคกूเคจ เคाเคฐाเค्เคाเคฐ เคेเคคเคฒा เคเคนे.เคเคคा เคเคช्เคฐेเคคเคฐ เคตเคฐिเค เคฌ्เคฒ เคฎเคง्เคฏे เคे เคाเคนी เคเคนे เคคो เคेเคธ เคฎเคงเคฒा เคोเคก เคฐเคจ เคนोเคคो. เคเคฐ เคเคช्เคฐेเคคเคฐ เคตเคฐिเค เคฌ्เคฒ เคฎเคง्เคฏे ' + ' เค เคธेเคฒ เคคเคฐ เคชเคนिเคฒा เคेเคธ เคฐเคจ เคนोเคฃाเคฐ.เคเคฐ ' - ' เค เคธेเคฒ เคคเคฐ เคฆुเคธเคฐा.
We use switch statements
Syntax:
Example:
/* Program to create a simple calculator for addition, subtraction, multiplication and division */
Output :
Enter operator +, -, * or / :
/
Enter two operators:
12
5
num2/num1=2.4;
Description:
This example shows how to use switch case.
Switch can be user when we need two or more if conditions.
Line 1,2 - Here we include two header files.
Line 5,6 - Declares variables.
Line 8 - Gets a character input from console. Important thing is to be noted that if anything except +,-,*,/ is typed then console returns error.
Actual working of switch case:
Line 11 - switch(operator): operator is a variable in which we have already taken input.
Now what "switch" does is it switches to particular case character as present in "operator" variable. Suppose operator has value + (plus) then switch() will be switched to first case.
Similarly others work.
Description in marathi: เคฎเคฐाเค ीเคค เคตเคฐ्เคฃเคจ:
เคน्เคฏा เคเคงाเคฐ्เคจाเคฅ เคธ्เคตीเค เคेเคธ เคเคธा เคตाเคชเคฐเคคाเคค เคคे เคฆिเคฒेเคฒा เคเคนे. เคธ्เคตीเค เคेเคธ เคเคชเคฃ เคตाเคชเคฐू เคถเคเคคो เคेเคต्เคนा เฅจ เคชेเค्เคทा เคाเคธ्เคค " เคเคซ เคเคฒ्เคธ " เคตाเคชเคฐाเคฏเคे เค เคธेเคฒ .
เคเคณ เฅง,เฅจ - เคเคฅे เคเคชเคฃ เคฆोเคจ เคนेเคกเคฐ เฅाเคเคฒ เคตाเคชเคฐเคฃ्เคฏा เคเคฐिเคคा เคคी เคเคณ เคฒिเคนिเคฒेเคฒी เคเคนे.
เคเคณ เฅซ,เฅฌ - เคช्เคฐोเค्เคฐाเคฎ เคฎเคง्เคฏे เคे เคตेเคฐिเคเคฌ्เคฒ เคตाเคชเคฐเคฒे เคाเคฃाเคฐ เคคे เคเคฅे เคฒिเคนिเคฒेเคฒे เคเคนे.
เคเคณ เฅฎ - เคเคจ्เคธोเคฒ เคตเคฐूเคจ เฅง เคाเคฐाเค्เคेเคฐ เคेเคฃ्เคฏाเคธाเค ी เคนि เคเคณ เคฒिเคนिเคฒेเคฒे เคเคนे.เคคो เคเคช्เคฐेเคคเคฐ เคจाเคตाเคा เคตเคฐिเค เคฌ्เคฒ เคฎเคง्เคฏे เคเคฎा เคนोเคคो.เคเคฅे "getch" เคตाเคชเคฐเคคांเคจा เคเคฐ +-*/ เคน्เคฏा เคชेเค्เคทा เคฆुเคธเคฐा เคाเคนी เคเคจ्เคธोเคฒเคตเคฐ เคฒिเคนिเคฒे เคคเคฐ เคเคจ्เคธोเคฒเคตเคฐ เคเคฐ्เคฐोเคฐ เคฆिเคธेเคฒ.
เคธ्เคตीเค เคेเคธ เคช्เคฐเคค्เคฏเค्เคทाเคค เคเคธा เคाเคฎ เคเคฐเคคो ?:
เคเคณ เฅงเฅง - Switch(operator) :เคเคช्เคฐेเคคเคฐ เคนा เคเค เค เคธा เคตเคฐिเค เคฌ्เคฒ เคเคนे เค्เคฏाเคค เคเคชเคฃ เคเคงीเค เคฏुเคธเคฐ เคเคกूเคจ เคाเคฐाเค्เคाเคฐ เคेเคคเคฒा เคเคนे.เคเคคा เคเคช्เคฐेเคคเคฐ เคตเคฐिเค เคฌ्เคฒ เคฎเคง्เคฏे เคे เคाเคนी เคเคนे เคคो เคेเคธ เคฎเคงเคฒा เคोเคก เคฐเคจ เคนोเคคो. เคเคฐ เคเคช्เคฐेเคคเคฐ เคตเคฐिเค เคฌ्เคฒ เคฎเคง्เคฏे ' + ' เค เคธेเคฒ เคคเคฐ เคชเคนिเคฒा เคेเคธ เคฐเคจ เคนोเคฃाเคฐ.เคเคฐ ' - ' เค เคธेเคฒ เคคเคฐ เคฆुเคธเคฐा.
Comments