About Ruby if statement

What to explain in this article ① What is an if statement? ② How to use the if statement ③ Further branch with elsif

I would like to briefly explain the above three points about the if statement.

① What is an if statement?

In a nutshell, it's a grammar for conditional bifurcation.

When the result of evaluating the conditional expression is true, the expression below true is evaluated, and if the conditional expression is false, the condition of elsif or else is evaluated.

Also, if only if is written, processing will be performed only when it is true.

Comparison operators are often used in if statements. 「<」「<=」「>」「>=」「==」「!=」 In a nutshell, it's for comparison! If you are interested in comparison operators, please check it out.

Operator article http://www.tohoho-web.com/ruby/operators.html

② How to use if statement

Write the conditional expression next to if and write the process below it. Use elsif for further bifurcation (elsif will be explained later).
if conditional expression 1 then
Process A: Processed when conditional expression 1 is true
else
Process B Processed when all conditional expressions are false
end

The "then" in this if statement can be omitted. It will be abbreviated below.

Pattern with only if

```ruby if conditional expression 1 Process A: Processed when conditional expression 1 is true, and do nothing if false end

 <h4> Simple example </h4>

```ruby
if 0 < 10
    puts "true"
else
    puts "is false"
end

#Since the result of processing is true, if processing is executed
if 10 < 0
    puts "true"
else
    puts "is false"
end

#Since the result of the process is false, the else process is executed.

What do you mean by conditional expressions "true" and "false" ...?

For example The conditional expression "if 10 <0" becomes false "because 0 is not greater than 10" The conditional expression "if 0 <10" is true "because 10 is greater than 0".

③ Further branch with elsif

What is elsif? It is executed when the conditional expression of ... if is false and the conditional expression of elsif is true.

Speaking in the figure below If conditional expression 1 of if is true, process A is executed and When conditional expression 1 is false and conditional expression 2 of elsif is true, process B is executed. If all conditional expressions are false, process C will be executed.

if conditional expression 1
Process A: Processed when conditional expression 1 is true

elsif conditional expression 2
Process B: Processed when conditional expression 1 is false and conditional expression 2 is true

else
Process C Processed when all conditional expressions are false
end

If you use elsif, you can branch further, so you can increase the conditions!

Summary

① What is an if statement? Grammar for conditional bifurcation ② How to use if ... Write a conditional expression next to if and write a process below it ③ What is elsif? Description to increase the number of branches

Reference article https://docs.ruby-lang.org/ja/latest/doc/spec=2fcontrol.html#if

https://pikawaka.com/ruby/if

Recommended Posts

About Ruby if statement
About for statement and if statement
[Ruby] problem with if statement
if statement
[Ruby] conditional branch if statement
About if statement and branch processing
[Ruby] postfix if
About Ruby symbols
About ruby ​​form
About Ruby Hashes
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Hashes
About Ruby Symbols
About Ruby variables
Ruby if, case
About Ruby methods
[Ruby] if statement concept of conditional expression
About Ruby Kernel Module
About Ruby error messages
About Ruby exception handling
About Ruby Hashes (continued)
About eval in Ruby
Ruby Learning # 17 If Statements
About Ruby instance methods
10 Corresponds to if statement
Studying Java-Part 10-if statement
[Ruby] About instance generation
[Ruby] What if you put each statement in each statement? ??
Ruby Learning # 16 Return Statement
About the [ruby] operator
Thinking about logic Ruby
Ruby Learning # 18 If Statements (Con't)
Explanation about Ruby Range object
Let's understand the if statement!
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
About Ruby hashes and symbols
Points for refactoring (if statement)
[Ruby] Introduction to Ruby Error statement
[Ruby] Conditional bifurcation unless statement
Ruby About various iterative processes
About Ruby and object model
About Ruby classes and instances
Explanation about Ruby String object
[Ruby] present/blank method and postfix if.
[Swift] "for-in statement" about iterative processing
About Ruby single quotes and double quotes
About Ruby product operator (&) and sum operator (|)
[Super Introduction] About Symbols in Ruby
[Ruby] If and else problems-with operators-
Java study # 4 (conditional branching / if statement)
About =
About object-oriented inheritance and about yield Ruby
[Ruby] Review about nesting of each
Explanation about Array object of Ruby
[Ruby] I tried to diet the if statement code with the ternary operator
[Ruby basics] About the role of true and break in the while statement
[Ruby on Rails] About bundler (for beginners)
[Ruby] Exception handling, give back if given