X belongs to the segment. Topic: Boolean data type

Lesson from the series " Geometric Algorithms»

Hello dear reader!

Today we will consider another typical problem from the series of geometric algorithms. Let's write a function that will check belonging arbitrary line segment given by the coordinates of its start and end.

To implement comparison operations on real data, we will write two more functions: the EqPoint() function, which will check whether two points on the plane coincide, and the RealMoreEq() function, which we will use to check the “>=” relation (greater than or equal to). We already know the reason for introducing special functions.

Task. Check, belongs whether line segment.

Let the points be the start and end points of the segment. is an arbitrary point on the plane.

A vector that starts at a point and ends at a point will have coordinates (x2-x1, y2-y1).

If P(x, y) is an arbitrary point, then the coordinates of the vector are: (x-x1, y - y1).

Point P will belong to the segment if:

Program geom3; Const _Eps: Real = 1e-3; (calculation precision) var x1,y1,x2,y2,x,y:real; Function RealEq(Const a, b:Real):Boolean; (strictly equal) begin RealEq:= Abs(a-b)<= _Eps End; {RealEq} Function RealMoreEq(Const a, b:Real):Boolean; {больше или равно} begin RealMoreEq:= a - b >= _EpsEnd; (RealMoreEq) Function EqPoint(x1,y1,x2,y2:real):Boolean; (Whether two points on the plane coincide) begin EqPoint:=RealEq(x1,x2)and RealEq(y1,y2) end; (EqPoint) Function AtOtres(x1,y1,x2,y2,x,y:real):Boolean; (Checking whether point P belongs to segment P1P2) Begin If EqPoint(x1,y1,x2,y2) Then AtOtres:= EqPoint(x1,y1,x,y) (points P1 and P2 coincide, the result is determined by the coincidence of points P1 and P) Else AtOtres:= RealEq((x-x1)*(y2-y1)- (y-y1)*(x2-x1),0)and (RealMoreEq(x,x1)and RealMoreEq(x2,x)Or RealMoreEq(x ,x2)and RealMoreEq(x1,x)) end; (AtOtres) begin (main) writeln(Enter point coordinates: x1,y1,x2,y2,x,y"); readln(x1,y1,x2,y2,x,y); if AtOtres(x1,y1,x2 ,y2,x,y) then writeln("Yes.") else writeln("No.); end. (main)

Program execution results.

Enter point coordinates: x1, y1, x2, y2, x,y
0.5 1 2.5 2.8 1.203 1.633
Yes.

Test results in the GeoGebra program:


Today we have written the AtOtres() function, which checks whether an arbitrary point belongs to a segment given by its coordinates.

Two more functions have been introduced: EqPoint() and RealMoreEq() to implement comparison operations on real data. The first checks whether two points on the plane coincide, the second is used to check the relation ">=".

In the next lesson, based on the previously written procedures, we will write a procedure for determining the coordinates of the point of intersection of two segments.

On this I say goodbye to you. See you at the next lesson.

Boolean variables typically get their values ​​from comparison and math operations (discussed in the previous lesson), as well as specific Boolean operations.

Turbo Pascal has logical operations that can be applied to variables of a boolean type. These are not, and, or and xor operations. In this topic, you will look at three logical operations. The designations and results of these operations are given in the table. Consider it.

Operation not(not) has one operand and forms its logical negation. The result of the not operation is False if the operand is true and True if the operand is false. So,

not True False (untruth is a lie)

not False True (non-false is true)

Operation result and(i) is true only if both of its operands are true, and false otherwise.

Operation result or(or) is true if either of its operands is true, and false only if both operands are false.

Exercise . Try to determine the meaning of boolean operations for statements:

  1. A schoolchild stays at home during the winter holidays or goes somewhere to rest.
  2. Philip Kirkorov is a singer and combine operator.
  3. Schoolchildren are boys and girls.

Logical, relational, and arithmetic operations often occur in the same expression. In this case, the relations to the left and right of the sign of the logical operation must be enclosed in brackets, since logical operations have a higher priority. In general, the following priority of operations is adopted:

  • and, *, /, div, mod
  • or, +, -
  • relation operations.

. The logical operation and is also called logical multiplication, and the logical operation or is also called logical addition.

