Skip to content

Case Statements

The case statement is the one statement where there is a difference in C/C++ compared to C#. The syntax is very similar, but there are limitations on the kind of data you can use in the expression. C/C++ can only use ordinal types in the switch statement, meaning we are limited to integer values.

Example

Here is an updated version of the case statement example. As our case statement is restricted to comparing integers, we use a simple menu to select between options instead of comparing strings.

#include "splashkit.h"
using std::stoi;
int main()
{
string line;
int option;
write_line("What language do you use? ");
write_line("1 - C#");
write_line("2 - C");
write_line("3 - C++");
write_line("4 - Others");
write("Enter option: ");
line = read_line();
option = stoi(line);
switch (option)
{
case 1:
write_line("Good choice, C# is a fine language.");
break;
case 2:
case 3:
write_line("These are great low level languages - we will be using these soon!");
break;
default:
write_line("Well... good luck with that!");
}
write_line("Great chat!");
}