User Tools

Site Tools


one_line

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
one_line [2018/09/19 17:16]
supergnu
one_line [2018/09/19 17:27]
supergnu
Line 1: Line 1:
 ====== One line If in C ====== ====== One line If in C ======
  
-Often called ternary operators. 
 A really handy way to write condensate and yet easily readable code in C is using one line if when doing only simple operation in the if. \\ A really handy way to write condensate and yet easily readable code in C is using one line if when doing only simple operation in the if. \\
 +There's two way of doing this, using a ternary operator (here ?) or a compact If syntax.
 <WRAP center round tip 60%> <WRAP center round tip 60%>
 It is absolutely not recommended to use this syntax everywhere but when only incrementing a variable for example, it is great. It is absolutely not recommended to use this syntax everywhere but when only incrementing a variable for example, it is great.
 </WRAP> </WRAP>
  
-Two syntax exists one with an integrated else:+The ternary operator:
 <code c> <code c>
-(CONDITION) ? TRUEFALSE;+VARIABLE = (CONDITION) ? VALUE_TRUEVALUE_FALSE;
 </code> </code>
 <code c syntax1.c> <code c syntax1.c>
 +Variable = (Captor > Threshold) ? ValueIfTrue : ValueIfFalse;
 +</code>
 +A variation of this use case when using increment and decrement operators.
 +<code c syntax2.c>
 (Variable > Threshold) ? Variable-- : Variable++; (Variable > Threshold) ? Variable-- : Variable++;
 </code> </code>
-And a simple if,+The compact If syntax :
 <code c> <code c>
 if (CONDITION) TRUE; if (CONDITION) TRUE;
 </code> </code>
-<code c syntax2.c>+<code c syntax3.c>
 if (Variable < 0) Variable = 0; if (Variable < 0) Variable = 0;
 </code> </code>
one_line.txt ยท Last modified: 2018/09/19 17:27 by supergnu