python常用語法和示例_C語言切換案例教程,語法,示例和規則

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:

注意事項:

  1. 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等。

  2. 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的值可以是整數或字符。 但是所有這些常數必須彼此不同。

  3. 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到另一個開關箱梯形圖,范圍可以是任何東西。

  4. Now, when we run our program, what happens first is the integer expression gets evaluated.

    現在,當我們運行程序時,首先發生的是對整數表達式求值。

  5. The control then goes inside the switch and the value received from the integer expression is compared with the case constants.

    然后,控制進入開關內部,并將從整數表達式接收的值與大小寫常量進行比較。

  6. 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語句一起執行該特定大小寫。

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542367.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542367.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542367.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

android so abi適配,Android NDK學習(六): so文件兼容之abiFilters的使用

最近項目中遇到了要使用JavaCV的情況&#xff0c;涉及到了abi兼容的選擇。因為如果全部都適配的話&#xff0c;包很大&#xff0c;這樣兼容那些用戶數極少的cpu就很不劃算&#xff0c;所以我只適配了armeabi-v7a這一個。但是今天在x64-v8a的模擬器上看的時候&#xff0c;提示我…

python中doc=parased.getroot()_python中執行sed命令操作源文件時出現錯誤

我想在python中執行一個sed命令&#xff0c;第一種方法直接指定文件時&#xff0c;可以正確輸出結果&#xff0c;但是第二種我打開文件操作的時候就有問題&#xff0c;不知道什么原因&#xff0c;求高手解答&#xff1f;(1)>>> sedcmd"sed -n \s/{//g; p\ /qye/p…

JavaScript基礎之Number對象和Math對象

2019獨角獸企業重金招聘Python工程師標準>>> //Math對象//屬性float Math.E; //返回自然對數的底數e&#xff0c;約2.718float Math.LN2; //返回2的自然對數&#xff0c;約0.693float Math.LN10; //返回10的自然對數&#xff0c;約2.302fl…

c++ stl 獲取最小值_如何在C ++ STL中找到向量的最小/最小元素?

c stl 獲取最小值Given a vector and we have to minimum/smallest element using C STL program. 給定一個向量&#xff0c;我們必須使用C STL程序最小/最小元素。 尋找向量的最小元素 (Finding smallest element of a vector) To find a smallest or minimum element of a …

android studio panic,Android Studio模擬器PANIC錯誤

Android Studio模擬器突然停止工作.當我嘗試運行虛擬設備時,我在事件日志中收到以下錯誤.模擬器:PANIC:找不到AVD系統路徑.請定義ANDROID_SDK_ROOT仿真器:處理完成,退出代碼為1所以我檢查了ANDROID_SDK_ROOT環境變量設置的值,它是空的.所以我把它設置為/Users/{username}/Libra…

linux特殊權限之訪問權限

特殊權限如/etc/passwd:sSuid:普通用戶以管理員身份運行命令&#xff08;chmod us FILE、chmod u-s FILE&#xff09;如果FILE本身原來就有執行權限&#xff0c;SUID顯示為s&#xff1b;否則顯示SSgid:基本組以管理組身份運行命令&#xff08;chmod gs FILE、chmod g-s FILE&am…

vb.net變量值變化觸發事件_Angular變化檢測的理解

獲取臟檢查的時機Angular 使用NgZone獲取變化的通知&#xff0c;然后進行全面的變化檢測&#xff0c;進而更新Dom臟檢查的過程Angular的數據流是自頂而下&#xff0c;從父組件到子組件單項流動&#xff0c;單項數據流保證了高效可預測的變化檢測。盡管檢查了父組件之后&#xf…

python 算術右移_Python算術序列| 競爭編碼問題

python 算術右移Question: 題&#xff1a; In mathematics, when in an arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant then it is called arithmetic constant. 在數學中&#xff0c;當在算術序列中是…

Android8內測申請,小米 6 安卓 8.0 來了 內測開始招募

Android 8.0 已經正式發布多時&#xff0c;目前不少廠商已經啟動了旗下進行的 Android 8.0 適配計劃。但令人納悶的是&#xff0c;一向對系統升級比較熱心的小米卻遲遲沒有動靜。好消息是&#xff0c;此前網友曝光的消息顯示&#xff0c;MIUI 已經悄然在官方論壇中招募小米 6 的…

