标签: switch-statement

Haskell开关/案例使用

作为我在Haskell中编写的迷你解释器的一部分,我正在编写一个执行以下操作的函数:如果是eval (App e1 e2),我想递归计算e1(eval e1),将结果设置为v1.然后使用Switch/Case,我想检查模式,v1如果它不是Error,则递归计算e2(eval e2)并将该值设置为v2.使用这两个值v1和v2,然后我申请的另一个功能(appVals这些值).

eval :: Exp -> Error Val
eval (App e1 e2) = appVals v1 v2 where
    v1 = case (eval e1) of
        Error err -> Error "Not an application"
        /= Error err -> eval e1 = v1
    v2 = case (eval e2) of
        Error err -> Error "Not an application"
        /= Error err …
Run Code Online (Sandbox Code Playgroud)

recursion haskell switch-statement

4
推荐指数
1
解决办法
1645
查看次数

在Switch中为Case Statement添加一个附加条件

是否可以在C#中添加一个额外的条件到Switch语句,如下所示

switch(MyEnum)
{ 
  case 1:
  case 2:
  case 3 && Year > 2012://Additional Condtion Here
       //Do Something here..........
      break; 
  case 4:
  case 5:
       //Do Something here..........
      break;  
 }
Run Code Online (Sandbox Code Playgroud)

在上面提到的例子中,如果MyEnum = 3,那么如果年> 2012,它必须被执行...是否可能?

[EDITED]

年份> 2012不适用于案例1和案例2.

c# switch-statement

4
推荐指数
2
解决办法
4万
查看次数

存储在变量与多种不同方法中

使用它的优点和缺点是什么:

String a = new String();
switch (i) {
case 1: a = "Cueck"; break;
case 2: a = "Blub"; break;
case 3: a = "Writing cases is BORING!"; break;
}
System.out.println(a);
Run Code Online (Sandbox Code Playgroud)

与:

switch (i) {
case 1: System.out.println("Cueck"); break;
case 2: System.out.println("Blub"); break;
case 3: System.out.println("Writing cases is BORING!"); break;
}
Run Code Online (Sandbox Code Playgroud)

哪个生成更好的字节码?哪个生成更多字节码?

java variables bytecode switch-statement

4
推荐指数
1
解决办法
91
查看次数

嵌套case,for和if循环的优先级是什么?

我有一个与此类似的程序:

switch(x)
{ case 1:

for(i=0; i<10; i++)
{
   if ((i==3) || (i==4) || (i==5) || (i==6) || (i==7))
   {
      if (foobar[i])
         break;    // i am talking about this break
   }
}
...further program
break;   /not this break
Run Code Online (Sandbox Code Playgroud)

如果foobar[i]是真的,程序是否会突破case标签或for循环?

c break switch-statement subroutine

4
推荐指数
2
解决办法
839
查看次数

如何在Javascript swich-case语句中执行somethnig`final`?

在javascript switch语句中,如果其中任何一个case满意,我想执行一些函数:

switch (myVar){
  case 0:
    do_something_0();
    break;
  case 1:
    do_something_1();
    break;
  // I want to execute myFunc() if myVar === 1 or myVar === 2
}
Run Code Online (Sandbox Code Playgroud)

我提出了拥有辅助变量的想法haveMatched,就像这样.

var haveMatched=false;
switch (myVar){
  case 0:
    do_something_0();
    haveMatched=true;
    break;
  case 1:
    do_something_1();
    haveMatched=true;
    break;
}
if (haveMatched){
  do_finally();
}
Run Code Online (Sandbox Code Playgroud)

我认为可能有更好的方法来实现这一点(例如,如果我不知道default:关键字,我会尝试类似的方式).我做得对,还是我错过了什么?

javascript switch-statement

4
推荐指数
1
解决办法
7536
查看次数

可以切换语句使用变量吗?

下面是声明两个int变量并尝试在switch语句中使用它们的代码.这是C++中的合法操作吗?如果没有,为什么不呢?

int i = 0;
int x = 3;
switch (i)
{
    case x:
    // stuff
    break;

    case 0:
    // other stuff
    break;
}
Run Code Online (Sandbox Code Playgroud)

c++ switch-statement

4
推荐指数
1
解决办法
159
查看次数

如何在swift中单击按钮时更改开关状态

我只是制作一个基本的应用程序,我可以打开和关闭开关告诉我,如果我在一天的某个班级做作业.我遇到的问题是重置按钮,将所有开关更改为关闭.如何使用按钮在swift中更改开关状态?这是我到目前为止所做的:

//
//  ViewController.swift
//  HomeworkManager
//
//  Created by Nate Parker on 9/2/14.
//  Copyright (c) 2014 Nathan Parker. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func resetClicked(sender: AnyObject) {
}

