[RAILS] [Ruby] conditional branch if statement

Currently, I am studying while advancing a reference book, but I made an article as an output of what I learned.

if statement

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 elsif else form

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.

Summary

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.

Reference book

・ Ruby on rails 5 that can be used in the field

Recommended Posts

[Ruby] conditional branch if statement
Ruby conditional branch processing
About Ruby if statement
[Ruby] if statement concept of conditional expression
Ruby conditional branch. if, conditional operator (ternary operator), unless, case
Swift conditional branch statement summary
[Ruby] Conditional bifurcation unless statement
[Ruby] problem with if statement
if statement
Java study # 4 (conditional branching / if statement)
About if statement and branch processing
[Ruby] postfix if
Java conditional branch
java conditional branch
[Java] Conditional branch
Ruby if, case
Ruby conditional branch (case, while, infinite loop, break)
Try using conditional branching other than if statement
[Ruby] What if you put each statement in each statement? ??
Ruby Learning # 17 If Statements
10 Corresponds to if statement
Studying Java-Part 10-if statement
Ruby Learning # 16 Return Statement
About for statement and if statement
Ruby study memo (conditional branching)
Ruby Learning # 18 If Statements (Con't)
Let's understand the if statement!
Points for refactoring (if statement)
[Ruby] Introduction to Ruby Error statement
Conditional branch with helper method
[Ruby] present/blank method and postfix if.
[Ruby] If and else problems-with operators-
[Introduction to Java] Conditional branching (if statement, if-else statement, else if statement, ternary operator, switch statement)
[Java] Conditional branching is an if statement, but there is also a conditional operator.
[Java] "Setting" using Enum rather than conditional branching by if statement -Strategy Enum