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

Control Flow: Switch Forum

View Course » View Exercise

555 points
51315408c3f3d809eb001060_425868639
Submitted by
Doesn't Matter
over 4 years ago

I dont understand

I dont understand this at all. I dont know why but everytime I get into more complicated lessons in programming I can't remember what I just learned. SOMEONE HELP ME PLEASE!


3 votes

permalink

ok,

I will try to explain :

switch is used when you have a long list of If/else to do

let's say you have a lottery with 2 winners (number 1 and number 2), you can have an easy if/else code :
if ($x==1) { prize ==1 };
elseif ($x==2) { prize ==2};
else echo "you lose"

but imagine you have a lottery with 15 winners and different prizes if the winner is a male or a female, and moreover, the prizes are different if if the winner is under 18 or over 18,

you can try to do it with if/else but it will be long and complicated, switch is there easier to use
comments : $x = number of the winner
$age = age of the winner
$sex = male or female
switch($x) {
case 1 :
switch($age)
case $age>18:
switch($sex):
case male:
echo "you win a date with Monicca Belluci"
case female:
echo "you win a date with Ryan Reynolds"
case $age<18:
switch($sex):
case male:
case female:
echo "you win a date PS3"

comments : (for $age<18 both sex won a PS3 )

let's try to do the same code with an if/else code :

if ($x==1 and $age>18 and $sex=="male") { echo "you win a date with monica bellucci"}
else if ($x==1 and $age>18 and $sex=="female" {echo "you win a date with ryan reynolds"}
else if ($x==1 and $age<18 and $sex=="male" of $sex=="female" { echo "you win a PS3}

if you have time, try to write the 15 winners, and you then you could say if you prefer switch or if/else (there are doing exactly the same thing, it's just (for me and a lot of programmers) easier to use switch when you have a lot of options and if/else when you have not so much options

749 points
Picture
Submitted by
Leleu
over 4 years ago

3 Comments

51315408c3f3d809eb001060_425868639 Doesn't Matter over 4 years ago

Thanks a lot. This really helped

51315408c3f3d809eb001060_425868639 Doesn't Matter over 4 years ago

You wouldn't mind explaing how Switch would be used in a program like an app or a game would you. It would signifigantly increase my understanding and i would be very greatful.

51ec5b3a631fe9b5ef009d6f_54962401 Arnold L. over 4 years ago

Very helpful, thank you Leleu!


1 vote

permalink

It's not that I mind, but I am still learning code and haven't had the chance to use it in an APP or a Game, so it's hard to me to imagine real use, perhaps this website could be useful to you: http://www.macs.hw.ac.uk/~pjbk/pathways/cpp1/node105.html

749 points
Picture
Submitted by
Leleu
over 4 years ago


0 votes

permalink

Hallo Doesn't Matter :)

explain your problem? your score is far from bad! as for me, the more difficult is to go from a language to another, is this your problem?

Best regards,

749 points
Picture
Submitted by
Leleu
over 4 years ago

1 Comment

51315408c3f3d809eb001060_425868639 Doesn't Matter over 4 years ago

I just don't understand the whole default/switch thing. I don't no what they do and why they're important. I never really fully understand something until I know what it's purpose is


0 votes

permalink

Would probably be used to choose a ID? A Colour? Or even a character type? just guessing off the top of my head with that. Example :

$CharacterType = "Warlock";

switch($CharacterType):
case "Warlock":
echo "You have chosen Warlock and the warlock story goes here?";
break;
case "Mage":
echo "You have chosen Mage and the mage story goes here?";
break;
default "No Class Selected!";
endswitch;

Doubt it works quite like that but might help you understand it better.

Happy Coding!

357 points
519ef6fc9d39ef6df70031e3_305238852
Submitted by
Ricardo
over 4 years ago


0 votes

permalink

Hey, I was struggling with this too. But then I found this very useful example on "the internet's favourite TV station" YouTube

https://www.youtube.com/watch?v=Yo1YjPWpy2M

Have a look

266 points
5304881d548c35d843001786_144205972
Submitted by
Philip Harper
over 3 years ago