Home   Help Search Login Register  

Author Topic: Multiple argument passing and loops  (Read 1149 times)

0 Members and 1 Guest are viewing this topic.

Offline NightJay0044

  • Former Staff
  • ****
  • Let's make OFPEC great again
    • My Steam Workshop
Multiple argument passing and loops
« on: 31 Oct 2008, 05:22:35 »
Hi all in this script there is a thing I don't understand.  It's placing the two variables of the (if) command "?" into parenthesis.  Why do they have to do this?

Quote
_Unit = _this select 0
_Vehicle = _this select 1

 
#Update

 
? (_Unit in _Vehicle) : goto "BlowUp"

 
~2

goto "Update"

#BlowUp

_Vehicle setdammage 1

exit


Couldn't you just write
? _unit in _vehicle : Goto "blowup"

I don't understand why, thanks all.. :)


Who's hyped for Arma4, long live Arma!

Offline Gcfungus

  • Members
  • *
Re: Multiple argument passing and loops
« Reply #1 on: 31 Oct 2008, 09:39:16 »
I think I'm right in saying it doesn't matter.
Code: [Select]
?(_unit in _vehicle): goto "blowup"is the same as
Code: [Select]
?_unit in _vehicle:goto "blowup"I believe it's just to make it more readable. I always leave them in (it's only 2 characters) and it makes it much more easier to read and work with.
The object of war is not to die for your country, but to make the other bastard die for his.
~George Patton

Offline savedbygrace

  • Intel Depot
  • Administrator
  • *****
  • Be swift to hear...slow to speak...slow to wrath.
Re: Multiple argument passing and loops
« Reply #2 on: 07 Nov 2008, 15:59:48 »
Bracket use has always confused me as I am not a scripter. If I remember correctly my lesson from one of the fellows, conditions should be within the parenthesis and commands without. i.e ?!(alive unit1) && !(alive unit2) : goto "end". Square brackets [ ] are reserved for arrays and Curly brackets  { } are used with the foreach comand.


Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re: Multiple argument passing and loops
« Reply #3 on: 18 Dec 2008, 22:32:13 »
The use of parenthesis in source code is to do with so-called operator precedence or just with readability of the code.

Some operators in a given programming language have more weight as so to speak in comparison to other operators.

We need to have a way to explicitly tell in which kind of order our operators are executed. For this we can use parenthesis (doesn't have to be parenthesis, but in most programming languages parenthesis are used).

I will give a short example.

These two calculations give a different result:

a + b / c

(a + b) / c

In the first example what happens is that first b gets divided by c, and then the result of that computation is added together with a.

In the second example what happens is that first a and b are added together, and then the result of that computation is divided by c.

It is like that because the / operator has a higher precedence than the + operator.

These are simple examples but this thing called operator precedence really goes through the whole programming language, and you have to be aware of it when writing source code in any given programming language.

It is best to write the parenthesis if there is even a small possibility that someone will understand the statement wrong. So writing the parenthesis even when they are not technically needed is a readability help to help people understand the code correctly.

Let's take our example further. If our intent was really to first divide b with c and then add the result to a, we could write either

a + b / c

or

a + ( b / c )

and both would give us the correct result, as they are both technically correct. But for clarity of the code we should use the latter style, with the parenthesis, to make it clear for everyone reading the source code what exactly will happen on that line.

What the writer of the code in the first post of this thread probably wanted was to make sure that the code works (maybe wasn't sure if it does or not without the parenthesis so played it safe) or that it is easier to understand the code. The latter choice is more likely I think. It just makes it clearer code to read. If you would have more than one condition on the same line, then things get trickier and you should use the parenthesis also for ensuring the correct order of operations.

Offline savedbygrace

  • Intel Depot
  • Administrator
  • *****
  • Be swift to hear...slow to speak...slow to wrath.
Re: Multiple argument passing and loops
« Reply #4 on: 19 Dec 2008, 05:23:56 »
Fantastic! Thanks for stepping in and helping out with the technical aspect of this post. Without a doubt, this will benefit all future readers who are not educated in programming.