Looking at the description, I felt that there were many basic expressions, so Let's sort out the expressions that were actually used.
Click here for the actual fizz_buzz problem explanation
fizz_buzz problem
def fizz_buzz
num = 1
while (num <= 100) do
if num % 15 == 0
puts "FizzBuzz"
elsif (num % 3) == 0
puts "Fizz"
elsif (num % 5) == 0
puts "Buzz"
else
puts num
end
num = num + 1
end
end
fizz_buzz
Assignment operator
num = 1 #Variable name=Value to store
--Action by the above description
--The value is stored in a variable
using a description called assignment
.
-- Substitution
--You can put it in the variable box by writing
= after the variable. --
=` is called an assignment operator.
In Ruby, an expression with one = means "assign the value on the right to the variable on the left".
--What is a variable?
--A variable
is like a box to put a value in.
Define the variable name so that you can easily understand what kind of value is included.
Reassignment
num = num + 1
--Reassigning another value to the variable after assigning the value once --Can be changed as many times as you like during the program ――It is also possible to summarize briefly with the following description
Abbreviation for reassignment(Adopt self-assignment operator)
num += 1
--What is an if statement? ――You can divide the process by saying "If it is 〇〇, do △△".
Conditional branching when correct and when not
if num % 15 == 0 #Conditional expression 1
puts "FizzBuzz" #Conditional expression 1 is true(true)Processing to be executed at
elsif (num % 3) == 0 #Conditional expression 2
puts "Fizz" #Conditional expression 1 is false(false)+Conditional expression 2 is true(true)Processing to be executed at
elsif (num % 5) == 0 #Conditional expression 3
puts "Buzz" #Both conditional expression 1 and conditional expression 2 are false(false)+Conditional expression 3 is true(true)Processing to be executed at
else #When not applicable
puts num #Processing to be executed when the above conditional expression is not met
end
Comparison operator+Algebraic operator
while (num <= 100) do #Is num 100 or less?
if num % 15 == 0 #The surplus (divided remainder) is
#Is it equal to 0
elsif (num % 3) == 0 #The surplus (divided remainder) is
#Is it equal to 0
elsif (num % 5) == 0 #The surplus (divided remainder) is
#Is it equal to 0
-** Comparison operator **
A > B
Is A greater than B?
A >= B
Is A more than B?
A < B
Is A smaller than B?
A <= B
Is A less than or equal to B?
A == B
Are A and B equal?
-** Algebraic operator **
--+
Addition
---
subtraction
--*
Multiplication
--/
Division
--%
Remainder (divided remainder)
Recommended Posts