Currently, I am studying while advancing a reference book, but I made an article as an output of what I learned.
If you write the if statement in the form of "the process you want to execute when the if condition condition is satisfied end", the program will be executed. The basic form is the sample code below.
if #Condition A
#Conditional expression a
else
#Conditional expression b
end
Use else for processing where the conditional expression of the if statement is false. If the if statement is executed, the else statement will not be executed, and if the if statement is not executed, the else statement will be executed.
If you want to increase the condition to branch with the if statement from 2 patterns to 3 patterns, add the conditional statement with elsif. Below is the sample code.
if #Condition A
#Conditional expression a
elsif #Condition B
#Conditional expression b
else
#Condition c
end
Now I would like to put a value in the sample code.
number = 1
if number == 1
puts 'The number is 1'
elsif number == 2
puts 'The number is 2'
else
puts 'Numbers are other than 1 and 2'
end
When I run the above code, I get'the number is 1'because number == 1 is true.
The if statement later describes a conditional expression, and when the condition is evaluated as true, the processing below it is executed. Also, elsif is described when you want to increase the pattern. It does not have to be, or multiple descriptions are acceptable.
・ Ruby on rails 5 that can be used in the field
Recommended Posts