Last Updated:

Operator else in Pascal

Working at Pascal is not too difficult if you know which algorithms to apply. Today we will tell you how to work with different types of operators (conditional and composite), as well as share some tips. We wish you a pleasant reading!

Operator else in Pascal

Working with a conditional operator in Pascal

Newcomers to Pascal use mostly linear algorithms. Work with algorithms such as branching is considered a higher level. But higher does not mean more complicated, which we will now try to convince you of, describing everything in detail.

There is such an operator in Pascal, which is called Conditional. With him, everything is quite simple - he sets the right condition.

What it looks like:

We present you an abbreviated version:

if (condition) then
operator;

sometimes the full version is preferred:

if (condition) then
operator1
else
operator2;

This is the very if that will set the condition that changes the sequence of the statement. Note that as a result, your condition has two developments of the event - either it receives the title of true (true) or false (false).

Remember! if — translated as "if", sets the condition; then — translated as "that"; else — translates as "otherwise".

But how does this whole structure work? First of all, the program checks the condition (which was set by us after if). If it is true, the statement number 1 is executed (it is written after then). If the condition turns out to be untrue, the operator number 2 will be executed.

Working with a Composite Operator in Pascal

It so happens that you have much more than one condition. In this case, the program should enclose them in a block that begins with begin and ends with the phrase end. This construction is called a composite operator, and programmers call each such block operator brackets.

Let's look at an example:

if (expression) then
begin
statement1;
operator2;
end
else
begin
operator1;
operator2;
end;

Separately, we take out that you cannot put a semicolon before else.

Examples of operations performed in Pascal

Pascal has a specific list of active operators. Here are some of them:

  • > means that one number is greater than the other;
  • < means that one number is smaller than another;
  • >= means that one number is greater than or equal to another;
  • <= means that one number is less than or equal to another.

Example 1. Suppose that you want to buy apples and you have 10 rubles.

var
c: real;
begin
writeln('Enter the price of apples: ');
readln(t);
if c > 10 then writeln('You don't have enough')
else
if c < 10 then writeln('You'enough')
else writeln('You bought apples');
readln
end.

Example 2. You have a triangle that has sides k, l, m. You need to check if there is a triangle whose sides are equal to these numbers.

From the school course of geometry, it should be recalled that a triangle exists if three conditions are met, namely: k + l>m, l + m>k, m + k>l. In addition, these three expressions must be executed simultaneously. To do this, we will use the operator and, which can combine them. What will this program look like:

program enteryourname;
var
k, l, m: real;
begin
writeln('Enter three numbers greater than zero:');
readln(k, l, m);
write('Triangle, sides of which ', k, ', ', l, ' and ', m);
{ Checking the triangle's subsistence condition: }
if (k+l>m)and(l+m>k)and(m+k>l) then writeln(' such a triangle exists')
else writeln('such a triangle does not exist');
readln
end.

We hope that the article was useful to you and you were able to deal with this topic completely and completely.