# 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!

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

Doesn't Matter almost 5 years ago

Thanks a lot. This really helped

Doesn't Matter almost 5 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.

Arnold L. almost 5 years ago

1 vote

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

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,

##### 1 Comment

Doesn't Matter almost 5 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

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!