-
Notifications
You must be signed in to change notification settings - Fork 1
Switch
{ case value0: statements; case value1: statements; case value3: statements; default: statements; }
switch$ (<string expression>) { case "string value 0": statements; case "string value 1": statements; ... case "string value N": statements; default: statements; } First, we need to create a new script:
exec("./switch.cs");
// Print a message to a console based on // the amount of ammo a weapon has // %ammoCount - Ammo count (obviously) function checkAmmoCount(%ammoCount) { // If the ammo is at 0, we are out of ammo // If the ammo is at 1, we are at the end of the clip // If the ammo is at 100, we have a full clip // If the ammo is anything else, we do not care if(%ammoCount == 0) echo("Out of ammo, time to reload"); else if(%ammoCount == 1) echo("Almost out of ammo, warn user"); else if(%ammoCount == 100) echo("Full ammo count"); else echo("Doing nothing"); }
checkAmmoCount(0); checkAmmoCount(1); checkAmmoCount(100); checkAmmoCount(44);
Out of ammo, time to reload
// Print a message to a console based on // the amount of ammo a weapon has // %ammoCount - Ammo count (obviously) function checkAmmoCount(%ammoCount) { // If the ammo is at 0, we are out of ammo // If the ammo is at 1, we are at the end of the clip // If the ammo is at 100, we have a full clip // If the ammo is anything else, we do not care switch(%ammoCount) { case 0: echo("Out of ammo, time to reload"); case 1: echo("Almost out of ammo, warn user"); case 100: echo("Full ammo count"); default: echo("Doing nothing"); } }
checkAmmoCount(0); checkAmmoCount(1); checkAmmoCount(100); checkAmmoCount(44);
Out of ammo, time to reload
// Check to see if a person's name is // a known user // %userName - String containing person's name function matchNames(%userName) { if(!strcmp(%userName, "Heather")) echo("User Found: " @ %userName); else if(%userName $= "Mich") echo("User Found: " @ %userName); else if(%userName $= "Nikki") echo("User Found: " @ %userName); else echo("User " @ %userName @ " not found"); }
matchNames("Heather"); matchNames("Mich"); matchNames("Nikki"); matchNames("Brad");
User Found: Heather User Found: Mich User Found: Nikki User Brad not found
// Check to see if a person's name is // a known user // %userName - String containing person's name function matchNames(%userName) { switch$(%userName) { case "Heather": echo("User Found: " @ %userName); case "Mich": echo("User Found: " @ %userName); case "Nikki": echo("User Found: " @ %userName); default: echo("User: " @ %userName @ " not found"); } }
This guide covered the basics of the switch and switch$ statement structures. When you need to perform one or two logical checks, you will use the basic control statements such as if(...), if else(...), and else. When you need a complex control statement handling multiple outcomes based on a value, try using a switch statement instead
|