1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

How NOT to use && and ||

Discussion in 'Tutorials' started by FrostyCat, Nov 16, 2016.

  1. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,870
    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: Dec 22, 2016
  2. Conor Egan

    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
     
  3. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,393
  4. SSJCoder

    SSJCoder Member

    Joined:
    Aug 12, 2017
    Posts:
    127
    Usually it's above the enter key, has a |\ on it, hold shift and press it
     
  5. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    5,043
    I think it may depend on keyboard layout (qwert/azerty etc) as on my keyboard it's next to my left shift key.
     
    SSJCoder likes this.
  6. Conor Egan

    Conor Egan Guest

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

    sitebender Member

    Joined:
    Sep 13, 2016
    Posts:
    854
    Wow those wrongs. I've never seen them like that.
     
    Sabnock likes this.
  8. Sabnock

    Sabnock Member

    Joined:
    Jul 21, 2016
    Posts:
    364
    Agreed
     
  9. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,870
    I have, and there were so many of them that I was compelled to write the article. This Google search shows how many times I cited this article in the new forums alone. There were even more in the old forums.
     
    Japster likes this.
  10. Null-Z

    Null-Z Member

    Joined:
    Dec 31, 2016
    Posts:
    92
    Aaaand Bookmarked!

    I was wondering why using these wasn't yielding the indented result.
     
  11. Froll

    Froll Member

    Joined:
    Apr 26, 2018
    Posts:
    8
    That's usefull.
     

Share This Page