C语言学习之关键字第五讲

本文详细介绍了C语言中的switch语句,包括其结构、工作原理以及如何使用case和default来实现多分支控制。强调了case常量的独特性和default的作用,同时也提到了break语句在控制流程中的重要性。此外,还探讨了switch语句中的特殊情况,如无break的执行流程和default的位置对程序执行的影响。
摘要由CSDN通过智能技术生成

switch 开关语句(多重分支语句)

case 开关语句中的分支标记

default 开关语句中的“其他”分支,可选。

The C switch Statement

C语言中switch分支表达形式

The switch and case statements help control complex conditional and branching operations. The switch statement transfers control to a statement within its body.

switch和case声明帮助控制复杂情况和分支操作,switch表达式控制转移至其内部的分支表达式。

 switch (expression )                      

{

    case constant-expression : expression; 

    case constant-expression : expression; 

    …     

    case constant-expression : expression;       

    default : statement;

}

switch(表达式)

{
    case 常量表达式1:  语句1;
    case 常量表达式2:  语句2;
    … 
    case 常量表达式n:  语句n;
    default:  语句n+1;
}

Control passes to the statement whose case constant-expression matches the value of switch ( expression ). The switchstatement can include any number of case instances, but no two case constants within the same switch statement can have the same value. Execution of the statement body begins at the selected statement and proceeds until the end of the body or until a break statement transfers control out of the body.

switch语句的表达式控制case的程序执行入口,switch的表达式中可以是其内部case的常量,也可以是其他常量。case中的常量表达式不能相同,执行时只要switch中的值和case值相同则从此case执行语句开始执行,知道遇到第一个break或者执行完所有的执行语句结束。

Use of the switch statement usually looks something like this:

switch ( expression 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值