Multiple Argument Statement

So, today I passed the test for computer science, and I had to write down a large number of consecutive if statements. They all had the same basic arguments, only the conditions were different. All of this made me wonder if there were several argument switching operators somewhere. An example of what I'm thinking of is the following:

int i = 7;

switch(i > 4, i < 10) {
    case T, T:
        return "between 4 and 10";

    case T, F:
        return "greater than 10";

    case F, T:
        return "less than 4";

    case F, F:
        return "your computer thinking is very odd";
}

In this case, the arguments i > 4and i > 10, and T, Fare the argument true or not.

I know that this example can be easily done in other ways, but I'm just trying to show its use. And what if there were 4 arguments, it would be like 20 if-statements, each of which would require repeating the conditions.

, : - , ? ? ?

+5
6

, " ", - , . .

, , , T, F .. , " " " , " ") (.. , ). , , . , , - .

+6

, , bool int, . . , 0 , .

int i = 7;

int range = (i>4) | (i<10)*2;

switch(range) {
    case 3: return "between 4 and 10";
    case 2: return "greater than 10";
    case 1: return "less than 4";
    case 0: return "your computer thinking is very odd";
}
+3

Mathematica:

Switch[{i > 4, i < 10},
  {True, True},   "between 4 and 10",
  {True, False},  "greater than 10",
  {False, True},  "less than 4",
  {False, False}, "your computer thinking is very odd"
]

, , 4 4, 10 10, .

:

Which[
  i < 4,        "less than 4",
  4 <= i <= 10, "between 4 and 10",
  i > 10,       "greater than 10",
  True,         "your computer thinking is very odd"
]
+3

Python:

i = 7

test = (i > 4, i < 10)

print test

if test == (True, True):
    print "between 4 and 10"

elif test == (True, False):
    print "greater than 10"

elif test == (False, True):
    print "less than 4"

elif test == (False, False):
    print "your computer thinking is very odd"
+1

. , ruby:

i = 7
case ([i > 4, i < 10])
when [true, true]:
    puts "between 4 and 10"
when [true, false]:
    puts "greater than 10"
...
end

groovy:

i = 7
switch([i > 4, i < 10]) {
case [[true, true]]:
    println "between 4 and 10"
    break;
case [[true, false]]:
    println "greater than 10"
    break;
...
}
+1

, , , . .

if (- if ((i > 4) && (i < 10)) ).

Switch keys can be modeled as return values ​​of a function expressed in some specific domain way, again, in many languages, even in most cases.

0
source

All Articles