Last Updated:

Conditional operator in Pascal

Conditional operator in Pascal

Pascal is a high-level programming language. It was developed by Niklaus Wirth in 1970. Wirth's goal was to create a high-level JP that could be taught to students. As a result, Pascal came out of universities "into the people". Professionals began to write complex programs in it, and the language itself is still supported. So, let's move on to familiarity with the language.

Alphabet and punctuation

Pascal includes a set of symbols familiar to everyone:

  • Letters of the Latin alphabet from A to Z (lowercase and uppercase);
  • Arabic numerals from 0 to 9;
  • Delimited characters: comma, semicolon, period, colon, apostrophe, quotation marks, parentheses, space;
  • Arithmetic signs: + (addition), — (subtraction), * (multiplication), / (division);
  • Relationship signs: > (greater), < (less), >= (greater than or equal to), <= (less than or equal), <> (not equal), = (equals).

Data Types

A type defines how the computer is internally represented for an object, as well as the actions that can be performed on it. Data can be variable or constant.

  • Integer is an integer. Numbers ranging from -32768 to 32767
  • Real is a real number. Numbers from 2.9 * 10-39 to 1.7 * 1038
  • Char is a symbol. Includes 1 character
  • String - A string. Includes up to 256 characters
  • Boolean is a boolean type. True / False

Let's write a simple program that displays a message.

Program myMessage;
Const Message = "I program in Pascal";
Begin
WriteLn(Message);
End.

Let's break down the lines.

  • Program — a reserved word with the name (identifier) of the program.
  • Const Message – declare an immutable Message value with the identifier "I program on Pascal".
  • Begin – the start of the program.
  • WriteLn(Message) is an output statement with a pre-declared constant that we want to display.
  • End. – end of the program

As you can see, it's simple. Reserved words are written in plain English.

Let's pay attention to what goes before Begin ... End. This is the descriptive part of the program. It contains non-standard identifiers used in executable statements.

Comments

Comment in programming is a part of the program that carries explanations for the developer himself. This part does not affect the semantics, hence the result of compilation in the program.

The syntax for a comment in Pascal is as follows:

{ this is a comment }
{* and that too *}

A comment can contain an arbitrary number of characters. Investing them in each other is unacceptable.

Standard Operators, Procedures, and Functions

I/O Operators

The interaction of the program and the user is one of the fundamental principles of programming. Consider what procedure helps the user to enter data.

Read(x,y,z,...);
Readln(x,y,z,...);

These operators allow the user to enter values from the keyboard and assign them to the variables x,y,z.
The Readln() statement differs from Read() in that after entering the last variable, the cursor is moved to the beginning of a new line.

There's a trick. If ReadLn(); insert at the end of the program, it will "stop", waiting for the user to enter data. Now let's look at the output. He is also served by two commands:

  • Write();
  • Writeln();

Output elements can be variable names, expressions, constants. Before the computer displays the values of the expressions, it will first calculate. List items are separated by commas. The difference between them is as follows: after executing the Writeln statement, a transition to a new line occurs, and after write is executed, the transition to a new line does not occur and printing on subsequent Write or Writeln output commands will occur on the same line.

Simple arithmetic

Pascal supports addition and subtraction, multiplication and division, as well as modulo division and integer division. Simple operations are called binary (between 2 operands). Some of these operations are familiar to us from school. It is worth noting that here, as in mathematics, there is a priority of calculation, which can be changed using parentheses.

Boolean

The results of these operations return true/false.

  • NOT — denial ("NOT");
  • AND — multiplication ("AND");
  • OR — addition ("OR");
  • XOR — "Exclusive OR".

Comparison operators

They're relationship operators.

  • — more;
  • < is less;
  • = — equals;
  • <> is not equal;
  • >= is greater than or equal to;
  • <= is less than or equal;

Relationship operations can involve not only numbers, but also symbols, strings, sets, and pointers.

Conditional

He is also a conditional transition operator (there is an unconditional transition). A condition is a Boolean expression, depending on which one of the two alternative branches of the algorithm is selected.

If the value of the condition is TRUE, the statement 1 written after the then keyword will be executed. Otherwise, statement 2 following the word else will be executed, and statement 1 is ignored. After executing these statements, the program proceeds to execute the command that immediately follows the if statement.

Example with the "even/odd number" check:

var n: integer;
begin
write('Enter an integer:');
readln(n);

if n mod 2 = 0 then
write('It is even.')

else
write('It's odd.');
readln
end.

 

While loop operator

 

A cycle with a precondition. Its header contains a Boolean expression. If it is true, the loop body is executed, if false it is not. Once the body of the loop has been executed, the program progress returns to the loop header again. The condition is again checked for truth. The body of the loop will execute as many times as the Boolean expression will return true. Therefore, it is very important in the body of the loop to provide for a change in the variable that appears in the title of the loop, so that someday the situation is bound to occur false. Otherwise, there will be a so-called looping, one of the most unpleasant mistakes in programming.

Example:

where
in, n: integer;

begin
write (‘Количество знаков: ‘);
readln (n);

i := 1;
while i <= n do begin
write (‘(*) ‘);
i := i + 1
end;

readln
end.

Case statement

 

It's a choice operator. It is used in cases where, depending on the value, you need to execute one or more operators. It can be considered a simplification of the if then else construct.

program RateToText;
var
rating : integer;
begin
write(‘Введите оценку 1-5 ‘);
readln(rating);

case(rating) of
1,2: writeln(‘Неудовлетворительно’);
3: writeln(‘Удовлетворительно’);
4: writeln(‘Хорошо’);
5: writeln(‘Отлично’);
end;

readln;
end.

 

Break statement

 

It instantly interrupts the cycle.

var
S : String;
begin
while True do
begin
ReadLn(S);
if S = » then
Break;
WriteLn(S);
end;
end.

The break call must be written inside the loop.

Arrays

 

Arrays are a collection of elements of the same type. In Pascal in the massif:

  • Each component can be explicitly identified and directly accessible;
  • the number of components in an array is determined when it is described and does not change in the future.

To denote the components of the array, the name of the array variable and the so-called indexes are used, which usually indicate the desired element. The index type can only be ordinal (except longint). The most commonly used interval type (range).

Example 1. The input and output of a one-dimensional array.

 

const
n = 5;
type
mas = array[1..n] of integer;
var
a: mas;
i: byte;
begin
writeln(‘введите элементы массива’);
for i:=1 to n do readln(a[i]);
writeln(‘вывод элементов массива:’);
for i:=1 to n do write(a[i]:5);
end.

You can define a variable as an array directly when describing it, without first describing the type of the array, for example:

var a,b,c: array[1..10] of integer;

If arrays a and b are described as:

var
a = array[1..5] of integer;
b = array[1..5] of integer;

then variables a and b are considered to be of different types.

If the array types are identical, then in the program one array can be assigned to another. In this case, the values of all variables in the same array will be assigned to the corresponding elements of the second array.
However, relationship operations are not defined on arrays. You can only compare two arrays element by element.

Since the type that follows the of keyword in the array description is any Type of Turbo Pascal, it can be another array. For example:

 

type
mas = array[1..5] of array[1..10] of integer;

Thus, the concept of a multidimensional array arises. The nesting depth of arrays is arbitrary, so the number of elements in the list of index types (array dimension) is not limited, but cannot be more than 65520 bytes.

Conclusion

 

We got acquainted with the basics of the Pascal language. This language is primarily educational. The reward for its persistent study will be the skill of competent programming.