In addition, the order of operations can be changed by parentheses. For example, in a logical expression, we arrange the order of actions

A or B and not (A or B)

The parenthesized or operator is executed first, followed by the not, and, or operators. If we substitute the values ​​True and False instead of variables A and B, then, using the already considered procedure, we get the value of the entire expression equal to True.

Exercise . Write in your notebook and calculate the values ​​of the expressions for a=10, b=20, c=true, d=false:

  • (a>5) and (b>5) and (a<20) and (b<30);
  • not (a<15) or not (b<30);
  • c or d and (b=20);

Attention! In Pascal, there is no way to enter boolean data using the read statement. However, the output of the values ​​of variables of a boolean type is provided using the write operator.

For example, after executing the write (5>2) statement, True will be displayed on the screen.

Independent work

Choose with the teacher a task for doing independent work from the exercises below.

  1. Calculate the values ​​of the expression:

    a) sqr(x)+sqr(y)<=4 при x=0.3, y=-1.6;

    b) k mod 7 = k div5-1 at k=15;

    c) odd(trunc(10*p)) at p=0.182;

    d) not odd(n) for n=0;

    e) t and (p mod 3=0) at t=true, p=101010;

    e) (x*y<>0) and (y>x) with x=2, y=1;

    g) (x*y<>0) or (y>x) when x=2, y=1;

    h) a or (not b) with a=False, b=True;

  2. Write a relation in Pascal that is true if the specified condition is met and false otherwise:

    a) integer k is divisible by 7;

    b) the point (x, y) lies outside the circle of radius R centered at the point (1,0);

    c) the natural number N is the square of the natural number;

    d) 0

    e) x=max(x,y,z);

    e) at least one of the logical variables a and b is True;

    g) both logical variables a and b are True.

  3. Specify the order in which operations are performed when evaluating expressions:

    a) a and b or not c and d;

    b) (x>=0) or not c and d.

  4. Evaluate the following expressions with a=True, b=False:

    a) a or b and not a;

    b) (a or b) and not a;

    c) not a and b;

    d) not (a and b)

  5. Write an expression in Pascal that is true if the specified condition is met and false otherwise:

    a) x belongs to the segment;

    b) x lies outside the segment;

    c) * x belongs to the segment or [-1, 1];

    d) * x lies outside the segments and [-1, 1];

    e) each of the numbers x, y, z is positive;

    g) none of the numbers x, y, z is positive;

    h) only one of the numbers x, y, z is positive;

    i) boolean variable a has the value True, boolean variable b has the value False;

    j) * a year with serial number y is a leap year (a year is a leap year if its number is a multiple of 4, but out of multiples of 100, only multiples of 400 are leap years, for example, 1700, 1800 and 1900 are non-leap years, 2000 is a leap year).

  6. Draw on the (x, y) plane an area in which and only in which the specified expression is true:

    a)* (y>=x) and (y+x>=0) and (y<=1);

    b) (sqr(x)+sqr(y)<1) or (y>0) and (abs(x)<=1);

    c) (trunc(y)=0 and (round(x)=0);

    d)* (abs(x)<=1)>(abs(y)>=1);

    e) (sqr(x)+sqr(y)<=4)=(y<=x).

  7. There is a conditional operator:
    if d<>10
    then
    writeln('hooray!')
    else
    writeln('bad...');

    Is it possible to replace it with the following statements:

  8. What will be the values ​​of the variables j, k after the execution of the conditional statement:
    if j>k
    then
    j:=k-2
    else
    dec(k,2);
    if the initial values ​​of the variables are equal:
    a) j=3, k=5;
    b) j=3, k=3;
    c) j=3, k=2.

Note . As a result of the statement dec (k,2), the value of the variable k is decreased by 2.

Choose with the teacher a task for doing independent work from the exercises below.

1. Calculate the values ​​of the expression:

a) sqr(x)+sqr(y)<=4 при x=0.3, y=-1.6;

b) k mod 7 = k div5-1 at k=15;

c) odd(trunc(10*p)) at p=0.182;

d) not odd(n) for n=0;

e) t and (p mod 3=0) at t=true, p=101010;

e) (x*y<>0) and (y>x) with x=2, y=1;