My linux

為什么80%的碼農都做不了架構師&#xff1f;>>> 1.linux 命令方式修改機器名稱 # hostname newHostName # vi /etc/sysconfig/network 修改或增加配置&#xff1a;hostnamenewHostName # vi /etc/hosts 修改對應的本地HOST映射 xx.xxx.xxx.xxx newHostName 2.Redha…

狂神說es筆記_人教版七上英語Unit5電子課本音頻+課堂筆記+課后同步習題

1人教 七上英語Unit5單詞七年級英語上冊Unit 5單詞默寫1做&#xff1b;干(助動詞)__________2做&#xff0c;干(助動詞第三人稱單數形式)__________3有__________4網球__________5球__________6乒乓球______7球棒&#xff1b;球拍__________8(英式)足球____________________9排…

Java RandomAccessFile getFilePointer()方法與示例

RandomAccessFile類getFilePointer()方法 (RandomAccessFile Class getFilePointer() method) getFilePointer() method is available in java.io package. getFilePointer()方法在java.io包中可用。 getFilePointer() method is used to get the current pointer in the Rando…

先進技術android,React Native實戰(JavaScript開發iOS和Android應用)/計算機科學先進技術譯叢...

導語內容提要本書作者Nader Dabit是AWS Mobile開發人員、React Native Training創始人和React Native Radio播客主持人。本書旨在幫助iOS、Android和Web開發人員學習使用React Native框架&#xff0c;構建高質量的iOS和Android應用程序。書中介紹了React Native入門基礎知識&am…

開發類似vs的黑色風格_傳聞:2020年《使命召喚》將是《黑色行動》重啟作品

據可信度較高的消息源透露&#xff0c;2020 年的《使命召喚》將是《黑色行動》的重啟作。而據之前的報道&#xff0c;《黑色行動》開發商 Treyarch 正在開發今年的《使命召喚》&#xff0c; Sledgehammer Games 和 Raven Software 負責輔助工作。該項目代號為“宙斯”&#xff…

ubuntu中 不同JDK版本之間的切換

Ubuntu中JDK 的切換前提是同時安裝了多個版本&#xff0c;如jdk7和jdk8&#xff0c;若要切換&#xff0c;在終端輸入&#xff1a; sudo update-alternatives --config javasudo update-alternatives --config javac

osi模型:七層模型介紹_聯網| OSI模型能力問題和解答 套裝1

osi模型:七層模型介紹1) There are the following statements that are given below, which of them are correct about the OSI model? The OSI model is a reference model that describes the network functionalities.The OSI model is an implemented model that describ…

華為鴻蒙系統正式登場,華為自研鴻蒙系統將于8月9日正式登場,還有全新的鴻鵠芯片...

最近華為發布了很多手機&#xff1a;榮耀20系列手機、榮耀9X系列、華為Nova 5系列&#xff0c;以及7月26日發布的華為Nova5i Pro和華為首部5G手機Mate20 X 5G版&#xff0c;這些手機將成為華為下半年的出貨主力&#xff0c;市場份額能否超過50%就看這些手機的表現了。華為還將在…

pythonencode和decode_Python3的decode()與encode()

python3的decode()與encode()Tags: Python Python3對于從python2.7過來的人&#xff0c;對python3的感受就是python3對文本以及二進制數據做了比較清晰的區分。文本總是Unicode,由str類型進行表示&#xff0c;二進制數據使用bytes進行表示&#xff0c;不會將str與bytes偷偷的混…

微信小程序 開發 微信開發者工具 快捷鍵

微信小程序已經跑起來了.快捷鍵設置找了好久沒找到,完全憑感覺.圖貼出來.大家看看. 我現在用的是0.10.101100的版本,后續版本更新快捷鍵也應該不會有什么變化. 現在貌似不能修改.如果有同學找到修改的方法,麻煩告訴我.謝謝. 微信小程序代碼編輯快捷鍵 常用快捷鍵 格式調整 Ctrl…

java 根據類名示例化類_Java MathContext類| 帶示例的getRoundingMode()方法

java 根據類名示例化類MathContext類的getRoundingMode()方法 (MathContext Class getRoundingMode() method) getRoundingMode() method is available in java.math package. getRoundingMode()方法在java.math包中可用。 getRoundingMode() method is used to get the Roundi…