Conditional operator

What does the ? mean in NW Script
The question mark (?) is called the ternary conditional and is a form of selection, much like an if/else assignment. The Lexicon has a brief but informative article covering the basic syntax.

You are probably familiar with using an if/else statement to check some condition and assign one of two values to a variable depending on the result: the ternary conditional does the same thing using just one line! Therefore if used wisely the ternary conditional can simplify or clarify some scripts. However it is one of the less well-known constructs exercise caution and comment well.

For example, if you wanted check if a number was odd or even you might write the following script: string sResult; // determine if number is odd or even using modulus if(nNumber % 2) {   sResult = "Odd"; } else {   sResult = "Even"; }

and while there is nothing wrong with that you might find it a little less cumbersome to rewrite it using the ternary conditional:

// determine if number is odd or even using modulus string sResult = (nNumber % 2) ? "Odd" : "Even";

You can also nest ternary conditionals: much in the same way as you can with an if/else if/else statement. However I would generally advise against this as it tends to result in greater confusion rather than greater clarity.

For example, if you wanted check if a number was positive, negative or zero you might write the following script:

string sResult; // determine if number is odd or even using modulo if(nNumber > 0) {   sResult = "Positive"; } else if(nNumber < 0) {   sResult = "Negative"; } else {   sResult = " Zero "; }

which again could be rewritten:

// determine if number is positive, negative or zero string sResult = (nNumber > 0) ? "Positive" : (nNumber < 0) ? "Negative" : "Zero";