How NOT to use && and ||

FrostyCat

Member
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:
Code:
n == 1 || 2 || 3 || 4
RIGHT:
Code:
(n == 1) || (n == 2) || (n == 3) || (n == 4)
WRONG:
Code:
n != 1 || 2 || 3 || 4
RIGHT:
Code:
(n != 1) && (n != 2) && (n != 3) && (n != 4)
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:
Code:
instance_exists(obj_a && obj_b)
RIGHT:
Code:
instance_exists(obj_a) && instance_exists(obj_b)
WRONG:
Code:
place_meeting(x, y+1, obj_a || obj_b)
RIGHT:
Code:
place_meeting(x, y+1, obj_a) || place_meeting(x, y+1, obj_b)
Code:
place_meeting(x, y+1, obj_parent)
Note: obj_parent is the parent of obj_a and obj_b.

WRONG:
Code:
with (obj_a && obj_b) {
  //...
}
Code:
with (obj_a || obj_b) {
  //...
}
RIGHT:
Code:
with (obj_a) {
  //...
}
with (obj_b) {
  //...
}
Code:
with (obj_parent) {
  //...
}
Note: obj_parent is the parent of obj_a and obj_b.

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
}
RIGHT:
Code:
for (i = 0; i < 5; i += 1) {
  a = 5;
  b = 7;
  c = 9;
}
Misguided Belief 4: "|| allows you to choose between values"

WRONG:
Code:
wheel_spin = "Try again" || "Small prize" || "Jackpot";
RIGHT:
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)];
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:
Code:
switch (n) {
  case 3 && 5:
    //...
  break;
}
Code:
switch (n) {
  case 3 || 5:
    //...
  break;
}
RIGHT:
Code:
switch (n) {
  case 3: case 5:
    //...
  break;
}
Misguided Belief 6: "&& and || allows you to glue keys together"

WRONG:
Code:
keyboard_check(vk_up && vk_down)
RIGHT:
Code:
keyboard_check(vk_up) && keyboard_check(vk_down)
WRONG:
Code:
keyboard_check_pressed(vk_left || ord("A"))
RIGHT:
Code:
keyboard_check_pressed(vk_left) || keyboard_check_pressed(ord("A"))
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:
  • 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:
C

Conor Egan

Guest
does l l mean or? if not, what does that mean? Also, how do u type it? Here I just used lowercase Ls
 
C

Conor Egan

Guest
oh, I found it. it looks like two lines on my keyboard but really is one
|
||||||||||||||||||||||||||
thanks
 
Top