0

I'm a beginner learning PHP. I have tried to make a loop that has a different behaviour for both even and odd numbers. I've been playing around with it for a while, yet I still can't get it to work. Has anyone got a solution?

$count = 0;
$mod = $count % 2;

while ($count < 10)
{
    if ($mod == 0) {
        echo "even, ";
    } else {
        echo "odd, ";
    }
    $count++;
}
1
  • 1
    just put $mod = $count % 2; inside the while loop. :) Commented Nov 29, 2014 at 10:48

4 Answers 4

4

A silly mistake, mod inside while() loop.

$count = 0;
while ($count < 10) {
    $mod = $count % 2; //Here
    if ($mod == 0) {
        echo "even, ";
    } else {
        echo "odd, ";
    }
    $count++;
}
Sign up to request clarification or add additional context in comments.

Comments

2
$count = 0;
$mod = $count %2;

Is were your problem is.

You have to use the modulus (%) operator inside the for loop. Also, there is no need to store the value from the use of the modulus operator at all, it can be compared directly inside the for-loop.

for ($count = 0; $count < 10, $count++) {
    if ($count % 2 == 0) {
        echo "even, ";
    } else {
        echo "odd, ";
    }
}

You can also switch the while to a for like this.

Welcome to PHP.

Edit #1: As you are getting a new value of $count every execution of the for-loop the old value if $count % 2 will be incorrect. It has to recalculate for every $count. First it checks if 0 is divisible by 2, then onto 1 and so forth. For every value of $count you have to check the divisibility.

In most programming languages you aren't computing a variable onto another, instead you are taking the value of the variable. Like $a = $b + $c; in that case, if you change the value of $b or $c it does not automatically update $a. Instead you have to call $a = $b + $c again. It is the same with % operator.

4 Comments

Thanks, why do you have to put % inside the mod loop though if I stored it inside a variable? it doesnt make sense
The % operator works in the same way as any arithmetic operation. If you do this int a = b + c; then later add to c it will not automatically update a. Instead you have to again write a = b + c; A bit off-topic, what you want to do is possible n some languages. Were you can work with references were it is possible.
I don't really understand what you said, sorry, can you reword it?
@DanielSpajic I hope it was more clear now, I added an update to my answer.
1
 $count = 0;

while ($count < 10) {
    $mod = $count % 2;
    if ($mod == 0) {
        echo "even, ";

    } else {
        echo "odd, ";
    }

    $count++;
}

Comments

0

use for loop instead of while loop

 for($count=0;$count<10;$count++)
 {
   if(($count % 2) == 0)
     echo "even,";
   else
     echo "odd,";
 } 

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.