switch работает только с числовыми параметрами. Строковые не подходят.
Как не подходят??
У вас даже в словаре пример со стринговыми константами:
У вас даже в словаре пример со стринговыми константами:
Switch operator
switch (expression)
{
case constant1: operators; break;
case constant2: operators; break;
...
default: operators; break;
}
It compares the expression value with constants in all variants of case and gives control to the operator that resembles the expression value. Each variant of the case can be marked with an integer or character constant or a constant expression. The constant expression must not include variables and function calls.
Example:
case 3+4: //valid
case X+Y: //invalid
Operators connected with a default label are executed if none of the constants in case operators equals the expression value. The default variant is not obligatory final. If none of the constants resembles the expression value and the default variant is absent, no actions are executed. The keyword case and the constant are just labels and if operators are executed for some variant of case the program will further execute the operators of all following variants until it hits a break operator. It allows linking one succession of operators with several variants. A constant expression is calculated during compilation.
None of two constants in one switch operator can have the same values.
Example:
switch(x)
{
case 'A':
Print("CASE A\n");
break;
case 'B':
case 'C':
Print("CASE B or C\n");
break;
default:
Print("NOT A, B or C\n");
break;
}
switch (expression)
{
case constant1: operators; break;
case constant2: operators; break;
...
default: operators; break;
}
It compares the expression value with constants in all variants of case and gives control to the operator that resembles the expression value. Each variant of the case can be marked with an integer or character constant or a constant expression. The constant expression must not include variables and function calls.
Example:
case 3+4: //valid
case X+Y: //invalid
Operators connected with a default label are executed if none of the constants in case operators equals the expression value. The default variant is not obligatory final. If none of the constants resembles the expression value and the default variant is absent, no actions are executed. The keyword case and the constant are just labels and if operators are executed for some variant of case the program will further execute the operators of all following variants until it hits a break operator. It allows linking one succession of operators with several variants. A constant expression is calculated during compilation.
None of two constants in one switch operator can have the same values.
Example:
switch(x)
{
case 'A':
Print("CASE A\n");
break;
case 'B':
case 'C':
Print("CASE B or C\n");
break;
default:
Print("NOT A, B or C\n");
break;
}
не путайте строки с литералами!
литералы вполне благополучно преобразуются в целые числа.
литералы вполне благополучно преобразуются в целые числа.
Так тип выражения и параметров все равно string хоть они строковые или литерные
Так тип выражения и параметров все равно string хоть они строковые или литерные
Он не string, строки в двойных кавычках. Если одинарные, то это скорее char
syntexor, откуда такая уверенность? не путайте одиночные кавычки с двойными. вот если Вы напишете "A", тогда это и будет считаться строкой.
Вы, кстати, не пробовали компилировать приведённый Вами наш пример из словаря?
Вы, кстати, не пробовали компилировать приведённый Вами наш пример из словаря?
Вы упускаете торговые возможности:
- Бесплатные приложения для трейдинга
- 8 000+ сигналов для копирования
- Экономические новости для анализа финансовых рынков
Регистрация
Вход
Вы принимаете политику сайта и условия использования
Если у вас нет учетной записи, зарегистрируйтесь
При попытке компиляции вот такого:
-выдается ошибка такая: