我想知道是否可以将多个if
语句重写为switch
。
问题是switch
运行:
案件通过检查后的所有代码。这就是为什么case语句在第一个案例之后运行所有代码的原因。
let arr = [1, 3];
if( arr.includes(1) === true ) {
console.log('if 1');
}
if( arr.includes(2) === true) {
console.log('if 2');
}
if( arr.includes(3) === true) {
console.log('if 3');
}
switch( true ){
case arr.includes(1):
console.log('switch 1');
case arr.includes(2):
console.log('switch 2');
case arr.includes(3):
console.log('switch 3');
}
let arr = [1, 3];
if( arr.includes(1) === true ) {
console.log('if 1');
}
if( arr.includes(2) === true) {
console.log('if 2');
}
if( arr.includes(3) === true) {
console.log('if 3');
}
switch( true ){
case arr.includes(1):
console.log('switch 1');
break;
case arr.includes(2):
console.log('switch 2');
break;
case arr.includes(3):
console.log('switch 3');
break;
}
所以问题是:如何将多个if
语句重写为单个switch
语句?
如果我不能:是否还有比if
多个语句更优雅的语法,这显然表明我正在进行类似的比较?
答案 0 :(得分:5)
如何将多个if语句重写为单个switch语句?
如果你想要多个案例匹配,你不能合理。 switch
可以替换if
/ else
,但不能替换一系列独立的if
,其中不止一个可以匹配。
是否有比if语句更优雅的语法,这显然表明我正在进行类似的比较?
这里的答案将倾向于特定于您正在编写的代码。有几种选择:
每当你有代码在一遍又一遍地做同样的事情时,参数化它并把它放在一个函数中,然后用参数重复调用函数。
function doTheThing(value) {
if (arr.includes(value)) {
console.log('xyz ' + value);
}
}
例如,在您的示例中:
function doTheThing(value) {
if (arr.includes(value)) {
console.log('xyz ' + value);
}
}
let arr = [1, 3];
doTheThing(1);
doTheThing(2);
doTheThing(3);
或
let arr = [1, 3];
[1, 2, 3].forEach(value => {
if (arr.includes(value)) {
console.log("xyz " + value);
}
});
或组合:
function doTheThing(value) {
if (arr.includes(value)) {
console.log('xyz ' + value);
}
}
let arr = [1, 3];
[1, 2, 3].forEach(doTheThing);
如果你正在做不同的事情,一个常见的做法就是有一个价值对行动的查找表,例如:
const actionsByValue = {
1() {
console.log("This is the thing for #1");
},
2() {
console.log("This is something else for #2");
},
3() {
console.log("Different logic again for #3");
}
};
const nop = () => { };
let arr = [1, 3];
arr.forEach(value => {
(actionsByValue[value] || nop)(value);
});
1() { }
符号可能看起来很奇怪,因为您没有经常看到带有数字名称的属性的方法表示法,但它完全有效。在不支持方法表示法的旧环境中:
const actionsByValue = {
1: function() {
console.log("This is the thing for #1");
},
2: function() {
console.log("This is something else for #2");
},
3: function() {
console.log("Different logic again for #3");
}
};
附注:=== true
Array#includes
永远不需要 //Include required preprocessors
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#define SHARED_MEM 1024 //shared memory size
int main(void)
{
/* The child process' new program. This program replaces the parent's */
/* program when 'fork()' is called */
printf("Process[%d]: child in execution ... \n",getpid());
int MAX = 15;
int shmID, shmID2;
key_t key = 1234, key2 = 2345;
int *test, *counter;
shmID = shmget(key, SHARED_MEM, 0666);
printf("\nShmID: %d", shmID);
shmID2 = shmget(key2, SHARED_MEM, 0666);
printf("\nshmID2: %d", shmID2);
test = (int *) shmat(shmID, 0, 0);
counter = (int *) shmat(shmID2, 0, 0);
printf("\ntEST before assignment: %d", *test);
printf("\nCounter: %d", *counter);
*test = 0;
*counter = 1;
printf("\ntest: %d", *test);
printf("\nCounter%d", *counter);
printf("\nAlmost there...");
if (*counter == 1){
for(int i=0; i < MAX; i++){
printf("\MAX: %d", MAX);
printf("%d", *test);
*test++;
}
*counter++; //to enter second condition of second child process
printf("\nCounter: %d", counter);
}
else if(*counter == 2){
for(int j = 0; j < MAX; j++){
printf("%d", *test);
*test++;
}
*counter--;
}
sleep(1);
printf("Process[%d]: child terminating ... \n", getpid());
shmdt(test);
shmdt(counter);
return 0;
}
。它总是返回一个布尔值。