python常用语法和示例
使用默认情况下的决策 (Decision making using switch-case-default)
Many times in our daily lives, we face conditions where we are required to choose between a number of alternatives rather than just two or three. For example, which school to go to, which food to have in a restaurant, which game to play, etc. Similarly, in programming languages, we sometimes face problems where we have to make the program user-friendly by giving them more than two alternatives to choose from rather than just one or two.
在我们的日常生活中,很多时候我们面临的条件是,我们需要在许多替代方案中进行选择,而不仅仅是两个或三个。 例如,去哪所学校,在餐厅里吃什么食物,玩什么游戏等。类似地,在编程语言中,有时我们会遇到问题,必须给程序提供两个以上的语言,以使程序易于使用可供选择的替代方法,而不仅仅是一两个。
In such cases, it becomes a convoluted problem if we use a series of if-else statements. Therefore, C provides us a discrete control statement which is "switch" to handle such issues effectively. Let us learn how to use a switch, case and default keywords?
在这种情况下,如果我们使用一系列if-else语句 ,这将成为一个令人费解的问题。 因此,C为我们提供了一个离散的控制语句,该语句是有效地处理此类问题的“开关” 。 让我们学习如何使用switch,case和default关键字 ?
The general form of the three keywords is:
这三个关键字的一般形式是:
switch (integral_expression)
{
case constant_1:
code;
[break;]
case constant_2:
code;
[break;]
.
.
.
case constant_n:
code;
[break;]
default:
code;
}
Points to be noted:
注意事项:
The integer expression after the switch keyword is any valid C statement that yields an integer value. Example, integer constants like 1, 2, 100 etc.
switch关键字之后的整数表达式是任何产生整数值的有效C语句。 例如,整数常量样1,2,100等。
The values of constant_1, constant_2 after the case keyword can be an integer or character. But all these constants must be different from each other.
case关键字后面的constant_1 , constant_2的值可以是整数或字符。 但是所有这些常数必须彼此不同。
The code mentioned above is the code that we want to execute. It can be anything ranging from printf to another switch-case ladder.
上面提到的代码是我们要执行的代码。 从printf到另一个开关箱梯形图,范围可以是任何东西。
Now, when we run our program, what happens first is the integer expression gets evaluated.
现在,当我们运行程序时,首先发生的是对整数表达式求值。
The control then goes inside the switch and the value received from the integer expression is compared with the case constants.
然后,控制进入开关内部,并将从整数表达式接收的值与大小写常量进行比较。
If the match is found with any case constant, that particular case will be executed along with the following case and default statements.
如果找到匹配的任何大小写常量,则将与以下case和default语句一起执行该特定大小写。
If the match is not found, only the statements after the default get executed.
如果找不到匹配项,则仅执行默认值之后的语句。
Example:
例:
#include <stdio.h>
int main( )
{
int i = 2 ;
switch ( i )
{
case 1:
printf ( "1 \n" ) ;
case 2:
printf ( "2 \n" ) ;
case 3:
printf ( "3 \n" ) ;
default:
printf ( "No match \n" ) ;
}
return 0;
}
Output
输出量
2 3 No match
In the program above, most of us expected the output to be only 2 since the value of constant i is 2. But that does not happen since all the case statements and the default gets executed after the match is found, as mentioned earlier.
在上面的程序中,我们大多数人都期望输出为2,因为常数i的值为2 。 但这不会发生,因为所有的case语句和默认值都是在找到匹配项后执行的,如前所述。
To prevent this from happening, we use another statement called the break statement to get the output from that particular case only. Note that break need not be written after the default statement since the control comes out of the switch loop anyway.
为了防止这种情况的发生,我们使用了另一个称为break语句的语句,仅从该特定案例获取输出。 请注意,由于控件始终从切换循环中出来,因此无需在默认语句之后编写break 。
Example:
例:
#include <stdio.h>
int main( )
{
int i = 2 ;
switch ( i )
{
case 1:
printf ( "1 \n" ) ;
break;
case 2:
printf ( "2 \n" ) ;
break;
case 3:
printf ( "3 \n" ) ;
break;
default:
printf ( "No match \n" ) ;
}
return 0;
}
Output
输出量
2
有关切换的更多信息(一些有用的方面和一些缺点) (More about switch (some useful points and some disadvantages))
1) The above program need not be made in an ascending order only. It can be made in other order.
1)上述程序不必仅按升序进行。 可以按其他顺序进行。
Example:
例:
#include<stdio.h>
int main( )
{
int i = 2 ;
switch ( i )
{
case 34 :
printf ( "1 \n" ) ;
break;
case 2 :
printf ( "2 \n" ) ;
break;
case 121 :
printf ( "3 \n" ) ;
break;
default :
printf ( "No match \n" ) ;
}
return 0;
}
Output
输出量
2
2) We can also use "character values" in "case and switch".
2)我们也可以在“大小写和切换”中使用“字符值”。
Example:
例:
#include<stdio.h>
int main()
{
char ch = 's';
switch (ch)
{
case 'a':
printf("The letter is 'a'");
break;
case 'b':
printf("The letter is 'b'");
break;
case 's':
printf("The letter is 's'");
break;
default:
printf("No match");
}
return 0;
}
Output
输出量
The letter is 's'
The characters are in reality replaced by their ASCII values by the compiler to make them act like integer constants.
实际上,编译器将这些字符替换为其ASCII值 ,以使其表现为整数常量。
3) If we want to write multiple statements within a particular case, we need not enclose them within a pair of braces.
3)如果要在特定情况下编写多个语句,则无需将它们放在大括号内。
4) If there is a statement inside the switch statement but it does not belong to any of the cases then that particular statement will not be executed or in other words will be skipped by the compiler.
4)如果switch语句中有一条语句,但它不属于任何情况,则该特定语句将不会执行,或者编译器将跳过该语句。
5) It is not compulsory to add the default statement at the end of the switch. Even if we don’t write it, the program would run just the same.
5)不必在开关末尾添加默认语句。 即使我们不编写它,该程序也将运行相同的代码。
6) Nested switches exist in reality but rarely used. The switch statements are mostly used for writing menu driven programs.
6)嵌套开关确实存在,但很少使用。 switch语句主要用于编写菜单驱动程序。
7) Many times, we want to execute the same set of statements under different cases. This can be done as shown below.
7)很多时候,我们希望在不同情况下执行同一组语句。 可以如下所示进行。
Example:
例:
#include <stdio.h>
int main()
{
char ch;
printf("Enter alphabets a, b or c:\n");
scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'A':
printf("The letter is 'a'");
break;
case 'b':
case 'B':
printf("The letter is 'b'");
break;
case 'c':
case 'C':
printf("The letter is 's'");
break;
default:
printf("No match");
}
return 0;
}
Output
输出量
Enter alphabets a, b or c: bThe letter is 'b'
Here, what happens is that the cases keep executing until a break statement is found. Therefore, if for example if alphabet a is entered the case 'a' is satisfied and because there are no statements after that, the control goes to the next case i.e. case 'A' and executes the statements underneath that.
在这里,发生的情况是案例一直执行到找到break语句为止。 因此,例如,如果输入了字母a,则满足条件“ a” ,并且由于此后没有语句,控制转到下一个情况,即条件“ A”,并在其下执行语句。
8) The switch statement is often compared with the if statement. It is better to use the switch in many cases due to the advantages listed above but also, the disadvantage of the switch is that we can't have a case which contains conditionals like: case i > 10:
8)经常将switch语句与if语句进行比较。 由于上面列出的优点,最好在许多情况下使用该开关 ,但是,该开关的缺点是我们不能有一个包含条件的案例,例如: case i> 10:
翻译自: https://www.includehelp.com/c/switch-case-tutorial-syntax-examples-and-rules.aspx
python常用语法和示例