Larry Ullman's Book Forums

# For Loops

## Recommended Posts

helo i'm doing my logical exercises and i'm getting hard to solve this output using for loops

2

1

4

3

6

6

8

9

##### Share on other sites

You mean you need to output the following integers in the following order?

2

1
4
3
6
6
8
9

Could you please better explain what you want?

Thank you.

##### Share on other sites

You need to find the correct mathematic formula. Simple as that. I'm not really sure you need nested loops for this, but math is in no way my strong suit. That's my feeling though.

##### Share on other sites

@Hartley San, yes that is exactly. could you please help me to code it? im doing it in for loops but with no success.

thanks mate.

##### Share on other sites

Its not entirely clear what you are trying to do - if its just outputting the integers you could set up an array and loop through it.

```\$integers = array(2,1,4,3,6,6,8,9);
foreach (\$integers as \$integer) {
echo '<p>' . \$integer . '</p>';
}
```

##### Share on other sites

I don't know if it that's simple, margaux, but maybe it is? Do you only need these numbers, or is there a pattern you want to find here?

If it's simply the numbers, then this will suffice:

```\$integers = array(2,1,4,3,6,6,8,9);
\$size = count(\$integers);

for (\$i = 0; \$i < \$size; \$i++)
{
echo \$integers[\$i] . ' ';
}
```

##### Share on other sites

I think margaux and Antonio have more or less hinted at the answer.

I don't see any other way to do it without adding a ton of logic within the for loop. I searched Google for the number 21436689, but got nothing of interest. Also, I don't really see any pattern. The only patterns I see are the following:

- The first and second numbers are reversed.

- The same for the third and the fourth.

- The fifth and sixth are both 6.

- The seventh and eighth are one more than their position.

That would require a bunch of logic in a for loop to get there though.

##### Share on other sites

im sorry mates i gave a wrong output, let's change it to this

0+

-2

4+

-4

8+

-6

12+

-8

create a loop that will increment by two and another loop that will increment by four, use for loop. please help me with this example. thanks mates.

##### Share on other sites

Why does this feel like we're doing your homework assignment for you?

I'm not going to give you the answer, but if you look at the spread between values, the answer should become apparent.

In other words, here's the spread between successive values:

0

2

6

8

12

14

18

20

##### Share on other sites

no its not an assignment. i have tried it but the output is this

+2

0

4

8

12

+4

0

4

8

12

+6

0

4

8

12

+8

0

4

8

12

so what i am asking is i cant separate the loop that will increment by 4.  what i wanted to do is to alternate them by two's as i have given the output. its not an assignment trust me. all im asking is for your help i need the actual code to compare to my answer. anyone of you could help me? its just an example and part of my studying the "nested loops".

##### Share on other sites

im sorry mates i gave a wrong output, let's change it to this

0+

-2

4+

-4

8+

-6

12+

-8

create a loop that will increment by two and another loop that will increment by four, use for loop. please help me with this example. thanks mates.

The following worked for me:

for (\$i=0, \$j=-2 ;  \$i<=12 && \$j >= -9 ; \$i+=4, \$j-=2){

echo '+'.\$i. ' '. \$j. ' ';

}

##### Share on other sites

Would it be appropriate to start a thread with coding challenges?

[just a thought - perhaps separate threads depending on difficulty/experience ]

##### Share on other sites

Sure, go ahead, StephenM. Sounds exciting.

Also, jabsalud, sorry for making the wrong assumption. The way you phrased everything made it sound like it was a school assignment or something. That's why I was a bit apprehensive to answer before.

Anyway, StephenM, if your solution works, great, but I think the following is much simpler:

```<?php

for (\$i = 0; \$i < 8; \$i++) {

echo ((\$i % 2 === 0) ? \$i * 2 : -\$i - 1) . '<br>';

}```

##### Share on other sites

Sure, go ahead, StephenM. Sounds exciting.

Also, jabsalud, sorry for making the wrong assumption. The way you phrased everything made it sound like it was a school assignment or something. That's why I was a bit apprehensive to answer before.

Anyway, StephenM, if your solution works, great, but I think the following is much simpler:

```<?php

for (\$i = 0; \$i < 8; \$i++) {

echo ((\$i % 2 === 0) ? \$i * 2 : -\$i - 1) . '<br>';

}```

Yes, in fact no need for 2 variables looping but  there are probably at least a dozen ways to do it.

```for (\$i=0; \$i >=  -8; \$i-= 2) {

echo \$i.'<br>';
if ((abs(\$i) > 0) && (abs(\$i) <8)) {echo '+'.(abs(\$i)*2).'<br>';}
}
```

##### Share on other sites

Yes, in fact no need for 2 variables looping but  there are probably at least a dozen ways to do it.

```for (\$i=0; \$i >=  -8; \$i-= 2) {

echo \$i.'<br>';
if ((abs(\$i) > 0) && (abs(\$i) <8)) {echo '+'.(abs(\$i)*2).'<br>';}
}
```

Sure, go ahead, StephenM. Sounds exciting.

Although, I'm not sure where to find them. I'll have a look around --> only worth it if a bit of interest on the forum.

##### Share on other sites

Sure, go ahead, StephenM. Sounds exciting.

Also, jabsalud, sorry for making the wrong assumption. The way you phrased everything made it sound like it was a school assignment or something. That's why I was a bit apprehensive to answer before.

Anyway, StephenM, if your solution works, great, but I think the following is much simpler:

```<?php

for (\$i = 0; \$i < 8; \$i++) {

echo ((\$i % 2 === 0) ? \$i * 2 : -\$i - 1) . '<br>';

}```

thanks stephenM and hartleysan i really appreciate your works, your codes work perfect thanks again

## Join the conversation

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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