Jump to content
Larry Ullman's Book Forums

Recommended Posts

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>';
}
  • Upvote 2
Link to comment
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] . ' ';
}
  • Upvote 1
Link to comment
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.

Link to comment
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. 

Link to comment
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

Link to comment
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".

Link to comment
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. ' ';

}

Link to comment
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>';
    
  }
Link to comment
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>';}
}
Link to comment
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.

Link to comment
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

Link to comment
Share on other sites

 Share

×
×
  • Create New...