A little complicated conditional branching

If the numerical value num is in the range of 1 or more and 10 or less, True is output. Or, if outside_mode is True, True is output even if the numerical value num is 0 or less and 11 or more.

This process will be described using conditional branching.

Output example:

in1to10(5, false)
 =>True
in1to10(11, false) 
=>False
in1to10(11  true)
=>True

This time we will use logical operators.

#true if both a and b are true
a && b 

#true if either a or b is true
a || b 

Ruby's logical operators evaluate conditional expressions from the left side to the right side, and if the evaluation of the entire expression is confirmed, it does not evaluate at that point.

def in1to10 (num, outside_mode)
  if (num >= 1 && num <= 10) || outside_mode
    puts "True"
  else
    puts "False"
  end
end

By making such a description, it is possible to output as in the output example.

Recommended Posts

A little complicated conditional branching
Also complicated conditional branching
Conditional branching with a flowing interface
A little effort to eliminate complicated if-else
Realize a decision table without using conditional branching
Program using conditional branching
Conditional branching of numbers
Ruby study memo (conditional branching)
[Ruby] Conditional branching Conditional expression order
java (conditional branching and repetition)
Basics of conditional branching and return
Introducing Bitboard-with a little Othello implementation-
A little regular expression story Part 1
A little regular expression story Part 2
Java study # 4 (conditional branching / if statement)
A little summary about typesafe config
A little troublesome story in Groovy
A little understanding of lambda expressions
[Java] Conditional branching is an if statement, but there is also a conditional operator.