**The branching structure. Conditional statement**

** Questions**

- 1. What is the usefulness of the branching structure in the program?
- 2. What is the full form of the statement if a conditional branch in Pascal?
- 3. What is the short form of the operator a conditional branch?
- 4. Examples of using the condition statements that take the full form.
- 5. Examples of using the condition statement, that takes a short form.
- 6. Examples of using the nested conditional statements.

## Answers

**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“:

ifconditionthenstatement1elsestatement2;

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:

ifconditionthenbegin//multiple operators...endelsebegin//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:

ifconditionthenstatement;

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:

ifconditionthenbegin//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.

vara,b:real; //a, b - variables for which the maximum is searchedmax:real; //maximum...begin... //a, b - are givenifa>bthenmax:=aelsemax:=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:

...varx,f:real;begin... //x - is givenif-5<xthenf := x*x+8elsef := -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‘.

...varmin:real; ...begin... //x, y - are givenmin := x;ifmin<ythenmin := y; ...end;

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

varx, f:real;begin... //x - is givenifx<-6thenf:=3*x*x-x;if(-6<=x)and(x<=5)thenf:=sqrt(7-x);ifx>5thenf:=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.

...vara, b, c:integer; max:integer; //maximum value...begin... //a,b,c - are givenifa<bthenbeginifb<cthenmax:=celsemax:=b;endelsebeginifa<cthenmax:=celsemax:=a;end; ...end;