D语言 中的 nested switch 语句函数

首页 / D语言入门教程 / D语言 中的 nested switch 语句函数

嵌套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 

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构核心20讲 -〔杨波〕

面试现场 -〔白海飞〕

零基础学Java -〔臧萌〕

手机摄影 -〔@随你们去〕

实用密码学 -〔范学雷〕

说透数字化转型 -〔付晓岩〕

攻克视频技术 -〔李江〕

Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕

AI大模型之美 -〔徐文浩〕

好记忆不如烂笔头。留下您的足迹吧 :)