Jump to content

For Loop.


StraightJ

Recommended Posts

Hello everyone.  I am new to PHP, and to programming in general.  As of right now, I am reading and learning from "Learning PHP and MySQL" by Michele E. Davis and Jon A. Phillips.  In the 'for' loop section of chapter 4, the example they give that actually works, is:

 

<?php
for ($num = 1; $num <= 10; $num++) {
      print "Number is $num<br />\n";
}
?>

 

A question at the end of the chapter requests that I make a 'for' loop counting from 10 to 1. I wrote it as follows:

<?php
for ($num = 10; $num >= 10; $num--){
     print "Number is $num<br />";
}
?>

 

For some reason, my code does not execute, but their example above does.  When I checked the appendix answer for creating a 'for' loop counting from 10 to 1, this is the answer the appendix provides:

<?php
for ($num = 10; $num >= 1; $num&#8722;&#8722;){
     print "$num<br>;
}
?>

For some reason, the 3rd expression in the code above is not showing up correctly, but it reads, "$num&#8722;&#8722" if that helps.

 

I don't agree with the appendix, being that it looks radically different than the example used in the chapter, and i have no idea how to decipher the 3rd expression in the for loop.

 

Can someone tell me what I am doing wrong?

Link to comment
Share on other sites

The php manual (www.php.net) has great documentation and would do a better job than I of explaining this than I would. You should always look there first and then post here if you have questions.

 

Here is the page for for loops

http://us2.php.net/manual/en/control-structures.for.php

 

But to explain it in my terms, the first expression is run when the loop is first encountered, The second expression determines when the loop should run - as long as the expression results in true the loop will execute. The third expression is executed on each iteration of the loop.

 

In your example

for ($num = 10; $num >= 10; $num--){

$num is first set to 10 and the loop will run as long as $num >= 10. Also, $num will be decremented by 1 on each iteration. So, the loop will run one time, then the value of $num will be decreased to 9 and will no longer run due to the condition in the 2nd expression.

 

However, in the book's answer

for ($num = 10; $num >= 1; $num--)

There is the same initial value of 10, but the 2nd expression states the loop should run as long as $num >= 1, so on the first iteration the loop runs and the value is again reduced to 9, but since 9 >= 1 the loop will run again and again - until the value of num is NOT >= 1.

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.