Control Structures – Talk

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
m (Text replacement - " (={2,})([^ = ])(.*)([^ = ])(={2,}) * " to " $1 $2$3$4 $5 ")
 
(22 intermediate revisions by 10 users not shown)
Line 1: Line 1:
Tried "if" and "switch" structures. "If" examples are wrong here! They don't work, because each block '{ }' must end with ';'. Am I right?
==Reinstate the dedicated pages?==
I think this page is very useful to give people an overview of the different control structures, and we should definitely keep it. But I think we should '''also''' have the dedicated pages we used to have for each specific command. That way we have a proper place for notes and discussions for the specific commands (which wouldn't quite fit in here). --[[User:Kronzky|Kronzky]] 00:47, 15 January 2007 (CET)
:I agree as well. I'm already missing those commands alot. [[User:Hoz|hoz]]
:: I agree, the description is now incomplete - reference should always be complete. Partial constructs may be used in an interesting (albeit strange) ways, like assigning result of if (x) into a variable and then using that variable together with an else clause. It is not a typical usage, but it is possible. Overall, it may be interesting for the scripter to know there are actually no "Control Structures" in this scripting language, only expressions. --[[User:Suma|Suma]] 14:29, 15 January 2007 (CET)
----
Sigh


Also I can't get "switch" to work , anyone know how to use it? These examples are not working... --[[User:Messiah2|Messiah(UA)]] 12:19, 25 December 2006 (CET)
[[User:Planck|Planck]] 05:20, 15 January 2007 (CET)


:1. yes that's true if used in [[SQF syntax]] scripts or functions. The structures are like any [[Statement]]: If used in [[SQF syntax]] scripts or functions, you have to add the semicolon ";" (except if you want to use the structure as return value of the function). Should we add the semicolons in the examples?
:Is that a ''No''? Or a sigh of relief that we're finally going to have the dedicated pages back? ;) --[[User:Kronzky|Kronzky]] 07:17, 15 January 2007 (CET)


:2. I just noticed that there doesn't seem to be a ":" after "default". Also, it could be that you ''must'' include a default block, even if empty. Could you test that, maybe?
I personally find the dedicated pages completely useless: IMO they are impossible to understand because completely out of context. If a dummy goes to a page about "else" where he finds that "else" requires an if-construct and an else-construct, he will understand exactly nothing. Same for all the other. Waste of wiki space and confusing more than solving anything, if you ask me.


:Apart from that, what are your errors? Take care that you have to write control structures into a single line if used in [[SQS syntax]] scripts. --[[User:Hardrock|hardrock]] 15:01, 25 December 2006 (CET)
But if you keep the structures together, e.g. if-then-else, and explain it within proper context, it is IMO clearer to understand what the "else" is really about.


::A default block isn't necessary. --[[User:TeRp|TeRp]] 15:10, 25 December 2006 (CET)
PS: Additionally, it's an old webdesign guideline that a page about some topic should exist only once on a website. "Normal" users think that the "else" page is the page that explains most about "else". It is confusing if you go to "else" and have to switch to ''another'' page to really find out about "else". --[[User:Hardrock|hardrock]] 12:21, 15 January 2007 (CET)
: Most programming documentations consist of two parts: reference and overview. Reference is always expected to be concise and complete, overview is expected to provide an overall and easy to understand view. This pages now serves the second role quite well, but there is no reference now (see also my other note above). --[[User:Suma|Suma]] 14:29, 15 January 2007 (CET)


:: AFAIK Control structures are intended for SQF. If so, then it would be definitely right to add semicolons in examples! Cocerning "switch", I was writing sqf and I can confirm, that default block is not necessary, I tried with and without it. And I can't write exact error right now (i'm at work), but I remember that something was wrong near "(VARIABLE) do {". I'll check it in the evening today and post here exact error. --[[User:Messiah2|Messiah(UA)]] 11:11, 26 December 2006 (CET)
:: I restored most of the commands, but added a '''see also''' back to the control structures, over the past couple of days, I've found the control structures invaluable for examples. If there are others then please revert them back and add the see also. [[User:Hoz|hoz]]


:: Figured it out. That was my bad and switch is working right now, but anyway example is missing one semicolon. My mistake was that I wrote "default:" automatically and I didn't notice, that "default" is right! So 100% working code is
== Bad example ==
      switch (VARIABLE) do
      {
        case VALUE:
        {
        };
        default
        {
        };
      };


[[step]] command should be added to list --[[User:T D|T_D]] 15:41, 25 December 2006 (CET)
The FOR-loop examples are missing a FORMAT command:
 
for [{_i=0}, {_i<10}, {_i=_i+1}] do
{
    player globalChat format["%1",_i];
};
 
We don't wanna scare away the scripting n00bs, do we?  ;)  [[User:DrStrangelove]]
 
:Thanks for pointing that out!
:I've fixed it, but be aware that these pages are open to ''everyone''! So if you see a mistake like that, feel free to just go in there and fix it. That's the great thing about Wikis! --[[User:Kronzky|Kronzky]] 15:43, 4 April 2007 (CEST)
 
== waitUntil / @ ==
 
Should the waitUntil (@ for SQS) commands be mentioned in this article? I can't find much mention of them in the scripting topics in the wiki. --[[User:Ceeeb|Ceeeb]] 03:35, 5 April 2007 (CEST)
:Yeah - I've been wondering myself why that one was missing here.
:I'd definitely be all for including it. --[[User:Kronzky|Kronzky]] 03:53, 5 April 2007 (CEST)
 
== Consistency and 'almost' 'C' language ==
 
