This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Control Flow: Switch Forum

View Course » View Exercise

203 points
Ec9154ac23a4961e39ac1e42e76b78c5?s=140&d=retro
Submitted by
stream8media
almost 4 years ago

Somebody please help with this problem.

Somebody please help with this problem.
<!DOCTYPE html>





<?php
$fruit = "Apple";

switch ($fruit) {
    __ 'Apple':
        echo "Yummy.";
        __;
}

?>
</body>

There is something i am not understanding here no matter how i try to fix..if possible, do not just print answer but explain as well as im new..thank you so much.


1 vote

permalink

<?php
$fruit = "Apple"; //let assume $fruit's value is 'Apple'!
switch ($fruit) {
case 'Apple':
echo "Yummy."; break;
default :
echo "Smoothie";
}

?>

 1. Switches work like if statements, if a condition is true, it executes a block of code.
 2. A switch statement accepts only one argument and that is the value that you make the expressions on.
3. keyword case is followed by the value being compared (or an expression).
 4. '**break**' block the current case and print statement if it is true.
 5.  If no breaks are defined, when a case is matched, all the cases below get executed. The following code would output: **Apple.Smoothie**.
  1. 'default' print the statement which is not true. or we can command to print if case does not match. hope this will help you to understand.. Glad to help

249 points
528f02fdabf821a46c001545_947168395
Submitted by
Sonu Pradhan
almost 4 years ago


0 votes

permalink

The first blank is case and the second break. I bet you got that part right but you still need a default case.

default:
echo "Whatever";

100 points
B434fb1a4ebc86c8355d8aa713b60c1e?s=140&d=retro
Submitted by
habib Bema
almost 4 years ago