Control structures

Location
1. Courses

/

2. Quick C course

/

3. Control structures

Logical operations

Logical operations are operations that return a boolean. A boolean is a value that can either equal 1 (true) or 0 (false). The following conditional operation tests if the variable 'level' equals 50:

`level == 50; // Returns 1 (true) if 'level' equals 50.`

The following, if 'level' is smaller than 40:

`level < 40;`

Here, we test if the variable 'level' is bigger than 60 OR if 'gold' is bigger than 100:

`level > 60 || gold > 100;`

If 'health' is smaller than 100 AND 'cooldown' equals 0:

`health < 100 && cooldown == 0;`

Here is a table with the logical operators:

OperatorThe result is true if
==Both operands are equal.
!=Both operands are different.
<The left operand is smaller than the right operand.
>The left operand is bigger than the right operand.
<=The left operand is smaller or equal to the right operand.
>=The left operand is bigger or equal to the right operand.
&&Both operands equal true (1).
||At least one of the operands equal true (1).
!Inverts the value of the boolean as right operand (true (1) becomes false (0) and false (0) becomes true (1)). That operator only takes one operand.

Reacting to the result of logical operations

A control structure is used to change the execution of a program in function of the result of a logical operation.

if

Here, we execute the instructions inside the braces only if the condition inside the parentheses equals 1 (true):

```if(level >= 60) { } ```

We can add 'else if' to test an other condition if the one above is false:

`123456````if(level >= 60) { } else if(level >= 40) { } ```

And finally, we can add 'else' to execute a set of instructions only if all the conditions above are false:

`123456789````if(level >= 60) { } else if(level >= 40) { } else { } ```

A complete example:

main.c

`123456789101112131415````#include <stdio.h> int main() { int level = 93; if(level == 100) printf("Maximum level.\n"); else if(level >= 60) printf("High level.\n"); else printf("Low level.\n"); return 0; } ```

while

The control structure 'while' repeats the instructions inside its braces, as long as the content of its parentheses equals 1 (true):

```while(nbTimes > 0) { nbTimes--; } ```

for

The loop 'for' is sightly more complex. It is used to repeat instructions a specific amount of times.

The content of its parentheses is divided into 3 sections:

1. The definition of the counter which keeps track of the number of times the loop iterated. Ex. int c = 0;
2. The condition that must be true for the loop to continue. Ex. c < 10;
3. The instruction that will be executed at the end of each loop. Ex. c++

Example:

```for(int c = 0; c < 10; c++) { printf("%d\n", c); } ```

Note that the counter (Above, it is the variable 'c') only exists within the braces below the 'for'.

Braces

The braces of control structures are optional when they contain only one instruction:

`123456789````if(grade >= 60) printf("Passing.\n"); else if(grade >= 58) { grade = 60; printf("Passing **.\n"); } else printf("Failing.\n"); ```

switch case

The 'switch case' structure allows to execute a specific set of instructions depending on the value of a variable:

`12345678910111213````int var = 5; switch(var) { case 2: // Code to execute if the value of 'var' is 2. printf("Two"); break; case 4: // Code to execute if the value of 'var' is 4. printf("Four"); break; default: // Code to execute if the value is something else. printf("Other number"); } ```

The instruction 'break;' is used to leave the 'switch case' structure and it is required when the 'case' is not the last one.