{"name":"if","operator":null,"title":"if","type":"command","url":"https://community.bistudio.com/wiki/if","revid":369387,"updated":"2024-09-03T18:43:16Z","groups":["Program Flow"],"status":"ok","replaced_by":[],"games":[{"game":"ofp","name":"Operation Flashpoint","version":"1.85"},{"game":"ofpe","name":"Operation Flashpoint: Elite","version":"1.00"},{"game":"arma1","name":"Armed Assault","version":"1.00"},{"game":"arma2","name":"Arma 2","version":"1.00"},{"game":"arma2oa","name":"Arma 2: Operation Arrowhead","version":"1.50"},{"game":"tkoh","name":"Take On Helicopters","version":"1.00"},{"game":"arma3","name":"Arma 3","version":"0.50"}],"since":{"game":"ofp","name":"Operation Flashpoint","version":"1.85"},"arma3_since":"0.50","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"description":"This operator creates a If Type which is used in the if-construct as described here.","syntaxes":[{"syntax":"if condition","since":null,"effect":null,"exec":null,"params":[{"name":"condition","type":"Boolean","optional":false,"default":null,"description":"if it evaluates as true, the then clause is executed.  Otherwise, the else clause (if present) is executed","types":["Boolean"],"since":null,"text":"condition: Boolean - if it evaluates as true, the then clause is executed.  Otherwise, the else clause (if present) is executed"}],"returns":{"type":"If Type","types":["If Type"],"description":"predicate which will execute 1st or 2nd option when used. This predicate is used in then or exitWith commands","text":"If Type - predicate which will execute 1st or 2nd option when used. This predicate is used in then or exitWith commands"},"form":"unary","operator":null,"left":null,"right":{"text":"condition","types":["Boolean"]}}],"examples":[{"text":"```sqf\n_retVal = if (1 > 0) then { \"It's true\" } else { \"It's false\" };\nhint str _retVal;\n```","code":["_retVal = if (1 > 0) then { \"It's true\" } else { \"It's false\" };\nhint str _retVal;"]},{"text":"```sqf\n_val = if (true) then [{ \"true\" }, { \"false\" }];\nhint _val;\n```","code":["_val = if (true) then [{ \"true\" }, { \"false\" }];\nhint _val;"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["else","exitWith","then","Control Structures","Code Optimisation"],"notes":[{"user":"Ceeeb","date":"2007-01-31T04:08:00Z","text":"Any _local variables you declare within the body of an if/then statement (ie between the curly braces) are local to that 'if' statement, and are destroyed at the end of the statement. If you know you want to use the variable outside the 'if' statement, make sure your declare it before the 'if' statement."},{"user":"Galzohar","date":"2010-01-17T02:40:00Z","text":"If the condition is nil then neither the \"then\" nor the \"else\" section get executed, but the script will proceed with no error messages.\n\nExample code:\n\n```sqf\nsystemChat \"script started\";    // will get executed\nif (nil) then\n{\n    systemChat \"true\";            // will never get executed\n}\nelse\n{\n    systemChat \"false\";            // will never get executed\n};\nsystemChat \"script ended\";        // will get executed\n```"},{"user":"AgentRev","date":"2015-06-05T09:35:00Z","text":"If you only need to choose between 2 raw values, it is possible to use the following trick to avoid using code blocks, as required by the if command, which results in greater atomicity and faster execution:\n\n```sqf\n_result = [falseValue, trueValue] select condition;\n```\n\nThe select command treats \"false\" as 0 and \"true\" as 1, therefore you can feed it a condition determining the array index of the value to be returned. Here is another example:\n\n```sqf\n_result = [1,-1] select (_this < 0); // if _this is less than 0, _result will be equal to -1, otherwise it will be 1\n```"},{"user":"DreadedEntity","date":"2022-05-19T01:11:46Z","text":"When using **if** to assign values to a variable, if you not have an else block it is treated as nil and returned, setting the variable value to nil. A3 2.08.149102\n\n```sqf\nprivate _myVar = \"value\";\nprivate _condition = false;\n_myVar = if (_condition) then { \"another value\" }; //perfectly valid sqf\nsystemChat str (isNil \"_myVar\"); //returns true\n//_myVar is now nil\n_myVar = if (_condition) then { \"another value\" } else { \"value\" };\nsystemChat _myVar; //returns \"value\"\n```\n\nThis behavior can be both desirable and undesirable depending on your needs"}],"categories":["ArmA: Armed Assault: Scripting Commands","Arma 2: Operation Arrowhead: Scripting Commands","Arma 2: Scripting Commands","Arma 3: Scripting Commands","Command Group: Program Flow","Introduced with Operation Flashpoint version 1.85","Operation Flashpoint: Elite: Scripting Commands","Operation Flashpoint: New Scripting Commands","Operation Flashpoint: Scripting Commands","Scripting Commands","Take On Helicopters: Scripting Commands"]}