+5

What is use of switch statement?

c++ c

9/19/2019 1:42:55 PM

Rajeshwari Dadaji Ahire

8 Answers

New Answer

+7

Rajeshwari Dadaji Ahire Please, Remember to use 🔍SEARCH... bar future you can find many similar threads! https://www.sololearn.com/Discuss/1091712/?ref=app https://www.sololearn.com/Discuss/1490288/?ref=app

+6

After completing the C++ course you should't be asking this. Revise your C++ course again https://www.sololearn.com/learn/CPlusPlus/1618/

+3

A replacement to a lot of if statement

+3

Switch statement allow you to choose one option from multiple option.

+3

You could use the switch statement to specific cases, that's doesn't exist any logical test For example If (age == 18){ printf("..."); } Else if (age == 30) { printf("..."); } Else { Something } You can use Switch (age) { Case 18: printf("..."); break; Case 30: printf("..."); break; default: something; break; } So , if you want to test specific cases, use the switch, it's "clearly" I think, and the if statement to logical tests like If (age <= 18) { Something } Because the switch case statement doesn't do that, and other scenario to use switch case is on some menu, where the user needs to choice some option , like 1 - register, 2 - login, whatever, on this cases switch is good I hope it helped you ;) Good studies! if you want to see some example, I've made a simple calculator in C language using the switch case statement ... https://code.sololearn.com/cBkU9745cQ4v/?ref=app

+2

We can use the switch statement where we need to compare for a fix condition with a multiple choices it only includes the Int value and other strings

+1

It's a cooler version of if, else if, else blocks together.

+1

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, Syntax: switch (n) { case 1: // code to be executed if n = 1; break; case 2: // code to be executed if n = 2; break; default: // code to be executed if n doesn't match any cases }