无涯教程-D语言 - nested switch 语句函数

本文详细解释了D语言中嵌套switch语句的语法结构,展示了如何在switch语句内部嵌套其他switch语句,并通过示例代码说明其实现过程和输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

嵌套switch语句意味着,在switch语句中可以随意嵌套任意的switch语句。

nested switch - 语法

嵌套switch语句的语法如下所示:-

switch(ch1) { 
   case A:  
      writefln("This A is part of outer switch" ); 
      switch(ch2) { 
         case A: 
            writefln("This A is part of inner switch" ); 
            break; 
         case B: /* case code */
      } 
      break; 
   case B: /* case code */
}

nested switch - 示例

import std.stdio;

int main () { 
   /* 局部变量定义 */
   int a=100; 
   int b=200;  
   
   switch(a) {
      case 100: 
         writefln("This is part of outer switch", a ); 
         switch(b) { 
            case 200: 
               writefln("This is part of inner switch", a ); 
            default: 
               break; 
         } 
      default: 
      break; 
   } 
   writefln("Exact value of a is : %d", a ); 
   writefln("Exact value of b is : %d", b ); 
  
   return 0; 
}

编译并执行上述代码时,将生成以下结果-

This is part of outer switch 
This is part of inner switch 
Exact value of a is : 100 
Exact value of b is : 200 

D语言 中的 nested switch 语句函数 - 无涯教程网无涯教程网提供嵌套switch 语句意味着,在switch 语句中可以随意嵌套任意的switch 语句。 nested swi...https://www.learnfk.com/d-programming/d-programming-nested-switch-statements.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值