This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
one_line [2018/09/19 15:01] supergnu created |
one_line [2018/09/19 17:27] (current) supergnu |
||
---|---|---|---|
Line 2: | Line 2: | ||
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' | ||
<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. | ||
</ | </ | ||
- | Two syntax exists one with an integrated else: | + | The ternary operator: |
<code c> | <code c> | ||
- | (CONDITION) ? TRUE: FALSE; | + | VARIABLE = (CONDITION) ? VALUE_TRUE: VALUE_FALSE; |
</ | </ | ||
<code c syntax1.c> | <code c syntax1.c> | ||
+ | Variable = (Captor > Threshold) ? ValueIfTrue : ValueIfFalse; | ||
+ | </ | ||
+ | A variation of this use case when using increment and decrement operators. | ||
+ | <code c syntax2.c> | ||
(Variable > Threshold) ? Variable-- : Variable++; | (Variable > Threshold) ? Variable-- : Variable++; | ||
</ | </ | ||
- | And a simple if, | + | The compact If syntax : |
<code c> | <code c> | ||
if (CONDITION) TRUE; | if (CONDITION) TRUE; | ||
</ | </ | ||
- | <code c syntax2.c> | + | <code c syntax3.c> |
if (Variable < 0) Variable = 0; | if (Variable < 0) Variable = 0; | ||
</ | </ |