Any chance of some consistency from BIS with the 'almost C' SQF language.
C consistently uses round brackets for conditions and curly brackets for
code blocks.
 
SQF uses is inconsistent in its use of round and curly brackets.
i) "while" requires curly brackets for condition and the 'do' code block.
ii) "if" and "switch" require round brackets for the expression/condition.
iii) for[{init};{cond};{loop}] is just a nightmare.
 
Also allowing the foreach keyword at the TOP of the code block as oppose to
at the bottom would vastly improve code readability.
 
Allowing "else if" would also be nice.
 
[[User:BarmyArmy|BarmyArmy]]
 
== if .. then ... else  ==
 
The comment against the else-statement is horrifically accurate in that it will only execute if the condition is FALSE.
 
Care must be taken with if .. then ...else  statements as ArmA booleans are tristate, having three values true,false and 'bool'.
Consider the following statement
 
[CODE]
if ( alive somePlayer) then {
hint "he's alive";
} else {
hint "He's dead jim.";
};
[/CODE]
 
Seems straight forward until you consider what happens if 'somePlayer' is undefined; the answer is than NEITHER the if-block nor the else-block will get executed. As alive objNull returns neither true nor false.
 
~~Barmy
 
Pitfall with semicolon valid only in the OFP (but no ArmA). In OFP, the expression ending semicolon returns nil, but in ArmA will return the last computed value.
 
~~DenV

Latest revision as of 19:20, 31 January 2021

Reinstate the dedicated pages?

I think this page is very useful to give people an overview of the different control structures, and we should definitely keep it. But I think we should also have the dedicated pages we used to have for each specific command. That way we have a proper place for notes and discussions for the specific commands (which wouldn't quite fit in here). --Kronzky 00:47, 15 January 2007 (CET)

I agree as well. I'm already missing those commands alot. hoz
I agree, the description is now incomplete - reference should always be complete. Partial constructs may be used in an interesting (albeit strange) ways, like assigning result of if (x) into a variable and then using that variable together with an else clause. It is not a typical usage, but it is possible. Overall, it may be interesting for the scripter to know there are actually no "Control Structures" in this scripting language, only expressions. --Suma 14:29, 15 January 2007 (CET)

Sigh

Planck 05:20, 15 January 2007 (CET)

Is that a No? Or a sigh of relief that we're finally going to have the dedicated pages back? ;) --Kronzky 07:17, 15 January 2007 (CET)

I personally find the dedicated pages completely useless: IMO they are impossible to understand because completely out of context. If a dummy goes to a page about "else" where he finds that "else" requires an if-construct and an else-construct, he will understand exactly nothing. Same for all the other. Waste of wiki space and confusing more than solving anything, if you ask me.

But if you keep the structures together, e.g. if-then-else, and explain it within proper context, it is IMO clearer to understand what the "else" is really about.

PS: Additionally, it's an old webdesign guideline that a page about some topic should exist only once on a website. "Normal" users think that the "else" page is the page that explains most about "else". It is confusing if you go to "else" and have to switch to another page to really find out about "else". --hardrock 12:21, 15 January 2007 (CET)

Most programming documentations consist of two parts: reference and overview. Reference is always expected to be concise and complete, overview is expected to provide an overall and easy to understand view. This pages now serves the second role quite well, but there is no reference now (see also my other note above). --Suma 14:29, 15 January 2007 (CET)
I restored most of the commands, but added a see also back to the control structures, over the past couple of days, I've found the control structures invaluable for examples. If there are others then please revert them back and add the see also. hoz

Bad example

The FOR-loop examples are missing a FORMAT command:

for [{_i=0}, {_i<10}, {_i=_i+1}] do {

   player globalChat format["%1",_i];

};

We don't wanna scare away the scripting n00bs, do we?  ;) User:DrStrangelove

Thanks for pointing that out!
I've fixed it, but be aware that these pages are open to everyone! So if you see a mistake like that, feel free to just go in there and fix it. That's the great thing about Wikis! --Kronzky 15:43, 4 April 2007 (CEST)

waitUntil / @

Should the waitUntil (@ for SQS) commands be mentioned in this article? I can't find much mention of them in the scripting topics in the wiki. --Ceeeb 03:35, 5 April 2007 (CEST)

Yeah - I've been wondering myself why that one was missing here.
I'd definitely be all for including it. --Kronzky 03:53, 5 April 2007 (CEST)

Consistency and 'almost' 'C' language

Any chance of some consistency from BIS with the 'almost C' SQF language. C consistently uses round brackets for conditions and curly brackets for code blocks.

SQF uses is inconsistent in its use of round and curly brackets. i) "while" requires curly brackets for condition and the 'do' code block. ii) "if" and "switch" require round brackets for the expression/condition. iii) for[{init};{cond};{loop}] is just a nightmare.

Also allowing the foreach keyword at the TOP of the code block as oppose to at the bottom would vastly improve code readability.

Allowing "else if" would also be nice.

BarmyArmy

if .. then ... else

The comment against the else-statement is horrifically accurate in that it will only execute if the condition is FALSE.

Care must be taken with if .. then ...else statements as ArmA booleans are tristate, having three values true,false and 'bool'. Consider the following statement

[CODE] if ( alive somePlayer) then { hint "he's alive"; } else { hint "He's dead jim."; }; [/CODE]

Seems straight forward until you consider what happens if 'somePlayer' is undefined; the answer is than NEITHER the if-block nor the else-block will get executed. As alive objNull returns neither true nor false.

~~Barmy

Pitfall with semicolon valid only in the OFP (but no ArmA). In OFP, the expression ending semicolon returns nil, but in ArmA will return the last computed value.

~~DenV