g) (x*y<>0) or (y>x) when x=2, y=1;

h) a or (not b) with a=False, b=True;

2. Write in Pascal a relation that is true if the specified condition is met and false otherwise:

a) integer k is divisible by 7;

b) the equation has no real roots;

c) the point (x, y) lies outside the circle of radius R centered at the point (1,0);

d) the natural number N is the square of the natural number;

e) x=max(x,y,z);

g) (do not use the not operation)

h) at least one of the logical variables a and b is True;

i) both logical variables a and b are True.

3. Specify the order in which operations are performed when evaluating expressions:

a) a and b or not c and d;

b) (x>=0) or not c and d.

4. Calculate the following expressions with a=True, b=False:

a) a or b and not a;

b) (a or b) and not a;

c) not a and b;

d) not (a and b)

5. Write an expression in Pascal that is true if the specified condition is met and false otherwise:

a) x belongs to the segment;

b) x lies outside the segment;

c) * x belongs to the segment or [-1, 1];

d) * x lies outside the segments and [-1, 1];

e) each of the numbers x, y, z is positive;

g) none of the numbers x, y, z is positive;

h) only one of the numbers x, y, z is positive;

i) boolean variable a has the value True, boolean variable b has the value False;

j) * a year with a serial number y is a leap year (a year is a leap year if its number is a multiple of 4, but out of multiples of 100, only multiples of 400 are leap years, for example, 1700, 1800 and 1900 are non-leap years, 2000 is a leap year).

6. Draw on the plane (x, y) the area in which and only in which the specified expression is true:

a)* (y>=x) and (y+x>=0) and (y<=1);

b) (sqr(x)+sqr(y)<1) or (y>0) and (abs(x)<=1);

c) (trunc(y)=0 and (round(x)=0);

d)* (abs(x)<=1)>(abs(y)>=1);

e) (sqr(x)+sqr(y)<=4)=(y<=x).

7) There is a conditional operator:

writeln('hooray!')

writeln('bad...')

Is it possible to replace it with the following statements:

a) if d=10 b) if not (d=10)

writeln ('hooray!') writeln ('hooray!')

writeln('bad...'); writeln('bad...');

c) if not (d=10) d) if not (d<>10)

writeln ('bad...') writeln ('bad...')

writeln('hooray!'); writeln('hooray!');

8) What will be the values ​​of the variables j, k after the execution of the conditional statement:

if the initial values ​​of the variables are equal:

Note. As a result of the statement dec (k,2), the value of the variable k is decreased by 2.

. X belongs to the segment ;
x lies outside the segment ;
x belongs to the segment or [-1,1];
x lies outside the segment or [-1,1].
3.
Explain the structure and rules for executing conditional statements.
4.
Write the specified action as a single conditional statement:
cos
2
x, at 0x at =
1-sin
2
x, otherwise.
5.
z
int z=0, x=1, y=

1;

80 Programming linear algorithms in the system C++Builder
if (x>0) if (y>0) z=1; else z=2;
6.
What value will the variable have z after executing the statements:
int z=0, x=

