java - 适合什么控制流程 "if A, then do a; if B, then do a, b; if C, then do a, b, c ..."

标签 java python c control-flow

在C语言中,可以使用级联switch语句轻松实现此流程:

switch (var) {
    case c: C();
    case b: B();
    case a: A();
    default: // no op
}

是否有其他编程语言可以支持此流程?例如。在 Python、Java 中?

最佳答案

您可以使用任何具有 if 语句和逻辑 OR 运算符(这将是全部)的语言来支持该流程

flow = 0;

if ( var == a ) {
   A();
   flow = 1;
}

if ( flow || var == b ) {
   B();
   flow = 1;
}

if ( flow || var == c ) {
   C();
   flow = 1;
}

// and so on

关于java - 适合什么控制流程 "if A, then do a; if B, then do a, b; if C, then do a, b, c ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28792264/

相关文章:

java - 如何获取任何给定驱动程序的 WebDriver 客户端日志(JSON 有线通信)?

python - 有没有一种巧妙的方法来使用 pandas (或其他 python 工具)检查数组中所有值的区间是否包含在内?

python - 如何从 Python 优雅地终止 GLib 主循环

c - TCP socket编程中服务端如何判断消息是哪个客户端发送的

C - 索引问题

java - HQL : Adding colon seperated strings for multiple parameters to search with

java - 在 Android 上验证数字签名

java - Hibernate、Spring、@Transactional - 围绕 try/catch?

python - 按数字和字母顺序对两个元素元组的列表进行排序

你能从连接的 TCP 套接字中确定源 IP 和端口吗?