[RUBY] Comparison operators and conditionals

What is a comparison operator?

Used when comparing values. The writing style seems to be similar to what I learned in mathematics, but it is slightly different. Some of the comparison operators are described below.

Comparison operator Conditional expression meaning
> A > B Is A greater than B?
>= A >= B Is A more than B?
< A < B Is A smaller than B?
<= A <= B Is A less than or equal to B?
== A == B Are A and B equal?

Boolean value

The boolean value indicates whether the expression is correct or different. True if the value is true False if the value is false It is represented by.

Operators that can check the authenticity of expressions and perform operations on boolean values ​​are called logical operators.

Logical operator OR( | | )

When used with boolean values, it returns true if either of the operations is true, or false if both are false. A || B (Image like A or B)

Logical operator AND (&&)

When used with booleans, it returns true if both of the operations are true, false otherwise. A && B (image like A and B)

Logical operator NOT (!)

The! (Exclamation mark) is called the not operator and is used to mean negation. ! =, which is a combination of! And =, returns true if the values ​​are not equal.

A! = B (A and B are not equal)

if statement

If it is 〇〇, it is a thing that branches the process depending on the condition like □□

if conditional expression # Processing is executed when this conditional expression is true
processing
end

If else is used, the process to be executed when the conditional expression is false is applied.

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

You can create more complex conditionals with elsif

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's all.

Recommended Posts

Comparison operators and conditionals
11 Corresponds to comparison and logical operators
[Java] String comparison and && and ||
Basic operators and operations
Java for beginners, expressions and operators 1
Java for beginners, expressions and operators 2
[Ruby] If and else problems-with operators-
[Ruby] Boolean values ​​and logical operators
Ruby's && and || are not conditional operators
Comparison of Android Handler Looper and runOnUiThread
Use inequality comparison operators in MyBatis SQL
Differences between preface and postfix of operators
Thorough comparison of Android and iOS implementations
[Java / Swift] Comparison of Java Interface and Swift Protocol
Accuracy comparison between Pivot Gaussian elimination and Gaussian elimination
[Java] Collection and StringBuilder operation method comparison
Basic CRUD comparison between Mybatis and Hibernate
Comparison of JavaScript objects and Ruby classes
[Java] Calculation mechanism, operators and type conversion