1,y=1;
if (x) ( if (y>0) z=1;) else z=2;
7.
What value will the variable have z after executing the statements:
int z=0, x=0, y=1;
if (x) ( if (y>0) z=1;) else z=2;
8.
What value will the variable have z after executing the statements:
int z=0, x=3, y=1;
if (x) ( if (y) z=1;) else z=2;
9.
Specify errors in the following statements:
if (1
elsex=0; y+= 1;
10.
int z=0, x=0, y=1;
if (!x) ( if (!(y

1)) z=1;) else z=2;
11.
What value will the variable z have after executing the statements:
int z=0, x=1, y=1;
if (!x) ( if (!(y

1)) z=1;) else z=2;
12.
Find errors in the program fragment:
(int n,x; switch (k) case + : x:=x-4 break; case

-

,

*

,x=5;
}
What rules are violated and how can errors be corrected?


81
3.4
Individual tasks for section 3
Each student needs to solve two problems of the first and second levels of complexity.
Tasks of the first level of complexity.
1.
You are given a four-digit positive integer. Write a program that prints true or false depending on whether the specified condition is met or not: the sum of the 1st and 4th digits is equal to the product of the 2nd and 3rd digits.
2.
Real numbers x, y are given. If x and y are negative, then replace each value with its modulus; if only one of them is negative, then increase both values ​​by 5.5; in other cases, both values ​​should be reduced by 10 times.
3.
Real numbers x, y are given. If x and y are positive, then decrease each value by a factor of 5; if both values ​​are in the range [-3, 9], then both values ​​should be replaced by 0; otherwise x and y increase by 3.
4.
Real numbers x, y (xy) are given. Replace the smaller of these two numbers with their half sum, and the larger one with their double product.
5.
If the sum of three pairwise distinct real numbers x, y, z is less than 1, then the smallest of these three numbers is replaced by the half-sum of the other two; otherwise, replace the smaller of x and y with half the sum of the two remaining values.
6.
Real numbers a, b, c, d are given. If a b>c>d, then leave the numbers unchanged; otherwise, replace all numbers with their squares.
7.
Determine if a six-digit integer is lucky. (A number is called lucky if the sum of its first three digits is equal to the sum of its last three digits.)
8.
true or false depending on whether the specified condition is satisfied or not: for arbitrary real numbers a, b, c, determine whether the equation ax
2
+bx+c=0 at least one real solution.

82 Programming linear algorithms in the system C++Builder
9.
To solve the following problem, write a program that prints true or false depending on whether the specified condition is met or not: to determine whether the sum of the first two digits of a given four-digit number is equal to the sum of its two last numbers.
10.
Some schools have the following admissions policies. Applicants take two exams, which are graded according to
100 points each. If the applicant scores at least 150 points, then this gives the right to enter the day department, from 100 to 149 - to the evening department; below 100 points means denial of admission to study. Write a program that, depending on the amount of points scored, informs the applicant of his right to enroll.
11.
Write a program using the selection operator , which allows you to get a verbal description of the marks. (1 - "bad", 2 -
"unsatisfactory", 3 - "satisfactory", 4 - "good", 5 -
"Great".)
12.
To solve the following problem, write a program that prints true or false depending on whether the specified condition is met or not: to determine whether among the digits of a given three-digit number are the same.
13.
Write a program that prints true if date d1, m1 precedes (within a year) the date d2, m2 and meaning false otherwise.
14.
Write a program using a select statement that displays the full name by the first letter of the name.
(Ivan, Peter, Nikolai, Vladimir, George).
15.
Write a program using the selection operator, which, given the input digit 0≤ k≤ 5 prints the name of this figure in Russian and English.
16.
Given an integer k (1

k

365). Determine whether the k-th day of the year will be a weekend or a workday, if January 1 is Monday.
17.
Given a real number x. Calculate f, if:











sin
,
1 0
,
0 5
2 11 2
cases
the rest
in
x
x
x
at
x
x
x
at
f

Programming with One-Dimensional Arrays
83 18.
Given a real number x. Write a program to calculate f:












34 5
,
12 3
lg ln
,
3 5
1 3
2 4
cases
the rest
in
x
x
x
at
x
x
x
at
f
19.
Write a program using a select operator that, by the first letter of the city name, displays the name of the city in full (Kursk, Moscow, Tula, Novgorod, Voronezh).
20.
Write a program using a select statement that displays the full name by the first letter of the last name (Ivanov, Petrov, Sidorov, Myshkin, Shishkin).
21.
Given the coordinates of the center and the radius of the circle. Determine whether a certain point with coordinates (x,y) belongs to the circle.
22.
Write a program using a select statement that will print the full name of the animal (dog, cat, giraffe, horse, monkey) given the first letter of the animal's name.
23.
Given three distinct numbers a,b,c. Sort these numbers in ascending order so that a corresponded to the smallest number, b- average number, c- the largest.
24.
Write a program using a select statement that, by number (from 1 to 7), displays the name of the day of the week.
25.
Write a program using a select statement that, by number (from 1 to 12), displays the name of the month of the year.
26.
Write a program using a select statement that, given the first letter of the name of the season of the year, prints the full name of the season of the year.
27.
You are given a five-digit positive integer. Write a program that prints true or false depending on whether the specified condition is met or not: the sum of the 1st, 3rd and 5th digits is greater than the product of the 2nd and 4th digits.
28.
You are given a four-digit positive integer. Write a program that prints true or false depending on whether

84 Programming linear algorithms in the system C++Builder whether or not the specified condition is met: arithmetic mean 1 and
4 digits is more than the sum of 2 and 3 digits.
29.
Given a digit and a three-digit number in decimal notation. If the digit matches the highest digit of the number, then calculate the sum of the digits of the number. If the digit matches the middle digit of the number, then calculate the product of the digits of the number. If the digit matches the least significant digit of the number, then calculate the arithmetic mean of the digits of the number. If none of the conditions is met, then calculate the geometric mean of the digits of the number.
30.
Given a digit and a three-digit number in decimal notation. If the digit matches the highest digit of the number, then calculate the arithmetic mean of the digits of the number. If the digit coincides with the middle digit of the number, then calculate the geometric mean of the digits of the number. If the digit matches the least significant digit of the number, then calculate the sum of the digits of the number. If none of the conditions is met, then calculate the product of the digits of the number
Tasks of the second level of complexity.
1.
Given a two-digit positive integer in the ternary number system. Write a program that displays the names of the digits of a number in Russian, if the highest digit of the number is greater than the youngest , otherwise
- in English.
2.
Given a two-digit positive integer in the five-digit number system. Write a program that displays the names of the digits of a number in English, if the highest digit of the number is greater than the lowest, otherwise
- in Russian.
3.
Given a two-digit positive integer in the five-digit number system. Write a program that prints the names of the digits of a number in English if the highest digit of the number is exactly 2 times the lowest digit, otherwise
- in Russian.
4.
Given a two-digit positive integer in the ternary number system. Write a program that displays the names of the digits of a number in Russian, if the highest digit of the number is less than the lowest, otherwise
- in English.

Programming with One-Dimensional Arrays
85 5.
Given a two-digit positive integer in the five-digit number system. Write a program that displays the names of the digits of a number in Russian.
6.
Given a two-digit positive integer in the octal number system. Write a program that displays the names of the digits of a number in Russian.
7.
Given a two-digit positive integer in the octal number system. Write a program that displays, if the highest digit is less than half the lowest digit, the names of the digits of the number in English, otherwise

in Russian.
8.
Given a two-digit positive integer in the septimal number system. Write a program that displays, if the digits are in descending order, the names of the digits of the number in English, otherwise

in Russian.
9.
You are given two digits in the decimal number system. Write a program that displays the names in English of the digits of the sum of these digits.
10.
You are given two digits in the decimal number system. Write a program that displays the names in Russian of the digits of the product of these digits.
11.
You are given two digits in the decimal number system. Write a program that displays the name in Russian of the digit of the modulus of the difference of these digits.
12.
You are given two digits in the decimal number system. Write a program that displays the names of the digits in English of the quotient and the remainder of the integer division of these numbers.
13.
Given a two-digit positive integer in the decimal number system. Write a program that displays, if the highest digit is three times the lowest digit, the names of the digits of the number in English, otherwise

in Russian.
14.
Given a two-digit positive integer in the decimal number system. Write a program that prints the name of the digit of a number that has a greater value in English, and the name of the digit of a number that has a smaller value,

in Russian.

86 Programming linear algorithms in the system C++Builder
15.
Given a two-digit positive integer in the septimal number system. Write a program that prints the name of the digit of a number that has a greater value in English, and the name of the digit of a number that has a smaller value,

in Russian.
16.
Given a two-digit positive integer in the hexadecimal number system. Write a program that displays the names of the digits of a number in English.
17.
Given two integer two-digit positive numbers in the ternary number system. Write a program that displays the names of the digits of the sum of these numbers in Russian.
18.
Two two-digit positive integers in the binary number system are given. Write a program that displays the names of the digits of the sum of these numbers in English.
19.
Given a two-digit positive integer in the five-digit number system. Write a program that displays, if the digits are in ascending order, the names of the digits of the number in English, otherwise

in Russian.
20.
Given a two-digit positive integer in the hexadecimal number system. Write a program that displays, if the highest digit is five times the lowest digit, the names of the digits of the number in Russian, otherwise

in English.
21.
Two digits are given in the octal number system. Write a program that displays the names in Russian of the digits of the modulus of the difference of these digits.
22.
Two digits are given in the octal number system. Write a program that displays the names in Russian of the digits of the sum of these two digits.
23.
Given a two-digit positive integer in the septimal number system. Write a program that displays, if the highest digit is three times the lowest, the names of the digits of the number in Russian, otherwise

in English.
24.
Given a two-digit positive integer in the octal number system. Write a program that displays the name of the digit of a number that has a greater value in Russian, and in

Programming with One-Dimensional Arrays
87 the rank of the digit of the number that has the lowest value,

in English.
25.
Given a two-digit positive integer in the septimal number system. Write a program that displays the name of the digits of a number that has a value less than 4 in Russian, and the name of the digits of the number that has a value greater than or equal to 4,

in English.
26.
Given a two-digit positive integer in the hexadecimal number system. Write a program that displays the name of the digits of a number that has a value less than 3 in Russian, and the name of the digits of a number that has a value greater than or equal to 3,

in English.
27.
Given the first two letters (case insensitive, both lowercase and uppercase) of the name of the month of the year. Write a program that displays the full name of the month of the year in Russian.
28.
Given the first two letters (case insensitive, both lowercase and uppercase) of the name of the day of the week. Write a program that displays the full name of the day of the week in Russian.
29.
In the old Japanese calendar, a 60-year cycle was adopted, consisting of five 12-year sub-cycles. Subcycles were designated by color names: green, red, yellow, white and black. Within each sub-cycle, the years were named after animals: rats, cows, tigers, hare, dragons, snakes, horses, sheep, monkeys, chickens, dogs and pigs (1984 - the year of the green rat - was the beginning of the next cycle). Write a program that enters the number of a certain year of our era and prints its name according to the old Japanese calendar.
30.
Given a two-digit positive integer in the decimal number system. Write a program that displays the name of the digits of a number that has a value greater than 5 in Russian, and the name of the digits of a number that has a value less than or equal to 5,

in English.

88 Programming linear algorithms in the system C++Builder
4 PROGRAMMING THE CYCLIC
ALGORITHMS IN THE C++BUILDER SYSTEM
In this section, the study of the language loop operators is carried out.
C++, acquiring skills in programming cyclic algorithms, introducing
Yes, compiling and debugging programs.
4.1
Basic concepts
On the tongue C++Builder There are three loop statements:
1.
Loop with precondition ( while);
2.
Loop with postcondition ( do);
3.
Loop with counter ( for).
Loop statement with precondition while (Figure 4.1):
while(condition) (loop body)
Figure 4.1

Scheme of the loop operator algorithm while
If the result of the condition evaluation is true(true), then the loop body is executed and the transition to the condition check is performed again. If the result of the condition evaluation is false(false), then the loop exits and jumps to the statement following the cyclic statement while.
If, before the first execution of the loop, the value of the expression was false, the loop body is not executed at all and the transition to the next statement occurs.
Condition
Loop body
Yes
Not

Programming with One-Dimensional Arrays
89
Example:
Calculate the sum of odd numbers between 1 and 10.
( int k=1, s=0; while (k ( s+=k; k+=2;
}
}
Loop statement with postcondition do (Figure 4.2):
do(loop body) while(condition); yes no condition
Loop body
Figure 4.2

Scheme of the loop operator algorithm do…while
The condition is an expression of a boolean type, the loop body is one simple or compound statement.
Statement loop body

Variables of a boolean type are described by an identifier Boolean . They can only take two values ​​- False (False) and True (true). They are also described in the variable declaration section. Var<имя>: Boolean;

Boolean expressions can include:

n booleans,

n relation operations (<- меньше, >- more,<=- меньше или равно, >=- greater than or equal,<>- not equal, = - equal).

n logical operations And, Or, Not

n diff. actions and functions


Turbo Pascal has logical operations that can be applied to variables of a boolean type. These are not, and, or and xor operations.

Operation not(not) has one operand and forms its logical negation. The result of the not operation is False if the operand is true and True if the operand is false. So, not True False (untrue is a lie) not False True (non-false is true).

Operation result and(i) is true only if both of its operands are true, and false in all other cases (boolean multiplication).

Operation result or(or) is true if either of its operands is true, and false only if both operands are false (logical addition).

Logical, relational, and arithmetic operations often occur in the same expression. In this case, the relations to the left and right of the sign of the logical operation must be enclosed in brackets, since logical operations have higher priority. In general, the following priority of operations is adopted:

2. and, *, /, div, mod

3. or, +, -

4. relation operations (<- меньше, >- more,<=- меньше или равно, >=- greater than or equal,<>- not equal, = - equal).

In addition, the order of operations can be changed by parentheses. For example, in a logical expression, we arrange the order of actions:

AorBandnot(AorB)


The parenthesized or operator is executed first, followed by the not, and, or operators. If we substitute the values ​​True and False instead of variables A and B, then, using the already considered procedure, we get the value of the entire expression equal to True.

Exercise 5: . Write in a notebook and calculate the values ​​of expressions

with a=10, b=20, c=true, d=false: a)(a>5) and (b>5) and (a<20) and (b<30);

b) not (a<15) or not (b<30);

