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.
Chat with our AI personalities
we can use switch statement in multiple time but in if statement we can not use multiple time
In a declarative statement, you initialize the object. But in an imperative statement, you use a preexisting statement and use it.
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.
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 {}.
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}