Register|Login

Have a question? Click here to ask the community.

Quiz 15 – While Loop

| In: PHP Quizzes | 20,115 views

*You are currently not logged in and your quiz score will not be recorded. Please login or register if you would like to submit your quiz score.

Directions: Answer all questions. Any questions skipped will be scored as wrong answers. Good Luck! :)

1. A while loop will continue to execute a segment of code until its condition evaulates to false?

True
False

2. A while loop is prefered when having to carry out a repetitive operation?

True
False

3. What will be displayed in a browser when the following PHP code is executed:

<?php
   $counter = 1;
   while ($counter <= 5) {
       $counter++;
   }
   echo "Counter is: $counter";
?>

Counter is: 1
Counter is: 5
Counter is: 4
Counter is: 6

4. What will be displayed in a browser when the following PHP code is executed:

<?php
   $counter = 6;
   while ($counter != 3) {
       echo "Hello World | ";
       $counter--;
   }
?>

Hello World | Hello World |
Hello World | Hello World | Hello World |
Hello World | Hello World | Hello World | Hello World |
None of the above

5. What will be displayed in a browser when the following PHP code is executed:

<?php
   $counter = 3;
   while ($counter != 3) {
       echo "Hello World | ";
       $counter--;
   }
?>

Hello World | Hello World
Hello World | Hello World | Hello World
Hello World | Hello World | Hello World | Hello World
None of the above



2 Responses to Quiz 15 – While Loop

Avatar

ers3

April 28th, 2015 at 11:55 am

For question 3, how do you derive the answer. My question is the code saying it will incrementally increase by one should the answer be 2.

Avatar

Robert Smith

April 28th, 2015 at 1:07 pm

No, the answer will be 6, since the while loop will continue to loop until the conditional statement evaluates to false. So the counter is incremented 5 times while the while loop is being executed. After the while loop is executed, the counter will be equal to 6.

Comment Form

You must be logged in to post a comment.

Users Online

Users Online: 2 Users

Most Users Ever Online: 91

Quiz Scores

Advertisments