c) c or d and (b=20).

Attention ! In Pascal, there is no way to enter boolean data using the read statement. However, the output of the values ​​of variables of a boolean type is provided using the write operator. for example, after executing the write (5>2) operator, True will be displayed on the screen.

Home questions and tasks:

1. How is a boolean variable described and what values ​​can it take?

2. What can be included in logical expressions. Give examples of simple logical expressions.

3. Tell us about logical operations in Pascal. Give examples of compound logical expressions.

4. What is the priority of different operations in Pascal. Give an example.


Tasks:

№19.

A. the integer k is divisible by 7;

C. At least one of the integers x, y is even;

G. x=max(x, y, z), that is, x is the largest of the three numbers x, y, z;

D. (don't use not operation)

E. at least one of the logical variables a and b is True;

G. Boolean variables a and b are both True.

№20. Specify the order in which operations are performed when evaluating expressions:

a) a and b or not c and d; b) (x>=0) or not c and d.

№21. Evaluate the following expressions with a=True, b=False:

a) a or b and not a; b)(a or b) and not a; in) not a and b; G) not (a and b)

№22. Make a program: At the entrance exams, the applicant passed physics, computer science, composition. An applicant will enter if he receives 5 in computer science and scores at least 13 points for three exams. Is it true that he entered (print True / False)?

