Jump to content

a do run run run while a do run run...


stevew

Recommended Posts

Removing the ; in these while and do while snippets is resulting in different outputs...can someone explain what effect this is having in each? thanks.

http://writecodeonline.com/php/

 

while ($value < 10);

while ($value < 10)

 

 

<?php
$value =20;
while ($value < 10);
{
$value *=2;
echo $value, "<br>";
}
?>

 

output: 40

 

 

 

 <?php
$value =20;
while ($value < 10)
{
$value *=2;
echo $value, "<br>";
}
?>

 

output: none

 

 

 

<?php
$value =20;
do{
echo $value, "<br>";
$value *=2;
} while ($value < 10);
?>

 

output: 20

 

 

 

<?php
$value =20;
do{
echo $value, "<br>";
$value *=2;
} while ($value < 10)
?>

 

output: Parse error

Link to comment
Share on other sites

The semicolon terminates the statement. The curly braces are ignored, and the rest of the code is run procedural, top to bottom.

 

The do...while(); needs a semicolon to terminate the statement.

 

A semi-colon is a very, very important language structure, and changing it's position will have a dramatic effect on the way your code works, or cause it to not work at all.

 

If you'd like a more specific response, narrow your question down to a single example you're curious about.

Link to comment
Share on other sites

The semicolon terminates the statement. The curly braces are ignored, and the rest of the code is run procedural, top to bottom.

 

The do...while(); needs a semicolon to terminate the statement.

 

A semi-colon is a very, very important language structure, and changing it's position will have a dramatic effect on the way your code works, or cause it to not work at all.

 

If you'd like a more specific response, narrow your question down to a single example you're curious about.

 

No that's great thanks...I was just looking for a general explanation. :)

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.