0

This code does echo out 10 to 19, but should it not give 20 as well? Because when it reaches 19 it is still lesser than 20 so the condition is still true and should run the code one last time and put out 1 to 20. I know I can add <= instead, but it should work without it, or what am I missing here?

$num1 = 10;

while ($num1 < 20) {

echo $num1;
$num1++;

}
6
  • 2
    When it is 19, it does execute the code; it is then 20, and does not execute the code. Commented Feb 12, 2018 at 10:23
  • It does exactly what you told it to do. 1.) Check whether $num1 is less than 20 2.) Output the unmodified value of $num1 3.) Increment Either use <= instead of < or increment before you output the value (which i oppose to do since it becomes somewhat unclear in which cycle you are). Commented Feb 12, 2018 at 10:24
  • understood....... Commented Feb 12, 2018 at 10:31
  • 2
    Some years ago, when I started coding, it was a good practice to "run" such small fragments of code using pencil and paper. It helps you understand why it works how it works and not how you imagine it should work. Commented Feb 12, 2018 at 10:35
  • 4
    @axiac, nowadays people start right with SO, as own effort is supposedly overrated. Come here, ask a question, slap it all together and brag to other people you know how to code. [rant over] Commented Feb 12, 2018 at 10:38

1 Answer 1

2

Let's step over what is happening:

$num1 = 10; // $num1 = 10, no issue

while ($num1 < 20) {  // If $num1 is less than 20, do
                      // everything between { and }

    echo $num1;  // print the value of $num1 as it is
                 // at this point of time. Can only be 
                 // in the range from 10 to 19

    $num1++;  // Increment the value of $num1 by 1
}

// Just for fun
echo $num1;  // displays 20 as that is current value 
             // of $num1

At the echo $num1;, you will only ever see a maximum value of 19 due to the while statement $num1 < 20.

After you see the echo of 19, then $num1 will have a value of 20 as that is what the $num1++ says to do.

Alternatively, you could do the following to print the numbers from 11 to 20:

$num1 = 10; // $num1 = 10, no issue

while ($num1 < 20) {  // If $num1 is less than 20, do
                      // everything between { and }

    $num1++;  // Increment the value of $num1 by 1

    echo $num1;  // print the value of $num1 as it is
                 // at this point of time.
                 // This will be in the range from 
                 // 11 (not 10) to 20
}

// And finally for testing
echo $num1;  // displays 20 as that is current value 
             // of $num1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.