§ 8. Solving problems on the topic “Linear programs. Boolean quantities."

Ex. 7 . Calculate the values ​​of the expression:

a) sqr(x)+sqr(y)<=4 при x=0.3, y=-1.6;

b) k mod 7 = k div5-1 at k=15;

c) odd(trunc(10*p)) at p=0.182;

d) not odd(n) for n=0;

e) t and (p mod 3=0) at t=true, p=101010;

e) (x*y<>0) and (y>x) with x=2, y=1;

g) (x*y<>0) or (y>x) when x=2, y=1;

h) a or (not b) with a=False, b=True;

Ex. eight. Write an expression in Pascal that is true if the specified condition is met and false otherwise:

a) x belongs to the segment;

b) x lies outside the segment;

c) * x belongs to the segment or [-1, 1];

d) * x lies outside the segments and [-1, 1];

e) each of the numbers x, y, z is positive;

g) none of the numbers x, y, z is positive;

h) only one of the numbers x, y, z is positive;

i) boolean variable a has the value True, boolean variable b has the value False;


j) * a year with a serial number y is a leap year (a year is a leap year if its number is a multiple of 4, but out of multiples of 100, only multiples of 400 are leap years, for example, 1700, 1800 and 1900 are non-leap years, 2000 is a leap year).

Ex. nine . Draw on the (x, y) plane an area in which and only in which the specified expression is true:

a)* (y>=x) and (y+x>=0) and (y<=1);

b) (sqr(x)+sqr(y)<1) or (y>0) and (abs(x)<=1);

c) (trunc(y)=0 and (round(x)=0);

d)* (abs(x)<=1)>(abs(y)>=1);

e) (sqr(x)+sqr(y)<=4)=(y<=x).

Ex. ten ..The book costs X rubles. The buyer has banknotes in denominations of 50, 10, 5, 1 rubles. How many and what banknotes do you need to take in order to pay for the book with the minimum number of banknotes?