Chapter 7. Branching and Decisions

Table of Contents

7.1. Conditional execution
7.2. Conditions and Boolean expressions
7.3. Logical operators
7.4. Alternative execution
7.5. Chained conditional execution
7.6. Nested conditions
7.7. Solutions

7.1. Conditional execution

Branching or conditional statements

Branching is a feature provided by programming languages that makes it possible to execute a sequence of statements among several possibilities.

Figure 7.1 shows a general schema of a simple conditional statement.

Figure 7.1. Flow of execution of a simple condition

The if statement has to be followed by a condition. Then a block is opened by a colon. This block contains the sequence of statements that has to be executed if the condition is fulfilled. Figure 7.3 and Figure 7.2 highlight the structural parts of the if statement.

Figure 7.2. If statement

Figure 7.3. Block structure of the if statement