# The branching structure. Conditional statement

## Questions

##### 1. What is the usefulness of the branching structure in the program?

Sometimes is needed realize the branching in the program. In this case, the process of solving the problem occurs on the basis of performance or non-fulfillment of certain conditions.

In Pascal the selection of actions depending on certain conditions can be realized by the structure

`if ... then ... else ...`

or

`if ... then ... `

#### ⇑

##### 2. What is the full form of the statement if a conditional branch in Pascal?

The full form of the conditional statement “if“:

```if condition then
statement1
else
statement2;```

Operator action is the following: first of all, the value of expression “condition” is calculated. If it is true then the operator, which follows behind the word “then“, is executed (statement1). If it is false then the operator, that follows behind the word “else“, is executed (statement2).

Statements “statement1” and “statement2” may be composite. It means they may include several code strings. Such statements are taken in operator brackets “begin” … “end“. This need arises when, after the reserved words “then” and “else” you need to specify multiple statements.

In this case the general view of condition statement may be as follows:

```if condition then
begin
// multiple operators
...
end
else
begin
// multiple operators
...
end;```

#### ⇑

##### 3. What is the short form of the operator a conditional branch?

The short form of the operator a conditional branch can’t contain the “else” block and is following:

```if condition then
statement;```

In this case, the statement operates as follows. First, the value of logical (boolean) expression is calculated. If the result of logical expression “condition” is true then the statement, that behind the word “then” is executed. If the result is “false” then the statement, that follows behind the statement “if” is executed (in the statement “if … then” nothing is done).

When result of condition is true and you need to execute several operators the general view of condition statement may be following:

```if condition then
begin
// two or more statements
...
end;```

#### ⇑

##### 4. Examples of using the condition statements that take the full form.

Example 1. Code snippet of calculating maximum value between two variables.

```var
a,b:real; // a, b - variables for which the maximum is searched
max:real; // maximum

...

begin

...

// a, b - are given

if a>b then
max:=a
else
max:=b;

...

end;```

Example 2. Calculation of function value according to condition. Suppose we want to find the value of the function:

A code fragment that solves this problem:

```...

var
x,f:real;

begin

...

// x - is given

if -5<x then
f := x*x+8
else
f := -x*x*x+2;

// variable 'f' takes result

...

end;```

#### ⇑

##### 5. Examples of using the condition statement, that takes a short form.

Example 1. Code snippet, that calculates a minimum value between two variables ‘x‘ and ‘y‘.

```...

var
min:real;

...

begin

...

// x, y - are given
min := x;
if min<y then
min := y;

...

end;```

Example 2. Calculating of function value according the condition. Suppose you need to calculate the value of following function:

```var
x, f:real;

begin

...

// x - is given

if x<-6 then
f:=3*x*x-x;
if (-6<=x) and (x<=5) then
f:=sqrt(7-x);
if x>5 then
f:=8*x-3;

...

end;```

#### ⇑

##### 6. Examples of using the nested conditional statements.

The conditional statements may be nested. The degree of their nesting is unlimited.

Example. The searching of maximum value between three given numbers, which are saved in the variables a, b, c. The maximum value is written in the variable max.

```...

var
a, b, c:integer;
max:integer; // maximum value

...

begin

...

// a,b,c - are given
if a<b then
begin
if b<c then
max:=c
else
max:=b;
end
else
begin
if a<c then
max:=c
else
max:=a;
end;

...

end;```