Jump to content

Loops Problem


Sanjib Sinha

Recommended Posts

A simple and same loop code gives two kind of output

here is the first code:

<?php
$count = 0;
while($count <= 10){
    if($count == 5){
	echo "FIVE" . "<br>";
}
else 
	echo "Count: " . $count . "<br>";
$count++;
}
echo "Total Count: " . $count;
?>

It gives output:

Count: 0

Count: 1

Count: 2

Count: 3

Count: 4

FIVE

Count: 6

Count: 7

Count: 8

Count: 9

Count: 10

Total Count: 11

It seems perfectly normal

Now the next code:

<?php
for($count = 0; $count <= 10; $count++){
    if($count == 5){
	echo "FIVE" . "<br>";
}
else 
	echo "Count: " . $count . "<br>";
$count++;

}
echo "Total Count: " . $count;
?>

It gives output:

Count: 0

Count: 2

Count: 4

Count: 6

Count: 8

Count: 10

Total Count: 12

Why this change? Where all odd numbers gone?

 

Link to comment
Share on other sites

Sure, let's break them down:

 

WHILE loop while

while(condition is true)

{

  execute code between the braces

}

 

$count = 0;
while($count <= 10)

Set a start point, 0.  Then while $count (which starts at 0) is less than or equal to 10, execute the code in the braces.  Less than or equal to 10 is the same thing as saying "when $count is greater than 10, stop executing, and go on to next block of code (after the { } of the while loop)

{
    if($count == 5){ //Check what $count is, if it's 5 , echo FIVE
      echo "FIVE" . "<br>";
   }
   else //If it's not five, echo it's current value
      echo "Count: " . $count . "<br>";
   $count++;// increment the $count by +1, and go back to the while(condition) area
}
echo "Total Count: " . $count;//echo $counts final value.

