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.
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
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.
<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".
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!
① 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