# Conditional structures/C

Conditional structures/C is part of Conditional Structures. You may find other members of Conditional Structures at Category:Conditional Structures.

### if-then-else

<lang c>int main (int argc, char ** argv) {

``` int input = 2;
```
``` if (3 == input) {
/* Do something */
}
```

``` if (3 == input) {
/* Do something */
} else {
/* Do something else */
}
```

}</lang>

One line predicates do not require curly braces (although their usage is often preferred for readability):

<lang c>if (cond)

``` expr;
```

if (cond)

``` expr;
```

else

``` expr;</lang>
```

And these may be mixed:

<lang c>if (cond)

``` expr;
```

else

```{
/* multiple expressions */
}</lang>
```

<lang c>if (cond)

```{
/* multiple expressions */
}
```

else

```expr;</lang>
```

### Short-circuit conditionals

The short-circuit evaluation of the && and || boolean operators are also often used for control flow. <lang c>if (node != NULL && node->next != NULL && guarded_test(node))

```  node->next = node->next->next;</lang>
```

Neither the assignment nor guarded_test() will be called if the previous two conditions aren't met. Other languages such as Pascal don't make that guarantee.

### switch

<lang c>int main (int argc, char ** argv) {

``` int input = 42;
```
``` switch (input) {
case 0:
/* Do something, because input = 0 */
break;
case 1:
/* Do something, because input = 1 */
break;
case 2:
/* Do something, because input = 2 */
/* fall through to the next statement if there is no "break" */
default:
/* Do something else. */
break; /* Optional */
}
```

}</lang>

A little known fact is that the default branch need not be the last one:

<lang c>int main(int argc, char* argv[]) {

``` switch (argc)
{
default:
/* this will be executed if argc is neither 2 nor 3 */
break;
case 2:
/* this will be executed if argc is 2 */
break;
case 3:
/* this will be executer if argc is 3 */
}
```

}</lang>

### Ternary ?:

Conditionals in C can also be done with the ternary operator, ?:. The arguments are expressions, and a?b:c is an expression as well. However, since many things in C are expressions (this especially includes assignments and function calls), ?: can be used for those, too. However, the if/else construct is usually more readable and therefore preferred.

<lang c>int main(int argc, char ** argv) {

``` int input = 2;
int output = (input == 2? 42 : 4711);  /* sets output to 42 */
int output2 = (input == 3? 42 : 4711); /* sets output2 to 4711 */
```
``` int do_something();
int do_something_else();
input == 1? do_something() : do_something_else(); /* only calls do_something_else() */
```

}</lang>

### Preprocessor Techniques (compile-time)

Conditional compile based on if a certain macro exists, <lang c>#ifdef FOO /* compile this only if macro FOO exist */

1. endif</lang>

Conditional compile based on if a macro doesn't exist <lang c>#ifndef FOO /* only compiled if macro FOO does not exist */

1. endif</lang>

Conditional compile based on if a certain macro exists, with else clause <lang c>#ifdef FOO /* compile this only if macro FOO exist */

1. else

/* compile this only if macro FOO does not exist */

1. endif</lang>

Conditional compile based on expression <lang c>#if defined(FOO) && FOO == 1 /* only compiled if macro FOO is defined and expands to a constant expression evaluating to 1 */

1. endif</lang>

<lang c>#if FOO /* only compiled if macro FOO is defined and expands to a constant expression of non-zero value */

1. endif</lang>

Chain of conditionals <lang c>#if defined(FOO) /* only compiled if macro FOO is defined */

1. elif defined(BAR)

/* only compiled if macro FOO is not defined, but macro BAR is */

1. else

/* only compiled if neither FOO nor BAR is defined */

1. endif</lang>

Typical usage: Include guards <lang c>#ifndef FOO_H_ALREADY_INCLUDED