[RUBY] if else practice

Premise
This is a summary of what I learned as a programming beginner (1 to 2 months).
It can be useless or wrong in the real world.
If you notice it, I would appreciate it if you could comment.

Since I did a practice exercise of conditional expression using if, else of Ruby, I will summarize my thoughts while explaining.

Q
① When the variable num is 1 or more and 10 or less When true
② When the variable outside is true
Create an in1to10 method that returns true when either ① or ② is satisfied, and false otherwise.
conditions
&& ||To make using.

&&When||about

First of all, in the conditions**&&When||**Whenはなにかについて解説します。 &&When||は論理演算子When呼ばれ、前後の記述の真偽によってtrueかfalseを返す演算子です。

&& is a and b

First, & can be entered with shift + 6. When two &'s are connected and become &&, it means true when both the preceding and following descriptions are true. For example

a && b

If it is written, it means that it becomes true when both a and b are satisfied.

||Is a or b

"|" Can be entered with shift + . The \ is on the left side of the backspace at the top right of the keyboard. ||Is true if either the preceding or following description is satisfied.

a || b

If it is written, it means that it becomes true when either a or b is satisfied.

Precautions when using logical operators

For conditional expressions that include logical operators, the conditional expressions are evaluated in order from the left side to the right side. When the evaluation is confirmed in this order, the rest of the evaluation will not be performed. In the case of a && b, if a is false, false is confirmed as a whole, so it is not judged whether B is true or false. a ||If it is b, if a is true, it will be true as a whole, so it will not be judged whether b is true or false.

Answer

def in1to10(num,outside)
  if (num<=10 && num=>1) || outside
    puts "True"
  else
    puts "false"
  end
end

num <= 10 && num >= 1 The conditional expression of 1 or more and 10 or less is made by the description of.

** About the authenticity judgment of outside ** In the case of true / false judgment (true / false) with a variable, just describe the variable and it will evaluate whether the value is true or false. There is no need to bother to write something like === true.

With this description, if 1 or more and 10 or less are true, or if outside is true, it is output as True, otherwise it is output as false.

Recommended Posts

if else practice
7th class: if, else
It's not "else if" but "else" and "if"
[Ruby] If and else problems-with operators-
[Practice] ArrayList
Array practice
[Practice] ArrayList
Array practice 2
ArrayList practice
[Practice] Enumeration
if statement