Home>

Thanks for your attention.
I don't know how to remember the two titles.

①
for ($i = 1;$i>= 100;$i ++)


①Increment by 1 to 100 with initial value

②
$i = 1
while ($i>= 100) {echo $i;$i ++}
③
$I = 1
while ($i>= 100) {echo $i}
④
$I = 2
while ($i>= 100) {if ($i% 2 == 0) {echo $i;}

If i is true between 1 and 100, call variable i one by one?

Calls variable i if true between 3 and 100.
Question 1: I don't understand the meaning of this infinite loop

If variable between 2 and 100 is divisible by 2, call variable i
Question 2: I don't know why if is included

Does question 3① and ② mean the same thing?

Please reply.

  • Answer # 1

    Question 1
    If we assume that $i is less than 100, we will answer
    Since $i does not increase in the loop, naturally $i = 1 will continue to be output.

    Question 2
    Why do you think if is not necessary when you want to see if it is divisible by 2?
    It's an if to check if too much divided by 2 is 0.

    Question 3
    If it is said that it has the same meaning, the result seems to be the same.

  • Answer # 2

    If you don't like if, you can avoid that case


    Do you mean the same thing?

  • Answer # 3

    Counter expression for syntax

    for ($i = 0;$i<10;$i ++) {}

    means

    i = 0;
    while (i<10) {
      ...
      i ++;
    }
    It is roughly equivalent to

    .

    Originally, "for" is used for "repetition whose number of repetitions is known", while "etc" is used for "repetition that does not know how many times it is repeated", and while can do the same as for.

    For questions 2 and 3, echo will "output" the expression that is the argument. In other words, 2 is intended to display the value repeatedly from 1 to 100.
    3 means that the number (%) divided by 2 is 0, which means that "even numbers between 2 and 100 are displayed".

    while is a repetition of "I don't know how many times it repeats", in other words, "It doesn't end as long as the condition is met" If the condition is always met, the iteration will not end. This is an infinite loop.
    Now take a closer look at 2 and 3. The conditional expression of while (possibly with the inequality sign reversed) is $i<= 100.
    As long as this is true, it will loop, but it does not change the value of $i in the loop. That is, even if you go around the loop, the value of $i remains the same.
    That means you can't escape from while forever.

Related articles