FrostyCat
Redemption Seeker
How NOT to use && and ||
GM Version: N/A
Target Platform: All
Download: N/A
Links: N/A
Summary: A summary of common novice abuses of && and || with corresponding alternatives.
Introduction
Novices learning GML often fall into the trap of translating word-for-word into GML symbols, and a common manifestation of this problem is improper use of && and ||. This tutorial lists several common misuses and the corresponding correct code.
Misguided belief #1: "|| allows you to create values equivalent to multiple values at once"
WRONG:
RIGHT:
WRONG:
RIGHT:
Note: The && is not a typo --- using || here would result in an expression that is always true.
Misguided Belief 2: "&& and || allows you to glue multiple objects together"
WRONG:
RIGHT:
WRONG:
RIGHT:
Note: obj_parent is the parent of obj_a and obj_b.
WRONG:
RIGHT:
Note: obj_parent is the parent of obj_a and obj_b.
Misguided Belief 3: "&& allows you to tack multiple statements together"
WRONG:
RIGHT:
Misguided Belief 4: "|| allows you to choose between values"
WRONG:
RIGHT:
Note: For legacy versions of GM and Studio 1.4.1757 or below, choose() can only be used when there are 16 or fewer possible values.
Misguided Belief 5: "&& and || allows you to combine cases in a switch statement" (Also see: How NOT to use switch)
WRONG:
RIGHT:
Misguided Belief 6: "&& and || allows you to glue keys together"
WRONG:
RIGHT:
WRONG:
RIGHT:
How to Avoid Misusing && and ||
If you don't want to use && and || where they don't belong and not know what hit you, follow these 2 guidelines:
Example 1: Is shooting = keyboard_check(vk_space) || keyboard_check(vk_shift); correct?
The left side of || is keyboard_check(vk_space), which is Boolean. The right side of || is keyboard_check(vk_shift), which is also Boolean. This piece of code is fine.
Example 2: Is points += 50 || 100; correct?
The left side of || is 50, which is not Boolean. The right side of || is 100, which is also not Boolean. This piece of code is just a bad literal translation of "increase points by 50 or 100" and should actually be points += choose(50, 100);.
GM Version: N/A
Target Platform: All
Download: N/A
Links: N/A
Summary: A summary of common novice abuses of && and || with corresponding alternatives.
Introduction
Novices learning GML often fall into the trap of translating word-for-word into GML symbols, and a common manifestation of this problem is improper use of && and ||. This tutorial lists several common misuses and the corresponding correct code.
Misguided belief #1: "|| allows you to create values equivalent to multiple values at once"
WRONG:
Code:
n == 1 || 2 || 3 || 4
Code:
(n == 1) || (n == 2) || (n == 3) || (n == 4)
Code:
n != 1 || 2 || 3 || 4
Code:
(n != 1) && (n != 2) && (n != 3) && (n != 4)
Misguided Belief 2: "&& and || allows you to glue multiple objects together"
WRONG:
Code:
instance_exists(obj_a && obj_b)
Code:
instance_exists(obj_a) && instance_exists(obj_b)
Code:
place_meeting(x, y+1, obj_a || obj_b)
Code:
place_meeting(x, y+1, obj_a) || place_meeting(x, y+1, obj_b)
Code:
place_meeting(x, y+1, obj_parent)
WRONG:
Code:
with (obj_a && obj_b) {
//...
}
Code:
with (obj_a || obj_b) {
//...
}
Code:
with (obj_a) {
//...
}
with (obj_b) {
//...
}
Code:
with (obj_parent) {
//...
}
Misguided Belief 3: "&& allows you to tack multiple statements together"
WRONG:
Code:
for (i = 0; i < 5; i += 1) {
a = 5 && b = 7 && c = 9
}
Code:
for (i = 0; i < 5; i += 1) {
a = 5;
b = 7;
c = 9;
}
WRONG:
Code:
wheel_spin = "Try again" || "Small prize" || "Jackpot";
Code:
wheel_spin = choose("Try again", "Small prize", "Jackpot");
Code:
wheel_spin_values[0] = "Try again";
wheel_spin_values[1] = "Small prize";
wheel_spin_values[2] = "Jackpot";
wheel_spin = wheel_spin_values[irandom(2)];
Misguided Belief 5: "&& and || allows you to combine cases in a switch statement" (Also see: How NOT to use switch)
WRONG:
Code:
switch (n) {
case 3 && 5:
//...
break;
}
Code:
switch (n) {
case 3 || 5:
//...
break;
}
Code:
switch (n) {
case 3: case 5:
//...
break;
}
WRONG:
Code:
keyboard_check(vk_up && vk_down)
Code:
keyboard_check(vk_up) && keyboard_check(vk_down)
Code:
keyboard_check_pressed(vk_left || ord("A"))
Code:
keyboard_check_pressed(vk_left) || keyboard_check_pressed(ord("A"))
If you don't want to use && and || where they don't belong and not know what hit you, follow these 2 guidelines:
- DO NOT translate English sentences word-for-word into GML symbols.
- If either side of an && or || operator is not meant to be treated as a Boolean value (true/false), the code is wrong.
Example 1: Is shooting = keyboard_check(vk_space) || keyboard_check(vk_shift); correct?
The left side of || is keyboard_check(vk_space), which is Boolean. The right side of || is keyboard_check(vk_shift), which is also Boolean. This piece of code is fine.
Example 2: Is points += 50 || 100; correct?
The left side of || is 50, which is not Boolean. The right side of || is 100, which is also not Boolean. This piece of code is just a bad literal translation of "increase points by 50 or 100" and should actually be points += choose(50, 100);.
Last edited: