[Solved] What does ";" do and Difference between = and ==?

Discussion in 'Programming' started by TriiiKill, Feb 21, 2017.

  1. TriiiKill

    TriiiKill Guest

    This is a double question, but if you know one, you might know the other so:
    1. What does the semicolon do in functions?
    I see it all the time, and I use it only when copy-pasting someones code, but I don't see the difference between it being there and not

    2. What is the difference between = and ==?
    for example, the difference between:
    if a = b
    (fill the blank)
    and
    if a == b
    (fill the blank)

    I looked it up on the manual, still not finding an answer.
     
  2. Pixelated_Pope

    Pixelated_Pope Member

    Joined:
    Jun 20, 2016
    Posts:
    785
    This is all about "proper" coding. Yeah, you can code without semi-colons and using single = for conditional checks in GM because GM is super nice to beginners. But the second you move to a different programming language, or even just using the YYC those rules get MUCH more strict.

    My advice is to end every line of code with a semi-colon (except where you aren't supposed to use one, like after an if or definition of a loop) and use == for comparisons and = for assignments only. It's just a good habit to form as you grow as a programmer.
     
  3. Bros Before Giraffes

    Bros Before Giraffes Member

    Joined:
    Jun 24, 2016
    Posts:
    30
    the semicolon is used to identify the end of a line. Some languages require it and some don't. GML doesn't necessarily require it but it's probably good practice to use it.
    = should only be used if you want to set a variable equal to some other value. I think GML still accepts it in an if statement but it's bad practice to use it there.
    == is only used when you want to compare two separate values, like in your examples.
    I hope that helped
     
  4. RangerX

    RangerX Member

    Joined:
    Jun 20, 2016
    Posts:
    2,595
    1 - A common way to MANY programming languages to end a line of code. With GML its also to end a line but if you forget it, GMS doesn't crash, its pretty flexible with synthaxe.

    2- = is "equal". == is a comparison.

    So basically, if you say "A=5;" you just updated the variable called "A" with the value of "5".
    If you say "if ( A==5)" you just asked "Does A equals the value of 5?"
     
  5. TriiiKill

    TriiiKill Guest

    Ah, okay. I find it odd that other programs couldn't recognize the difference. Like if it's an "if" statement, that's a completely different function than setting a variable.
    Thanks for the information, glad to hear I wasn't making a large mistake on my programming. :)
     
  6. Mick

    Mick Member

    Joined:
    Jun 30, 2016
    Posts:
    725
    In those languages you can assign variables in if clauses like if((var1=function()) == 5) to assign the result of a function call and check the value "all at once" as an example.

    Just to be "correct" the semicolon is a statement terminator (used at the end of a statement). There can be multiple statements separated with semicolon on the same line and one statement can use multiple lines.
     
    Last edited: Feb 22, 2017
  7. J. A. Whye

    J. A. Whye Member

    Joined:
    Nov 8, 2016
    Posts:
    1
    I'll gladly be the voice of dissent on the semicolon issue. The only time you should use a semicolon is when it is required. Otherwise, you're just giving yourself extra meaningless stuff to type.

    A computer language or compiler that requires a semicolon to terminate every statement is poorly designed. As a newbie to GMS I'm really glad to see they didn't pull that bozo move. :cool:

    Jay
     
    Last edited: Jun 5, 2017
  8. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,549
    Except the cases where it isn't optional isn't genuinely clear-cut, which is why "semicolon-free" propaganda for GML like this is a problem.

    At its heart, GML requires the use of semicolons, but the parser is supported by a heuristic that guesses at line boundaries when they are absent. As you should know by now, var and the deprecated globalvar need semicolons. But what you don't know is that there have been isolated incidents like this one where a missed semicolon outside var contexts caused unexpected behaviour. Nobody saw them coming until runtime, the current exceptions aren't well-defined, and I won't be surprised if others are uncovered in the future.

    If you wan to talk about poorly designed languages, a language that isn't consistently parsed is more poorly designed than a language that needs semicolons. This is why I have been arguing for more rigorous parsing in GML for years --- either remove the multiple-statements-in-a-line use case that gives semicolons their purpose, or require semicolons and stop giving false hope that sometimes go unanswered.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice