• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

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

T

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

RangerX

Member
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?"
 
T

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. :)
 

Mick

Member
Like if it's an "if" statement, that's a completely different function than setting a variable.
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:
J

J. A. Whye

Guest
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 by a moderator:

FrostyCat

Redemption Seeker
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
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.
 
Top