It is 11 because on pass 10, the condition is still true, so it increments and goes to check if the condition is still valid (it's not now)


 

FOR loop for

for(expression 1; expression 2; expression 3)

[evaluate expression one] [run expression 2]

{

  do code between braces

}

[run expression 3]

 

--commonly thought of as:

for(start value; condition check; action) //at least to me

for($count = 0; $count <= 10; $count++)

The first expression initializes $count at 0.

The second checks that $count is less than or equal to (not greater) than 10.

The third is what to do after the loop has ran through it's { code } once

{
    if($count == 5){//if $count == 5, echo FIVE
      echo "FIVE" . "<br>";
   }
   else //else echo the value
      echo "Count: " . $count . "<br>";
   $count++;//increment count
}//LOOP HAS FINISHED, RUN THIRD EXPRESSION $count ++

As you can see, you have already incremented it in your code, then before the loop is run again, the third expression in the for loop runs, incrementing the $count variable

echo "Total Count: " . $count;

?>

 

Hopefully that isn't too confusing?

 

Link to comment
Share on other sites

Sure, let's break them down:

 

WHILE loop while

while(condition is true)

{

  execute code between the braces

}

 

$count = 0;
while($count <= 10)

Set a start point, 0.  Then while $count (which starts at 0) is less than or equal to 10, execute the code in the braces.  Less than or equal to 10 is the same thing as saying "when $count is greater than 10, stop executing, and go on to next block of code (after the { } of the while loop)

{
    if($count == 5){ //Check what $count is, if it's 5 , echo FIVE
      echo "FIVE" . "<br>";
   }
   else //If it's not five, echo it's current value
      echo "Count: " . $count . "<br>";
   $count++;// increment the $count by +1, and go back to the while(condition) area
}
echo "Total Count: " . $count;//echo $counts final value.

It is 11 because on pass 10, the condition is still true, so it increments and goes to check if the condition is still valid (it's not now)


 

FOR loop for

for(expression 1; expression 2; expression 3)

[evaluate expression one] [run expression 2]

{

  do code between braces

}

[run expression 3]

 

--commonly thought of as:

for(start value; condition check; action) //at least to me

for($count = 0; $count <= 10; $count++)

The first expression initializes $count at 0.

The second checks that $count is less than or equal to (not greater) than 10.

The third is what to do after the loop has ran through it's { code } once

{
    if($count == 5){//if $count == 5, echo FIVE
      echo "FIVE" . "<br>";
   }
   else //else echo the value
      echo "Count: " . $count . "<br>";
   $count++;//increment count
}//LOOP HAS FINISHED, RUN THIRD EXPRESSION $count ++

As you can see, you have already incremented it in your code, then before the loop is run again, the third expression in the for loop runs, incrementing the $count variable

echo "Total Count: " . $count;

?>

 

Hopefully that isn't too confusing?

It's not confusing. Thanks for your explanation. But I did not get my answer, that is: why 1,3,5,7 and 9 are not printed out?

I put the code slightly differently:

<?php
for($count = 0; $count <= 10; $count++){

if($count == 12){
	echo "Four: " . $count . "<br>";		
}
else echo $count . "<br>";
$count++;

}

The output is:

0

2

4

6

8

10

Total Count: 12

Look the same thing happens! Again! Odd integers are out. Why odd integers are out?

 

Link to comment
Share on other sites

You took it slightly different. Try this as your for loop.

 

<?php
for($count = 0; $count <= 9; $count++){
    if($count == 5){
      echo "FIVE" . "<br>";
   }
   else 
      echo "Count: " . $count . "<br>";

}
echo "Total Count: " . $count;
?>

 

Assuming you wan't to stop the total count at 10. I changed the top bit to 9 so it can stop at 10. You already have $count++ in the for() function. So why put it lower down? that is why it was double counting.

Link to comment
Share on other sites

look coments

<?php
for($count = 0; $count <= 10; $count++ // first increment variable $count
   ){
   
   if($count == 12){
      echo "Four: " . $count . "<br>";      
   }
   else echo $count . "<br>";
   $count++; // 2nd increment variable $count
   
}

just remove ones of this

<?php
for($count = 0; $count <= 10;    ){
   
   if($count == 12){
      echo "Four: " . $count . "<br>";      
   }
   else echo $count . "<br>";
   $count++;
   
}

or

<?php
for($count = 0; $count <= 10; $count++){
   
   if($count == 12){
      echo "Four: " . $count . "<br>";      
   }
   else echo $count . "<br>";
   
   
}

Link to comment
Share on other sites

You took it slightly different. Try this as your for loop.

 

<?php
for($count = 0; $count <= 9; $count++){
    if($count == 5){
      echo "FIVE" . "<br>";
   }
   else 
      echo "Count: " . $count . "<br>";

}
echo "Total Count: " . $count;
?>

 

Assuming you wan't to stop the total count at 10. I changed the top bit to 9 so it can stop at 10. You already have $count++ in the for() function. So why put it lower down? that is why it was double counting.

 

Thanks for your interest. What I really want to know is when I put $count++ lower down it throws odd integers out. Why?

Link to comment
Share on other sites

You took it slightly different. Try this as your for loop.

 

<?php
for($count = 0; $count <= 9; $count++){
    if($count == 5){
      echo "FIVE" . "<br>";
   }
   else 
      echo "Count: " . $count . "<br>";

}
echo "Total Count: " . $count;
?>

 

Assuming you wan't to stop the total count at 10. I changed the top bit to 9 so it can stop at 10. You already have $count++ in the for() function. So why put it lower down? that is why it was double counting.

 

Thanks for your interest. What I really want to know is when I put $count++ lower down it throws odd integers out. Why?

 

Because it is essentially adding 2 every time to count, as you can see in the for loop count++ is already there, that works everytime, so when you add count++; inside the for loop it runs twice, thus giving you the odd integers.

 

Make sense? It was like you were adding 2 each time the loop ran.

Link to comment
Share on other sites

You took it slightly different. Try this as your for loop.

 

<?php
for($count = 0; $count <= 9; $count++){
    if($count == 5){
      echo "FIVE" . "<br>";
   }
   else 
      echo "Count: " . $count . "<br>";

}
echo "Total Count: " . $count;
?>

 

Assuming you wan't to stop the total count at 10. I changed the top bit to 9 so it can stop at 10. You already have $count++ in the for() function. So why put it lower down? that is why it was double counting.

 

Thanks for your interest. What I really want to know is when I put $count++ lower down it throws odd integers out. Why?

 

Because it is essentially adding 2 every time to count, as you can see in the for loop count++ is already there, that works everytime, so when you add count++; inside the for loop it runs twice, thus giving you the odd integers.

 

Make sense? It was like you were adding 2 each time the loop ran.

AT LAST! I GOT MY ANSWER. IT COUNTS TWICE AND STOPS WHEN IT REACHES 10. THAT'S WHY 1,3,5,7 AND 9 ARE NOT PRINTED OUT. WELL, SO IT CAN BE USED TO THROW ODD NUMBERS OUT FROM A BUNCH OF NUMBERS. THANKS A LOT.

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.