标签: switch-statement

用户模式 ​​vs 管理员模式

我有几个关于类 Unix 机器上的用户模式和主管模式的问题。

用户模式和主管模式有什么区别?我知道用户进程无法访问所有内存和硬件并执行所有指令。还有更多吗?

不同模式有什么好处?

从用户模式切换到管理员模式需要哪些步骤?

当用户程序进行系统调用时,模式必须从用户模式更改为管理员模式。我在别处读到这是通过使用 int x80 在 x86 机器上实现的。那么模式切换与中断处理有何不同?

它与上下文切换有何不同?

不同架构下如何实现监管模式?

任何答案或指示将不胜感激!

supervisor-mode switch-statement

3
推荐指数
1
解决办法
8452
查看次数

Switch语句在字节码中的表示和效率?

虽然 switch 语句可以表示为一系列 if 语句,但似乎在将 Java switch 语句编译成字节码时,使用了不同的方法。

  • 字节码使用的表示是什么?
  • 我认为这种替代表示是出于效率原因,那么效率与 if 语句表示相比如何?
  • 是否有任何其他考虑导致使用这种表示?

java performance bytecode representation switch-statement

3
推荐指数
1
解决办法
1793
查看次数

switch(true)与if(true)不返回相同(JavaScript)

我正在编写一个单击按钮的功能.有很多不同的按钮,所以我想使用一个switch(true)语句而不是一系列的if...else if语句.

有几个按钮用于数字,我把数字放在标签的value属性中<button>.但是,测试这两个函数会产生不同的结果.

switch(true) {
    case (Number(button.value)):
        console.log('thats a number');
        break;
    default:
        console.log('NaN');
        break;
}

if(Number(button.value)) {
    console.log('thats a number');
} else {
    console.log('NaN');
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下原因吗?我已经进行了彻底的测试而没有到达任何地方,并且觉得它必须在方式上有一些技术差异switch并且if处理语句.

javascript switch-statement

3
推荐指数
1
解决办法
215
查看次数

不可能的类型切换案例:不能有动态类型

具有以下结构:

package dto

type CapacityResponse struct{
    Val int
    Err error
    TransactionID string
}

func (r *CapacityResponse) GetError() (error) {
    return r.Err
}

func (r *CapacityResponse) SetError(err error)  {
    r.Err = err
}

func (r *CapacityResponse) Read() interface{}  {
    return r.Val
}

func (r *CapacityResponse) GetTransactionId() string  {
    return r.TransactionID
}
Run Code Online (Sandbox Code Playgroud)

它的界面:

package dto

type Responder interface {
    Read() (interface{})
    GetError() (error)
    SetError(error)
    GetTransactionId() (string)
}
Run Code Online (Sandbox Code Playgroud)

并遵循以下逻辑:

func handler(w http.ResponseWriter, r *http.Request) {
    cr := request2CacheRequest(r)
    responseChan := make(chan dto.Responder)

    go …
Run Code Online (Sandbox Code Playgroud)

interface go switch-statement

3
推荐指数
1
解决办法
779
查看次数

swift:如何在switch case语句中替换if else

我有这个if else检查:

var aItem: CGFloat = 0

if item == 0 {
    aItem = 457

} else if item == 1 {
    aItem = 576

} else if item == 2 {
    aItem = 758

}

print(aItem)
Run Code Online (Sandbox Code Playgroud)

我想用switchcase语句替换这段代码.怎么做?

我试过了:

    var aItem: CGFloat = 0

    switch item {
    case _ where item == 0:

        aItem = 457

    case _ where item == 1:

        aItem = 576

    case _ where item == 2:

        aItem = 758

    default:
        print("this …
Run Code Online (Sandbox Code Playgroud)

switch-statement ios swift

3
推荐指数
1
解决办法
818
查看次数

避免if else实例化一个类 - python

我想根据字段的值创建一个类的对象.

例如:

if r_type == 'abc':
                return Abc()
            elif r_type == 'def':
                return Def()
            elif r_type == 'ghi':
                return Ghi()
            elif r_type == 'jkl':
                return Jkl()
Run Code Online (Sandbox Code Playgroud)

什么是pythonic方法,以避免如果在这里.我正在考虑创建一个字典,其中r_type是key,classname是value,并且获取值并实例化,这是一种正确的方式,还是在python中有更好的惯用方法?

python conditional design-patterns switch-statement

3
推荐指数
1
解决办法
454
查看次数

switch case: case label does not reduce to an integer constant

I am perfectly aware of the mechanism behind the switch statement and why an integer constant is required. What I don't undestand is why the following case label is not considered an integer constant. What is it then? A non-existing variable? Can anyone categorize it? Does the C compiler really need to be so dumb?

struct my_struct {
    const int my_int;
};

switch (4) {

case ((struct my_struct) { 4 }).my_int:

    printf("Hey there!\n");
    break;

}
Run Code Online (Sandbox Code Playgroud)

And of course…

error: case …
Run Code Online (Sandbox Code Playgroud)

c switch-statement

3
推荐指数
1
解决办法
304
查看次数

为什么`switch(null)`是编译错误,但是`switch(str)`可以,因为str是`static final String str = null;`?

虽然switch(null)是一个编译错误,但是switch(str)很好(str被static final String str = null;)。

难道不是static final String str = null; 在编译时就将其替换为switch(str)的编译时常量switch(null)吗?

switch (null) {  // compile Error immediately!

}
Run Code Online (Sandbox Code Playgroud)

但:

public class Test {

    // compile-time constant?
    static final String s4 = null; 

    public static void main(String[] args) {

        switch (s4) {  // compiles fine! NPE at runtime

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

PS我想这static final String str = null; 不是一个编译时常量,因为只有static final String str = 'string …

java null constants compile-time-constant switch-statement

3
推荐指数
1
解决办法
62
查看次数

c++ default not working in switch statement

Fairly new to programming here. I can't get my default code in the switch statement to work. I want it to display an error and exit as soon as any data input that's not in the range is entered. For some reason, it goes to the next cout line after the switch statement.

#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int main()
{
char choice;
const double P=8.00, A=10.50, B=12.50;
double t,w,price;

cout<<"\nPlease enter the state code: "; …
Run Code Online (Sandbox Code Playgroud)

c++ switch-statement

3
推荐指数
1
解决办法
89
查看次数

使用GCC开关案例范围时,是否可以构建范围为[A-Za-z]的单个案例?

非常简单的问题。GCC的Case范围允许以下内容:

switch (c.toLatin1()) {
default: {
    foo();
    break;
} case 'A' ... 'Z': { 
    bar();
    break;
} case 'a' ... 'z': {
    bar();
    break;
}
Run Code Online (Sandbox Code Playgroud)

https://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html

但是,这里的问题bar()是多余的,并且'A' ... 'z'最终将包含许多不需要的字符:

在此处输入图片说明

从概念上讲,是否可能出现以下情况?

switch (c.toLatin1()) {
default: {
    foo();
    break;
} case 'A' ... 'Z' || 'a' ... 'z': { 
    bar();
    break;
}
Run Code Online (Sandbox Code Playgroud)

显然,这是伪代码,但是您知道了。如果需要的话,解决方案可以包括文本宏。我更担心意外引入错误,因为我忘记添加新代码:

switch (c.toLatin1()) {
default: {
    foo();
    break;
} case 'A' ... 'Z': { 
    foo(); // I add here
    bar();
    break;
} case 'a' ... 'z': …
Run Code Online (Sandbox Code Playgroud)

c++ qt gcc duplicates switch-statement

3
推荐指数
1
解决办法
79
查看次数