Jump to content

Increment a count by +4


jarvis
Go to solution Solved by cyberRobot,

Recommended Posts

Hi All,

 

Incredibly simple I'm sure! I have an if while loop with a counter.

 

$i = 0;

 

How can I rewrite this part :

if($i == 1 || $i == 5 || $i == 9 || $i == 13 ){

 

I tried if($i % 4 === 0){ but that's not right

 

Thanks

Link to comment
Share on other sites

Did you think to test what the result of ($i % 4) would be for different values of $i? If you had it would have shown that you were on the right track, but made one flaw.

for($i=0; $i<14; $i++)
{
    echo "{$i} : mod 4 = " . ($i%4) . "<br>\n";
}

Here are the results of that test (I've highlighted the ones that I think you should pay attention to:

 

 

0 : mod 4 = 0

1 : mod 4 = 1
2 : mod 4 = 2
3 : mod 4 = 3
4 : mod 4 = 0
5 : mod 4 = 1
6 : mod 4 = 2
7 : mod 4 = 3
8 : mod 4 = 0
9 : mod 4 = 1
10 : mod 4 = 2
11 : mod 4 = 3
12 : mod 4 = 0

13 : mod 4 = 1

 

EDIT: cyberRobot beat me to it (because he took the easy route :) ), but I wanted to hopefully teach you how to solve problems like this in the future.

Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.