c语言条件语句示例_带有示例的C / C ++教程中的Switch Case语句-程序员宅基地

技术标签: c++  css  python  java  编程语言  

c语言条件语句示例

c语言条件语句示例

C/C++ programming languages provides switch ... case statement for selection according to given switch state. switch  ... case can be used for different cases in order to control the flow of the application.

C / C ++编程语言提供了switch ... case语句,用于根据给定的switch状态进行选择。 switch ... case可以用于不同的情况,以控制应用程序的流程。

句法 (Syntax)

switch ... case has the following syntax.

switch ... case具有以下语法。

switch(EXPRESSION)
{
   case CONDITION1:
     CASE1_CODE;
     break;

   case CONDITION2:
     CASE1_CODE;
     break;
...

   default:
      DEFAULT_CODE;
}
  • `switch` is the keyword used to create a `switch … case` structure.

    “ switch”是用于创建“ switch…case”结构的关键字。
  • `EXPRESSION` is the which will be checked against provided cases.

    将根据提供的情况检查“ EXPRESSION”。
  • `case` keyword is used to create a new case with the specified condition.

    “ case”关键字用于创建具有指定条件的新案例。
  • `CONDITION` specifies the conditions related to the case.

    CONDITION指定与案件相关的条件。
  • `CASE_CODE` is used to execute if the given condition is met for the given variable.

    如果给定变量满足给定条件,则使用CASE_CODE执行。
  • `break` is used to end the given case and exit from it. Normally after the matched case, the below cases will be tried to match but the `break` will end the complete switch block and do not check the following cases.

    “ break”用于结束给定的案例并从中退出。 通常,在匹配大小写之后,将尝试匹配以下大小写,但是`break`将结束整个切换块,并且不检查以下大小写。
  • `default` case is used when no one of the previously defined cases are met.

    如果没有满足任何先前定义的大小写,则使用“默认”大小写。
  • `DEFAULT_CODE` is used to run when the default case is executed.

    DEFAULT_CODE`用于在执行默认大小写时运行。

切换…案例陈述示例 (Switch … Case Statement Example)

Well, the switch case statement is a bit hard to understand according to the other keywords and mechanisms because it contains a lot of code.

好吧,根据其他关键字和机制,switch case语句有点难以理解,因为它包含很多代码。

Switch ... Case Statement Example
Switch … Case Statement Example
切换…案例陈述示例
#include <stdio.h>

int main () {

   /* Variable which will be used inside the switch case */
   char mygrade = 'B';

   switch(mygrade) {
      case 'A' :
         printf("Your grade is A\n" );
         break;

      case 'B' :
         printf("Your grade is B\n" );
         break;

      case 'C' :
         printf("Your grade is C\n" );
         break;

      case 'D' :
         printf("Your grade is D\n" );
         break;

      case 'E' :
         printf("Your grade is E\n" );
         break;

      case 'F' :
         printf("Your grade is F\n" );
         break;

      default :
         printf("Invalid grade\n" );
   }


   return 0;
}

In this example, we will set the variable mygrade as B and this will match with the case B and print to the screen Your grade is B.

在此示例中,我们将变量mygrade设置为B ,这将与情况B匹配,并显示在屏幕上mygrade Your grade is B

切换语句规则 (Switch Statement Rules)

While using switch ... case statement there are some rules to obey.

在使用switch ... case语句时,需要遵守一些规则。

  • The expression should be a result of constant value.

    该表达式应为常数值的结果。
  • Same value cannot be used for multiple cases.

    相同的值不能用于多种情况。
  • The `default` statement is optional.

    default语句是可选的。
  • `break` statement is optional but in generall it is used in most cases in order to stop the current check flow of the switch case.

    break语句是可选的,但通常在大多数情况下使用它,以停止当前开关箱的检查流程。
  • Multiple switch case blocks can be nested but it should be avoided because it will make the application hard to read and understand.

    可以嵌套多个开关盒块,但应避免使用它,因为这会使应用程序难以阅读和理解。
LEARN MORE  Javascript Decision Making with If-Else
通过If-Else了解更多Javascript决策

默认声明(Default Statement)

default statement is used to run code if there is no match in the existing cases. This can be very helpful to run code in unspecified cases. We will add the default ad the end of the cases and do not provide any case and just provide the default code block we want to run. In the following example we will provide mygrade as Z so it will match the default case and print screen Invalid grade.

如果在现有情况下不匹配,则使用default语句运行代码。 这对于在未指定的情况下运行代码非常有帮助。 我们将在案例结尾处添加default广告,不提供任何案例,而仅提供我们要运行的默认代码块。 在下面的示例中,我们将mygrade提供为Z因此它将与默认情况和打印屏幕Invalid grade匹配。

#include <stdio.h>

int main () {

   /* Variable which will be used inside the switch case */
   char mygrade = 'Z';

   switch(mygrade) {
      case 'A' :
         printf("Your grade is A\n" );
         break;

      case 'B' :
         printf("Your grade is B\n" );
         break;

      case 'C' :
         printf("Your grade is C\n" );
         break;

      case 'D' :
         printf("Your grade is D\n" );
         break;

      case 'E' :
         printf("Your grade is E\n" );
         break;

      case 'F' :
         printf("Your grade is F\n" );
         break;

      default :
         printf("Invalid grade\n" );
   }


   return 0;
}

违约声明 (Break Statement)

The normal behavior of the switch case is flowing from top to down and run matches cases code block and continue for the following cases. But this is generally unwanted situations where after a case is matched and code block executed we generally prefer to exit from switch case. We can use break statement after executing a case code block which will end the switch case completely and does not check for the following cases.

开关案例的正常行为是从上到下流动的,并且运行匹配案例代码块,并在以下情况下继续运行。 但这通常是不希望的情况,在匹配大小写并执行代码块之后,我们通常更喜欢退出切换大小写。 我们可以在执行案例代码块之后使用break语句,该代码块将完全结束切换案例,并且不会检查以下案例。

翻译自: https://www.poftut.com/switch-case-statement-in-c-c-tutorial-with-examples/

c语言条件语句示例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cunjiu9486/article/details/109077925

智能推荐

Android 应用程序组件_android清单文件包含了组成应用程序模块所需要的组件-程序员宅基地

文章浏览阅读168次。应用程序组件是一个Android应用程序的基本构建块。这些组件由应用清单文件松耦合的组织。AndroidManifest.xml描述了应用程序的每个组件,以及他们如何交互。以下是可以在Android应用程序中使用的四个主要组件。组件 描述 Activities 描述UI,并且处理用户与机器屏幕的交互。 Services 处理与应用程序关联的后台操作。 Broadcast Receivers 处理Android操作系统和应用程序之间的通信。 Content Prov_android清单文件包含了组成应用程序模块所需要的组件

22.8.29 C语言作业5道_a.c: in function ‘main’:-程序员宅基地

文章浏览阅读387次。1.字符转换输出递归按位输出_a.c: in function ‘main’:

【pygame游戏】用Python实现一个蔡徐坤大战篮球的小游戏,可还行?【附源码】_python蔡徐坤代码复制-程序员宅基地

文章浏览阅读6.9w次,点赞137次,收藏246次。表弟大周末的跑来我家,没事干天天骚扰我,搞得我都不能跟小姐姐好好聊天了,于是为了打发表弟,我决定用Python做一个小游戏来消耗一下他的精力..._python蔡徐坤代码复制

系统分析、设计_信息系统开发中分析与设计的重要性。-程序员宅基地

文章浏览阅读1.9k次。blueski推荐 [2007-1-31]出处:Java夜无眠作者:蔡学镛 1、系统分析是什么?   系统分析工作是解决一个问题的工作,目标是将一个对计算机应用系统的需求转化成实际的物理实现,其中复杂就复杂在实际的面太多.在系统分析过程之中注意问以下的问题,可能会所进行的系统分析设计工作有帮助。    1)您所完成的系统目的是什么?注意不是功能要求,而是目的.也就是为什么要建设、为什么要现_信息系统开发中分析与设计的重要性。

nginx 学习+案例练习_nginx 使用pipe:rollback-程序员宅基地

文章浏览阅读659次。nginx软件学习nginx是个web服务器,常用作静态文件服务器,反向代理服务器,邮件代理服务器,负载均衡服务器1.安装淘宝nginx,编代码编译安装,先解决模块依赖 yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel..._nginx 使用pipe:rollback

python_one893.app-程序员宅基地

文章浏览阅读6.7k次。pycharm安装库包失败:比如安装matplotlib失败下载库文件:https://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib根据python版本等来选择适合的文件,比如python3.8选择cp38,python3.10选择cp310,这里我选择的是matplotlib‑3.4.3‑cp310‑cp310‑win_amd64.whl将下载好的文件放入python安装包的Scripts中,运行cmd命令行,执行pip install .\_one893.app

随便推点

回溯算法(leetcode 306 python)-程序员宅基地

文章浏览阅读295次。回溯算法:回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条件时,就“回溯”返回,尝试别的路径。回溯法是一种选优搜索法,按选优条件向前搜索,以达到目标。但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”。许多复杂的,规模较大的问题都可以使用回..._leetcode 306、python

redis的使用总结_redis.redis(host='127.0.0.1', port=6379)-程序员宅基地

文章浏览阅读352次。数据库redis使用优点使用方法redis的简单类型String字符串注意一个键最大能存储512MB哈希list列表set集合注意redis安全可以通过以下命令查看是否设置了密码验证用python实现链接数据库redis-connectredis密码破解redis使用设置数据添加数据redis查看服务器配置插入值追加数据setget数据导出dumprdb 这个即为_redis.redis(host='127.0.0.1', port=6379)

华为ICT解决方案助力全球190多家电力公司数字化转型-程序员宅基地

文章浏览阅读1k次。华为以“比特驱动瓦特,共建全联接智能电网”为主题,举办第七届华为全球电力峰会(线上),邀请来自全球各地的客户、伙伴、行业精英和思想领袖共同探讨。面对2020年的全球疫情、政治、经济等不确..._ict数字化解决方案资金分配

将图片资源文件整合到DLL文件中 _易语言dll加入资源-程序员宅基地

文章浏览阅读733次。 1、新建一个类库,例如库名为 ResourcesLibrary;2、添加引用 System.Drawing;3、添加资源文件(添加--新建项--资源文件),例如文件名为 Resource1.resx;4、添加图片(打开Resource1.resx,单击“添加资源”后的小三角,选择添加现有文件,选择需要作为资源的图片),例如添加了图片 Sunset.jpg5、添加类,例如名为 GetImage_易语言dll加入资源

MySQL 时间函数(答学生问:关于员工考勤查询)_%h:%i-程序员宅基地

文章浏览阅读4.4k次,点赞4次,收藏15次。今天有学生问个关于MySQL时间操作的问题,直接看图: 查询某日人员的出勤时间问题。我们都知道,企业员工考勤使用打卡方式,每天会记录每个员工的多次打卡记录,最后取每个员工的每天:上班时间:当日最早时间;下班时间:当日最晚时间。所以分析查询每个员工每日考勤记录,需要根据员工分组,当天时间、时间最小和最大,时间格式化等操作,下面举例模拟该问题:学生表student ..._%h:%i

Python实战——1_1.网页制作_"<nav> <ul class=\"summary\"> <!-- 一级目录 --> {% for-程序员宅基地

文章浏览阅读1.3w次,点赞4次,收藏31次。Python实战——1_1.网页制作引言网页的组成部分:CSS样式 - 给结构以装饰Html - 结构部分JavaScript- 功能实现代码部分第一部分为网站的基本结构(在IDE中新建网页后既给出) {% for docs in toc_list %} "

推荐文章

热门文章

相关标签