"while statement


#1

hallo!!!
m stuck right from the first example under the 'while statement'.i dont understand every bit of it.i proceeded to the first exercise but no improvement. i need someone to explain fully in details how to go about the 'while statement....i mean every syntax of it.tenx


#2

Its:

while(condition){
instructions }

It's not like 'for' loop where you put the incrementation into the '()' so if you want to increment it, do it inside of the brackets. Like this:

var count = 0; // Set the variable to a starting value of 0.

while(count < 5){ // Set only the condition
echo "Still looping";
count++; } // Increment here

Is it enough or dou you need specific explanations about the 1/7 exercice?


#3

please continue.i didnt even get the exercises too.and i dnt want to skip them.tenx


#4

Ok my variables are in french but here's my code. I added some comments :

$nombreDeFaces = 0; // At the beginning of the game, you have 0 heads
$nombreDeLances = 0;
while ($nombreDeFaces < 3) { // Your objective is to get 3 heads in a row. So you want your loop to keep looping until it's the  case.
	$lance = rand(0,1); // Each turn/loop, $lance will be set randomly to 0 OR 1 (head = 1 and tail = 0)
	$nombreDeLances ++;
	if ($lance){ // If $lance == 1 (So $lance = TRUE = head)
		$nombreDeFaces ++; // You add +1 to your variable $nombreDeFace

		echo "<div class=\"piece\">F</div>"; // Display the result
	}
	else {
		$nombreDeFaces = 0; // If $lance was randomly set to 0, you start from the beginning 
		echo "<div class=\"piece\">P</div>"; // Display the result
	}
}
echo "<p>Il nous a fallu {$nombreDeLances} lancés !</p>"; 
?>

Is it ok for you?


#5

@shanowar, I played around with your code and came up with this (in lieu of missing HTML/CSS)...

<!doctype html>
<html lang="fr">
    <head>
        <meta charset="UTF-8">
        <title>Faces and Lances</title>
        <style>
        .piece {
            display: inline;
        }
        .piece:nth-last-child(4):before {
            content: " » ";
            color: green;
        }
        .piece:nth-last-child(-n+4){
            color: red;
        }
        </style>
    </head>
    <body>
		
<?php
    $nombreDeFaces = 0;
    $nombreDeLances = 0;
    while ($nombreDeFaces < 3) {
        $lance = rand(0,1);
        $nombreDeLances ++;
        if ($lance){
            $nombreDeFaces ++;
            echo "<div class=\"piece\">F</div>";
        } else {
            $nombreDeFaces = 0;
            echo "<div class=\"piece\">P</div>";
        }
    }
    echo "<p>Il nous a fallu {$nombreDeLances} lancés !</p>"; 
?>

    </body>
</html>

Please forgive any language discrepancies that crop up. My French isn't what it was 20 years ago.


#6

thanks so much.wl try to make use of it now and inform u if d dere z any changes