answersLogoWhite

0


Best Answer

Switch Statements are used to generate different outputs of code based on the value of an expression. Switch Statements work as follows:
{
randomNumber = floor(random(3))+1;
switch(randomNumber) {
case 1: { } break;
case 2: { } break;
case 3: { } break;
default: { } break;
}
}

This may seem confusing if you are new to GML, so I will give an in-depth explanation. The first line sets the variable randomNumber to a random number between 0 and 2, and adds it by 1 to make it a random number from 1-3. So far the only thing that has gone on in the code is to set a variable to either 1, 2, or 3. This is where the switch statement comes in.
switch(randomNumber) {
case 1: { } break;
case 2: { } break;
case 3: { } break;
default: { } break;
}

this is the actual switch statement. You may be wondering what the case statements are for. case statements are always written inside switch statements and do nothing anywhere else. case statements activate when the expression in the switch statement is the same as the value that they are assigned to. Take a look at this switch statement:
{
rand = floor(random(3));
switch(rand) {
case 0: {
show_message("The Random Value Was 0");
} break;
case 1: {
show_message("The Random Value Was 1");
} break;
case 2: {
show_message("The Random Value Was 2");
} break;
}
}

When the values assigned to the case statements are equal to the expression in the switch statement, the case statement will run the code contained in it's brackets. break statements order the switch statement to abort. The reason that you need break statements inside a switch statement is because it keeps the other cases from activating as well. (When one case statement activates, the others do as well.)
A final briefing on switch statements is that they are not limited to variables. Take a look at this switch statement.
{
switch(obj_block.x > x) {
case true: {
show_message("The Block Is Ahead Of You.");
} break;
case false: {
show_message("You Are Ahead Of The Block.");
} break;
}
}
This switch statement returns a true or false value, and the case statements operate accordingly.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you use a switch statement in GML?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Basic Math

What is the difference between 'switch' statement and 'if' statement?

we can use switch statement in multiple time but in if statement we can not use multiple time


What is the difference between a declarative statement and an imperative statement?

In a declarative statement, you initialize the object. But in an imperative statement, you use a preexisting statement and use it.


How do you set a timer in game maker?

You can use the D&D icon that looks like a clock, or you can use this command in GML: alarm[argument0] = argument1; argument0 = alarm you want to set. argument1 = how long it will take before the alarm will go off.


What is the syntax of a conditional statement in JavaScript?

In JavaScript we have the following conditional statements:if statement - you would use this statement to execute some code only if a specified condition is trueif...else statement - you would use this statement to execute some code if the condition is true and another code if the condition is falseif...else if....else statement - you would use this statement to select one of many blocks of code to be executedswitch statement - you would use this statement to select one of many blocks of code to be executedFor example: If StatementUse the if statement to execute some code only if a specified condition is true. Syntaxif (condition) {code to be executed if condition is true}If...else StatementUse the if....else statement to execute some code if a condition is true and another code if the condition is not true. Syntaxif (condition) {code to be executed if condition is true}If...else if...else StatementUse the if....else if...else statement to select one of several blocks of code to be executed. Syntaxif (condition1) {code to be executed if condition1 is true}else if (condition2){code to be executed if condition2 is true}else{code to be executed if condition1 and condition2 are not true}else{code to be executed if condition is not true}


What is a compound statement in programming?

A compound statement is a code block. We typically use compound statements as the body ofanother statement, such as a while statement:while (i >= 0){a[i] = x;++x;--i;}Note that all compound statements are surrounded by braces {}.

Related questions

What is the difference between 'switch' statement and 'if' statement?

we can use switch statement in multiple time but in if statement we can not use multiple time


Can a continue statement be used in a switch statement?

If you have a loop in your switch statement or around your switch statement, you can use the continue statement in that. You cannot use a continue statement outside of a loop (do, for, or while).


What programming languages use a C switch statement?

There are two programming languages which use a C switch statement. The two languages are C and C++, hence the name C switch statement. There may be more, but those are the most obvious ones


What is the use of switch statement in java?

In java, a switch statement is used to simplify a long list of 'if' statements. A switch statement takes the form of:switch (variableName){case condition1; command1;case condition2; command2;...}


How do you use gml?

Go to yoyogames.com, they have a bunch of tutorials. You could also go to, gmc.yoyogames.com. It's the forum of gml game makers.


In command prompt what does the if switch do?

There is not switch called "if". We generally use "if" statement in batch programming in DOS.


How do you choose multiple if statement and switch statement?

If you must evaluate two or more expressions separately, use multiple if statements. If you only need to test all the possible evaluations of a single expression, use a switch.


What is the deffernce of the switch statement and the if statement in c plus plus?

If statement is single selection statement,whereas the switch statement is multiple selective.


How if else if statement is differ from switch in general in java?

An else statement is comparing to items, while a switch statement is used to compare multiple items. So if you need multiple results from one statement use a switch but for booleans or very specific functions use a if else statement. Else If ex. if(a=6){System.out.println("It is six");} else(){System.out.println("It is not six");} Switch ex. switch(a){ case 1: System.out.println("it is one"); break; case 2: System.out.println("it is two"); break; default: System.out.println("it is not one or two"); break; } Basically a switch is a good way to check against a lot of possibilities.


Why you use for loop inside switch statement?

Because you have to repeat something. (Or you can use while-loop, too.)


What does default mean in switch statement?

Default clause in switch statement used to indicate that the desired option is not available with the switch case statement. it is similar to else statement of if statement which is used when the condition does not satisfy.


Can you use conditional operator in cout statement?

Yes, but 'cout' is not a statement! Examples for statements: null-statement, block, expression, if-else, while, do-while, for, continue, switch, break, return.