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.mzph.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…