Jump to content

What am I doing wrong?


canadian_angel

Recommended Posts

        <?php  // Script 6.2 - Odd Numbers

 

        // Address error handling.

        ini_set ('display_errors', 1);

        error_reporting (E_ALL & ~E_NOTICE);

 

        // Validate the Odd Numbers.

 

        /*  Odd Numbers */

 

        $i = 1;

      while ($i < 1 && => 49); {

        echo $i++;

 

// Print out odd numbers between 1-49.

for ($v <= 1; $v > 49; $v++)

print odd $v;

 

}

 

 

?>

Link to comment
Share on other sites

I think the better question to ask here is what are you doing right?  :shrug:

 

Assuming for the minute that the concept was correct your syntax for the following is incorrect.

 

$i < 1 && => 49

 

You have to do it like this:

 

$i < 1 && $i => 49

 

Addressing the logical problem, in your script $i will never be less than 1 (you set it to 1 right before this!). You also shouldn't have a semicolon after while(...) (see the manual on control structures.

 

Finally, you can't just do something like this:

 

print odd $v;

 

To solve your question to print out all odd numbers 0-49, you could take an approach like so:

 

for($i = 0;$i < 50;++$i) {
if($i % 2) {
	echo "$i is odd!<br />\n";
}
}

 

See arithmetic operators for more on the modulus operator.

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.