Ruby conditional branch processing

Conditional branch processing

Comparison operators (>,> =, <, <=, ==)

Comparison operator How to use Description

A> B Is A greater than B? = A> = B Is A more than B? <A <B A is less than B <= A <= B Is A less than or equal to B? == A == B Are A and B equal?

true, false

python


#Is 1 less than 20?
irb(main):001:0> 1 < 20
=> true

#Is 5 less than 5?
irb(main):002:0> 5 <= 5
=> true

#Is 1 greater than 20?
irb(main):003:0> 1 > 20
=> false

#Is 5 more than 5?
irb(main):004:0> 5 >= 5
=> true

#Is 2 x 5 equal to 10?
irb(main):005:0> 2 * 5 == 10
=> true

#Is 10 equal to 20?
irb(main):006:0> 10 == 20
=> false

Logical operator (!)

Operators that can check the authenticity of expressions and perform operations on boolean values are called logical operators. ! (Exclamation mark) is called the not operator and is used in the negative sense. ! =, which is a combination of! And =, returns true if the values are not equal in the opposite sense of ==.

python


#Isn't 2 x 3 6?
irb(main):001:0> 2 * 3 != 6
=> false
#Isn't 2 x 3 10?
irb(main):002:0> 2 * 3 != 10
=> true

if statement

The if statement can branch the process to be executed depending on whether the conditional expression is correct or incorrect, that is, true or false.

python


if conditional expression
processing
end

else (if statement)

Use else to process when the conditional expression is false in the if statement. Describe the processing when the condition is not met between else ~ end

python


if conditional expression
  #Conditional expression is true(true)Processing to be executed at
else
  #Conditional expression is false(false)Processing to be executed at
end

elsif (if statement)

elsif describes the processing when the conditional expression is false like else, but you can add the conditional expression like if when the condition is not satisfied.

python


if conditional expression 1
  #Conditional expression 1 is true(true)Processing to be executed at
elsif conditional expression 2
  #Conditional expression 1 is false(false)At that time
  #Conditional expression 2 is true(true)Processing to be executed at
else
  #Both conditional expression 1 and conditional expression 2 are false(false)Processing to be executed at
end

That is, the conditional branch of positive, negative, or 0 is as follows.

That's all from the scene!

Recommended Posts

Ruby conditional branch processing
[Ruby] conditional branch if statement
Java conditional branch
java conditional branch
[Ruby] Iterative processing
Ruby Iterative Processing
[Java] Conditional branch
Ruby conditional branch (case, while, infinite loop, break)
Ruby conditional branch. if, conditional operator (ternary operator), unless, case
Ruby study memo (conditional branching)
Swift conditional branch statement summary
[Ruby] Conditional branching Conditional expression order
[Ruby] Conditional bifurcation unless statement
Conditional branch with helper method
[Ruby ~ Iterative processing ~] Study memo 4
Memorandum (other conditional bifurcation, iterative processing)
Introduction to Ruby processing system self-made
Memorandum (Ruby: Basic grammar: Iterative processing)
About if statement and branch processing
[Beginner] Various iterative processing for Ruby arrays
[Ruby] if statement concept of conditional expression
[Java ~ Conditional branching / Iterative processing ~] Study memo (3)
Rails6 OmniAuth twitter Authentication Email conditional branch