# [ruby] drill output

## Contents ## Implementation ## Commentary

This time, I created a program that adds numbers from 1 to 10 in order. Below is the code I first tried. The output is below. This program fails because it is 1 + 2 + 3 + 4 + 5 ...

Then what should I do (-_-;) Let's preach one by one

① Prepare a variable sum to save the total value (2) Add numerical values from 1 to 10 to the variable sum in order. (3) Replace the process of adding in order by repeating the times statement. ④ Output the value of the variable sum to the terminal

First is ①. Add 2 to 1 Add 3 to the result Add 4 to the result Add 5 to the result, and repeat until 10.

Therefore, we need a variable to always save the result. Define this as the variable name sum. Since nothing has been added yet, 0 is assigned to sum.

(2) Add numerical values from 1 to 10 to the variable sum in order. Let's write a program that adds numerical values from 1 to 10 to the variable sum in order. This code is very long and the same process continues, so use the times statement to rewrite it into efficient code.

(3) Replace the process of adding in order by repeating the times statement. You can use the times statement to put together the same process that is repeated many times. This time, the sum + = numerical part is summarized.

In the times statement, write the number of times you want to repeat .times to determine the number of repetitions. This time, 1 to 10 are added in order, so the same process is repeated 10 times. Therefore, I want to repeat it 10 times. What should I do with the numerical part? This number changes as the number of repetitions increases. The first time is 1, the second time is 2, and the third time is 3. In other words, it is the same as the number of repetitions. In the times statement, the number of repetitions is automatically assigned as a numerical value in the variable i. Therefore, the variable i can be used to add the number of repetitions to the variable sum. However, since it is a program, the value of i for the first time will be 0. Therefore, add 1 to i + 1. ④ Output the value of the variable sum to the terminal Finally, output sum to the terminal with the puts method and you're done.