@IBOutlet var spanish: UISwitch
@IBOutlet var algebra: UISwitch
@IBOutlet var …
Run Code Online (Sandbox Code Playgroud)

xcode switch-statement ios swift

4
推荐指数
1
解决办法
2万
查看次数

为什么不能在switch语句中使用元组常量作为大小写

我决定使用Swift的case语句和元组。它看起来像是该语言的较酷功能之一。

我决定玩月/日/年元组。令我惊讶的是,我不能在switch语句中使用常量元组值作为案例。这是一个示例(可以粘贴到Playground中并运行)

import UIKit
typealias mdyTuple = (month: Int, day: Int, year: Int)
let joesBirthday: mdyTuple = (month: 6, day: 7, year: 1978)
let someday: mdyTuple = (6, 7, 1978)

switch someday
{
  //---------
  //The line "case joesBirthday" won't compile.
  //case joesBirthday:
  //  println("Joe was born on this day"
  //---------
case (joesBirthday.month, joesBirthday.day, joesBirthday.year):
  println("Joe was born on this day")
case (joesBirthday.month, joesBirthday.day, let year):
  println("Joe is \(year-joesBirthday.year) today")
default:
  println("Some other day")
}
Run Code Online (Sandbox Code Playgroud)

注释掉的代码case joesBirthday:不会编译(如果重要的话,请在Xcode 6.3中)。下面的情况(我分别列出了joesBirthday元组的所有元素)很难输入,也很难阅读,确实可以工作)

键入此命令时,我的Playground崩溃了Xcode,再次尝试重新启动Xcode崩溃了,所以我在报告错误代码时遇到了麻烦。 …

tuples constants switch-statement swift

4
推荐指数
1
解决办法
438
查看次数

表达式不是iOS目标c中的整数常量表达式

我想使用以下表达式

-(void)SwitchCondn{
    int expression;
    int match1=0;
    int match2=1;

    switch (expression)

    {
        case match1:

            //statements

            break;

        case match2:

            //statements

            break;

        default:

           // statements

            break;

    }
Run Code Online (Sandbox Code Playgroud)

但是我得到了

在此输入图像描述

当我研究我发现

In order to work in Objective-C, you should define your constant either like this:
#define TXT_NAME 1
Or even better, like this:
enum {TXT_NAME = 1};
Run Code Online (Sandbox Code Playgroud)

我很久以来一直在使用这种方法.现在我的变量值将在运行时改变,所以我需要以其他方式定义,我不想使用if else所以有任何方式的声明变量其他方式

我接受了以下研究

为什么我不能在Objective-C中的switch-case语句中使用我的常量?[error = Expression不是整数常量表达式]

Objective C switch语句和命名整数常量

Objective C具有case/switch的全局常量

整数常量'不会减少为整数'

objective-c switch-statement ios

4
推荐指数
2
解决办法
7180
查看次数

swift 2中引入的可选模式的优点/用例有哪些?

对于简单的情况,if let或者guard我没有看到优势,

if case let x? = someOptional where ... {
  ...
}

//I don't see the advantage over the original if let

if let x = someOptional where ... {
  ...
}
Run Code Online (Sandbox Code Playgroud)

对于for-case-let简化使用可选集合的情况,我真的希望swift可以更进一步:

for case let x? in optionalArray {
  ...
}

//Wouldn't it be better if we could just write

for let x? in optionalArray {
  ...
}
Run Code Online (Sandbox Code Playgroud)

谷歌一段时间之后我认为唯一有用的案例是" Swift 2模式匹配:解开多个选项 ":

switch (username, password) {
case let (username?, password?):
    print("Success!")
case …
Run Code Online (Sandbox Code Playgroud)

pattern-matching switch-statement optional swift swift2

4
推荐指数
1
解决办法
295
查看次数