Programming Lessons

# [PHP]Let’s Try Writing Loop Processes! [for statement] [Programming for Kids and Above #7]

Hello!

I’m kat, the manager of ‘kat’s blog’.

This time, which is the seventh installment of the ‘Programming Lessons’ series, I will be talking about loop processing.

In our daily lives, we do certain things over and over again, right?

For instance, when going to school, we take the same route at the same time everyday.

It’s a hassle to have to write out the same process over and over again to do something repeatedly in programming.

Actually, there is a way to write it out very easily.

Let’s learn how to do that this time!

## What we will use this time

The programming language we will use this time

This time we will use the following programming language to write out a program.

・PHP

The programming environment we will use this time

Let’s use the paiza website which is listed below for executing the program.

※For those who are using this for the first time, please check the page below. It gives instructions on how to use the website.

How to use paiza.io[Basics][Programming with Your Browser][PHP]Hello, I'm kat, the manager of 'kat's blog.' This time I would like ...

## What is loop processing?

As I said in the beginning, loop processing is to do the same thing over and over again many times.

In loop processing, the same process will be repeated for as many times as you choose.

Once the first process is finished, the second process will start after it has returned to the beginning of the loop.

## Let’s write out a loop process

Now then, let’s actually write out a loop process.

For this time, let’s make it so that to the numbers; 1,2,3,4,5,6,7,8,9, 2 will be added to each of them, and the numbers will be displayed in order.

You can think of the process to go like this.

This will be repeated a total of nine times from 1~ 9.

Now, let’s take a look at the source code.

```<?php

// Repeat for 1~9
for (\$i = 1; \$i < 10; \$i++) {
\$result = \$i + 2;
echo \$result . ",";
}
```

When this is executed,

```3,4,5,6,7,8,9,10,11,
```

the result will be displayed as shown above.

As you can see, 2 has been added to each number without fail.

～Explanation 1～

```// Repeat for 1~9
for (\$i = 1; \$i < 10; \$i++) {
```

This is called a for statement, and is used when doing loop processing.

The process that is written after the ‘{‘ of the for statement will be executed repeatedly.

Also this time, the numbers from 1~9 will be in the ‘\$i’ variable. (The variable can be named freely)

‘\$i = 1;’ which is within the ‘()’ of the for statement, means that the numbers that are in ‘\$i’ start from 1.

‘\$i < 10;’ means that the process will be repeated while ‘\$i’ is smaller than 10.

The ‘\$i++’ at the end means that 1 will be added each time ‘\$i’ loops around.

✓ Point

By changing the value in the ‘()’ of the for statement, you can freely change the number of times the loop repeats.

‘for (\$i = 1; \$i < 366; \$i++) {‘

By making it this way, we can make it repeat 365 times.

The ‘<‘ used in ‘\$i < 366;,’ means ‘less than ~,’ so the number that comes to the right of it needs to be 1 larger than the maximum number.

However, if ‘<=’ is used, this means ‘less than or equal to ~,’ so in that case there is no problem in making the maximum number ‘365.’

～Explanation 2～

```    // Add 2 and display
\$result = \$i + 2;
echo \$result . ",";
```

This is a review of up until last time. The following process is being executing in order.

・Add 2 to ‘\$i,’ and enter the result in ‘\$result.’

・Output ‘\$result’ and ‘,’ on the screen.

## Final thoughts

Thank you for reading to the end of this lesson this time as well.

This time we learned about for statements of loop processing, but I think you now understand how useful they are when you want to do multiple processes of the same kind.

I explained about loop processing this time as well, but this almost always comes up when doing programming, so please try to memorize it!

See you next time!