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常用語法和示例