3. Your First While Loop


#1



https://www.codecademy.com/courses/web-beginner-en-5YvPF/0/3?curriculum_id=5124ef4c78d510dd89003eb8#


I'm getting an error: "The program took too long to finish. Check your code for infinite loops and try again."
I even tried putting the example code. Still getting the same error.


<?php
	//Add while loop below
$loopCount = 0;
while ($loopCount<4){
   echo "<p>Iteration number: {$loopCount}</p>";
   $loopCount ++;
}
    
    ?>


#2

Hi this line

while ($loopCount<4)

put a space around < like that

while ($loopCount < 4)

#3

@wizmarco @leonhard.wettengmx.n
when you use the equal sign in the condition it gives an extra one.. for example

$headCount=0;
$flipCount=0;
while ($headCount <=3) {
        }

it results to four continuous heads in a row instead of three.. why?


#4

HI why it print four it's because it begin to count from 0 and not 1 like that

#here you have the while loop
$headCount = 0;

while ($headCount <=3) {
    echo "Hello";
    $headCount+++
}
#you have that condition
while ($headCount <=3)

#when you run it 
1. while (0 <= 3) #true
2. while (1 <= 3) #true
3. while (2 <= 3) #true
4. while (3 <= 3) #true
5. while (4 <= 3) #this one won't work because it's false and will stop the loop

#it will Output
1.hello
2.hello
3.hello
4.hello

and when you don't have just the < whithout equale

while ($headCount <3)

#when you run it 
1. while (0 < 3) #true
2. while (1 < 3) #true
3. while (2 < 3) #true
4. while (3 < 3) # this one won't work because it false and will stop the loop

#it will Output
1.hello
2.hello
3.hello

#5

Thanks.. i now understand