Please advice how the follwoing code works

Reply

Join Date: Jun 2009
Posts: 38
Reputation: Jintu is an unknown quantity at this point 
Solved Threads: 0
Jintu Jintu is offline Offline
Light Poster

Please advice how the follwoing code works

 
0
  #1
Jul 20th, 2009
I dun quite understand how this program works.......
Please advice...
What is the output of this program?

  1. <html>
  2. <!-- test01.php -->
  3. <body>
  4.  
  5. <?php
  6. $x = 2;
  7. for ($i = 1; $i <= 6; $i++)
  8. {
  9. if ($i % 2 == 1)
  10. {
  11. $x++;
  12. }
  13. print $i . 'x' . $i * $x . " ";
  14. }
  15.  
  16. ?>
  17.  
  18. </body>
  19. </html>
  20.  
  21.  
  22.  
  23.  
  24.  
  25. *******
  26. Answer:
  27.  
  28. 1x3 2x6 3x12 4x16 5x25 6x30

Please advice...
Thanks,

Tony
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: Please advice how the follwoing code works

 
0
  #2
Jul 20th, 2009
This sounds like a homework problem to me, maybe you should to a little more studying. Why else would you include the "answer."
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 38
Reputation: Jintu is an unknown quantity at this point 
Solved Threads: 0
Jintu Jintu is offline Offline
Light Poster

Re: Please advice how the follwoing code works

 
0
  #3
Jul 20th, 2009
Originally Posted by paradox814 View Post
This sounds like a homework problem to me, maybe you should to a little more studying. Why else would you include the "answer."
This is an example of the kind of question that might appear in the final exam and although I have done 4 weeks of PHP, I dun quite get the reason for the output, which was given by my prof...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,081
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster

Re: Please advice how the follwoing code works

 
0
  #4
Jul 20th, 2009
You've spent more time on daniweb than hitting your books. Not a bad thing to do, but you need to do your own work now.

Clue: what type of numbers does $i % 2 == 1 give you??
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 38
Reputation: Jintu is an unknown quantity at this point 
Solved Threads: 0
Jintu Jintu is offline Offline
Light Poster

Re: Please advice how the follwoing code works

 
0
  #5
Jul 20th, 2009
Originally Posted by ardav View Post
You've spent more time on daniweb than hitting your books. Not a bad thing to do, but you need to do your own work now.

Clue: what type of numbers does $i % 2 == 1 give you??
I guess, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, etc...
Am I correct?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,081
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster

Re: Please advice how the follwoing code works

 
0
  #6
Jul 20th, 2009
Give the man a cigar! Now work out the relationship to the output or series. Oops almost gave it away.
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 38
Reputation: Jintu is an unknown quantity at this point 
Solved Threads: 0
Jintu Jintu is offline Offline
Light Poster

Re: Please advice how the follwoing code works

 
0
  #7
Jul 20th, 2009
Originally Posted by ardav View Post
Give the man a cigar! Now work out the relationship to the output or series. Oops almost gave it away.
X = 2 rite,
Then howcome the answer to the code is:
1 x 3, 2 x6, 3 x 12, 4 x16, 5 x 25, 6 x30

x++(shouldn't it be 3, 5, 7, 9, 11, 13)

I am confused...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,081
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster

Re: Please advice how the follwoing code works

 
0
  #8
Jul 20th, 2009
Originally Posted by Jintu View Post
X = 2 rite,
Then howcome the answer to the code is:
1 x 3, 2 x6, 3 x 12, 4 x16, 5 x 25, 6 x30

x++(shouldn't it be 3, 5, 7, 9, 11, 13)

I am confused...
Mais non, mon cheri.
Originally Posted by Blaise Pascal
You always admire what you really don't understand
It would be wrong of me to arrest your admiration.
Last edited by ardav; Jul 20th, 2009 at 1:41 pm.
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 38
Reputation: Jintu is an unknown quantity at this point 
Solved Threads: 0
Jintu Jintu is offline Offline
Light Poster

Re: Please advice how the follwoing code works

 
0
  #9
Jul 20th, 2009
Originally Posted by ardav View Post
Mais non, mon cheri.


It would be wrong of me to arrest your admiration.
Could you please help me one last time on this, I am really lost...

Thanks,

Tony
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,081
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster

Re: Please advice how the follwoing code works

 
0
  #10
Jul 20th, 2009
Paid รข bod yn wirion, gwranda, rwyt wedi derbyn sawl darn o god yn rhad ac am ddim, cer i wneud gwaith dy hun.

In a word, no.

I think your semester average has been bumped enough already. You get the grades you deserve at the end.

False dawns and dead ends are the making of men. What feeble excuse of a man shall you turn out to be if you continue to complete a crossword by first checking the answers?

Go forth and pound thy books.
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 364 | Replies: 9
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC