Branching with IF (Ruby for beginner 3 of 11)
Ruby for beginner series (3 of 11)

In Ruby, if a logic has two or more answers, it must use branching logic.
Ruby branching logic can use the if command or case statement.
The if command is more commonly used than case. The case command sometimes has the advantage of being shorter.
If a condition contains two or more conditions, then the two conditions are combined using the && operator, which represents AND and || represents OR.
There is also a ternary operator (? ), which can abbreviate writing the if form if the conditions are simple.
In order to understand it more clearly, it will be explained using the following example:
Below is detail explanation about first practice above:
- first_person = Exam.new(‘John’, ‘Math’, 75)
- create a new object first_person with named John with a math lesson score of 75.
- The check_note method is a simple if-implementation form with 2 answers (Pass/Fail).
- The find_grade method is the implementation of if with more than 2 answers, so it requires the elsif command.
- The check_note_short_version method is the implementation of if with a ternary model by using a question mark ( ? ).
- The check_reward method is to logically do this if it meets the conditions, so the writing between the conditions and what to do is reversed.
- The check_reward_with_unless method is the implementation of if by doing this, unless it doesn’t meet the conditions, don’t
- The give_entertainment_reward method, the implementation of the ||= operator means that if the variable still contains nil (or is empty), give it a candy reward, but if it already has contents, such as a bicycle or a motorcycle, don’t replace it with candy.
- The check_super_reward method is the implementation of a logical if with several conditions combined with the && (AND) operator and the || operator. (OR)
Additional notes
- command If not, it can be replaced with unless
- a || = 2, which means that if variable a is empty (nil), it is filled with 2.
- to combine more than two conditions we can use the word AND / OR
- It is highly recommended to use the symbols && and || in ruby practice because the use of the words and and or does not have priority which order is done first.
- When using && and ||, && is executed first, followed by ||