mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Dec 2002
Posts: 417
O
Othello Offline OP
Fjord artisan
OP Offline
Fjord artisan
O
Joined: Dec 2002
Posts: 417
I would like to know if there is any difference in how brackets are used within a statement?

is there a difference between brackets and parentheses used in a statement?

The way I see it both, of these statements are correct


ON @:TEXT:*:#: {
if $1 == op { mode $chan +o $2- }
}

ON @:TEXT:*:#: {
if ($1 == op) mode $chan +o $2-
}


from what I gather this one the extra brackets are useless within the statement

ON @:TEXT:*:#: {
if ($1 == op) [color:red]{ [color:green] mode $chan +o $2- [color:red]}

}




Intelligence: It's better to ask a stupid question, then to prove it by not asking....
Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
If you strictly follow the help file, only () { } is acceptable, but you're right, all three of those formats work.

Joined: Dec 2002
Posts: 417
O
Othello Offline OP
Fjord artisan
OP Offline
Fjord artisan
O
Joined: Dec 2002
Posts: 417
The reason I ask is because I see different formats amung other scripts I just wanted to know if there was a correct format on scripting....




Intelligence: It's better to ask a stupid question, then to prove it by not asking....
Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
if (blah) { do stuff } is the correct format, but you can get away with using either () or {}.

Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
The () parentheses are meant to allow you to control the order of operations within the comparison. Example:

if (%a && %b || %c) { ;commands }

if (%a && (%b || %c)) { ;commands }

if ((%a && %b) || %c) { ;commands }

In the first statement, the comparison will be evaluated in whatever priority mIRC uses (|| usually has lower priority than &&). In the second and third statements, you can see in exactly what order they will be evaluated mIRC (the || vs && priority is overridden by the parentheses).

As for the correctness, I always use at least one set of () in my if/while statements. I find it easier to follow the code that way. I also do this because I don't use the {} brackets when there is only one /command after it.

if (%a == a) echo -a a

if (%b == b) {
echo -a b
echo -a bb
}

-genius_at_work

Joined: Dec 2002
Posts: 417
O
Othello Offline OP
Fjord artisan
OP Offline
Fjord artisan
O
Joined: Dec 2002
Posts: 417
Thanks Genius at work that puts the arguments of a statement in perspective.

genius_at_work
Quote:
The () parentheses are meant to allow you to control the order of operations within the comparison.


So, I guess the interpretation of how a statement is written is left to the scriptor, as long as all command arguments in the statement are in perspective.




Intelligence: It's better to ask a stupid question, then to prove it by not asking....
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Regarding the order, mirc evaluates conditions from left to right and && and || have equal precedence (an ages old behaviour that I would like changed).


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Aug 2006
Posts: 3
A
Self-satisified door
Offline
Self-satisified door
A
Joined: Aug 2006
Posts: 3
It would take fractionally longer to evaluate:
if ($1 == op) { mode $chan +o $2- }
than it would:
if ($1 == op) mode $chan +o $2-

This is why if its only one command following the if I tend not to use the {}

From a purely personal stand point I find it very hard to read code thats missing the parentheses around the condition, it looks more like an alias with parameters than an if-then-else statement.


--
Albie
Joined: May 2006
Posts: 93
Babel fish
Offline
Babel fish
Joined: May 2006
Posts: 93
mIRC reads the { } like commands (so it's a little slower), so if you have just one line of code is better to use
if (%a > %b) echo -a %a is bigger!

if the lines are more than one you have to use the { }
if (%a > %b) {
echo -a %a is larger!
echo -a %b is smaller!
}

(i don't like the form command1 | command2 because is less readable)

I also always use the () in the if/while conditions, the code is more readable and don't think that it's slower


Link Copied to Clipboard