Logical expressions and logical operations. Boolean (true and false) values ​​in Perl What does true and false mean

In order for the program to be non-linear (i.e., depending on the situation, different instructions) programming languages ​​use logical expressions, the result of which can be either true (true) or false (false). The result of logical expressions is usually used to determine the execution path of a program.

Simple logical expressions are the result of relational operations between two operands (values). In the examples below, the operands are the values ​​of the variables x and y. Operands can be numbers, characters, or other data types. Everything that can be compared with each other. However, it is not recommended to compare real numbers due to the nature of their storage in computer memory.

Pascal provides the following relational operators:

  • less than: x< y
  • greater: x > y
  • equals: x = y
  • not equal to: x<>y
  • less than or equal to: x<= y
  • greater than or equal to: x >= y

Boolean types

The result of a logical expression is always a Boolean (logical) value. The Boolean data type can only take two values ​​(true or false). These values ​​are ordered as follows: false< true. Это значит, что данные булевого типа являются не только результатом операций отношения, но и могут выступать в роли операндов операции отношения. Также к ним можно применять функции ord, succ, pred, процедуры inc и dec.

A boolean value takes up 1 byte in memory.

In the example, six Boolean variables are assigned the values ​​of simple Boolean expressions. The values ​​stored in such variables are then displayed on the screen.

In addition to the boolean type, Pascal introduced three more Boolean types - bytebool (occupies 1 byte), wordbool (occupies 2 bytes) and longbool (occupies 4 bytes).
For all boolean types, false is 0, and true is any non-zero value. Boolean variables belonging to different Boolean types behave differently when operations are performed on them. Below is an example implemented in FreePascal (the result is displayed in the comments).

var b: boolean ; wb: wordbool; begin b: = false ; b: = pred(b) ; writeln(b, "", ord(b)); // TRUE 255 writeln (b= true ) ; // TRUE wb: = false ; wb:=pred(wb); writeln(wb, " ", ord(wb)); // TRUE -1 b: = true ; b:=succ(b) ; writeln(b, "", ord(b)); // TRUE 2 wb: = true ; wb:=succ(wb); writeln(wb, " ", ord(wb)); // FALSE 0 end .

Logical operations

You can use logical operators to form complex logical expressions. Logical operators are often used in relation to simple Boolean expressions.

The Pascal programming language provides the following logical operations:

true xor true = false
true or false = true
false or true = true
false xor false = false

  • Conjunction (logical multiplication, intersection) - and. The expression a and b evaluates to true only if a and b are true. In all other cases, the value of the expression a and b is false.

    true and true = true true and false = false false and true = false false and false = false

  • Disjunction (logical addition, union) – or. The expression a or b evaluates to false only if a and b evaluate to false. In all other cases the result is true.

    true or true = true true or false = true false or true = true false or false = false

  • Negation (inversion) – not. The expression not a has the opposite meaning of a.

    not true = false not false = true

  • Exclusive OR – xor. The expression a xor b evaluates to true only if only one of its operands evaluates to true.

The sequence of execution of logical operators: not, and, or.

In Pascal, the first steps are logical operators(and, or, xor, not), and only then relational operators (>, >=,<, <=, <>, =), so do not forget to place parentheses in complex logical expressions.

Complex Boolean expressions may not be processed completely if continued evaluation does not change the result. If a Boolean expression must be processed to completion, this is ensured by including the compilation directive (B+).

Standard Boolean Functions

  • odd(x) = true if x is odd (x is an integer type);
  • eoln(x) = true if the end of the line of the text file x is encountered;
  • eof(x) = true if end of file x is encountered.

In other cases, these functions take the value false.

Perl does not have a special boolean type, but the documentation often mentions that a function returns a "boolean" value. Sometimes it is simply written that the function returns true or false.

So where is the truth?

There is no special boolean type in Perl, but any scalar value when tested with if will return true or false. So you can write

If ($x eq "foo") ( )

If ($x) ( )

the first expression checks if the contents of the variable match $x with the string "foo" and the second will check if the value $x itself is true.

What values ​​are considered true or false in Perl?

It's pretty simple. Let's look at the documentation:

The number 0, the strings "0" and "", the empty list "()" and "undef" are considered false in boolean context. All other values ​​are considered true. Negating the true meaning with "!" or "not" returns a special false value. When used as a string it is considered "", and when used as a number it is considered 0.

From perlsyn, section "Truth and Falsehood".

Thus, the following scalar values ​​are considered false:

  • undef - undefined value
  • 0 is the number 0, even if you write it as 000 or 0.0
  • "" empty line.
  • "0" is a string containing the single number 0.

All other scalar values ​​are true, including these:

  • 1 any number other than zero
  • " " string with one or more spaces
  • "00" two or more 0's in a line
  • "0\n" 0 and line feed
  • "true"
  • "false" yes, even the string "false" is considered true.

I think this is because Larry Wall, the creator of Perl, has a generally positive view of the world. Apparently he believes that very few things in the world are truly bad and false, and most of them are true.

False, a. 1. Uttering falsehood; unbelievable; given to deceit; dishnest; as, a false witness.… … The Collaborative International Dictionary of English

false- adj 1: not genuine, authentic, or legitimate compare counterfeit 2 a: not true or correct; esp: intentionally or knowingly untrue or incorrectly injured by false accusations b: intended to mislead or deceive: decept … Law dictionary

false- W3S3 adj ▬▬▬▬▬▬▬ 1¦(untrue)¦ 2¦(wrong)¦ 3¦(not real)¦ 4¦(not sincere)¦ 5 false economy 6 under false pretences 7 false move/step 8 false imprisonment/arrest ▬▬▬▬▬▬▬ adj. false, falsest 1. not true; in error; incorrect; mistaken 2. untruthful; lying; dishonest 3. disloyal; unfaithful adjective ** 1.) not true: The report was dismissed as totally false. a false statement/claim/accusation ─ opposite TRUE 2.) made to look like something real: ARTIFICIAL: false eyelashes a) not real and intended to trick people: a… … Usage of the words and phrases in modern English

false- adjective 1. not true or real, but intended to look real in order to deceive people: false and misleading advertisements Firms issuing false certificates might be subject to lawsuits. 2. a false economy something that you... ... Financial and business terms

This article is about a Unix utility. An article about an esoteric programming language is called FALSE. false (translated from English as “false”) is a console command of UNIX compatible operating systems, the only action of which is to return the value 1, ... ... Wikipedia

False- is the antonym of the adjective true.False is the 2nd album of Gorefest, False (album).False may also refer to: * FALSE, an esoteric stack oriented programming language * false, a Unix utilityee also* Contradiction * Falsity * Lie * Falsework, a... ... Wikipedia

false- fȯls adj, fals·er; fals·est 1) not corresponding to truth or reality a test for HIV which gave false results 2) artificially made false teeth 3) of a kind related to or resembling another kind that is usu. designated by the... ... Medical dictionary

Books

  • False Delicacy, a Comedy, Kelly Hugh. The book is a reprint edition. Despite the fact that serious work has been done to restore the original quality of the publication, some pages may contain...
  • False Dmitri, a Russian romance and tragedy described by British eye-witnesses, 1604-1612, Howe Sonia E. The book is a reprint edition. Despite the fact that serious work has been done to restore the original quality of the publication, some pages may contain...

True-false [͵tru:ʹfɔ:ls] a log.

truthful; having truth values ​​"true" and "false"

true-false questions - “yes-no” questions


New large English-Russian dictionary. 2001 .

See what "true-false" is in other dictionaries:

    true-false- adjective offering a series of statements each of which is to be judged as true or false a true false test Ant: multiple choice * * * true false “TROO FLS”, adjective. having to do with or containing statements which must be marked as either… … Useful english dictionary

    true-false- adjective Allowing only the answers true or false. Somehow he got one true false question wrong out of twenty … Wiktionary

    true-false test- true′ false′ test n. edu a test requiring one to mark statements as either true or false Etymology: 1920–25 … From formal English to slang

    true-false test- ☆ true false test n. a test, as one typically given in school, consisting of a series of statements to be identified as either “true” or “false” … English World dictionary

    True/False Film Festival- Infobox Film Festival name = True/False Film Festival caption = opening = closing = awardsreceived = awardsgiven = films = directors = producers = writers = starring = host = Ragtag Programming for Film and Media Art date = number = 40 50… … Wikipedia

    true-false test- (ˈ) ̷ ̷ˈ ̷ ̷ noun: an objective test consisting of a series of statements each of which is to be marked as either true or false * * * /trooh fawls /, Educ. a test requiring one to mark statements as true or false. * * * n. a test… …Useful english dictionary

    true-false test- noun Date: 1924 a test consisting of a series of statements to be marked as true or false … New Collegiate Dictionary

    true-false test- /trooh fowls/, Educ. a test requiring one to mark statements as true or false. * * * …Universalium

    The True False Identity- Infobox Album | Name = The True False Identity Type = Album Artist = T Bone Burnett Released = 2006 Recorded = Genre = Rock Length = 59:30 Label = DMZ Producer = T Bone Burnett Reviews = * Allmusic Rating|3.5|5… … Wikipedia

    False- is the antonym of the adjective true.False is the 2nd album of Gorefest, False (album).False may also refer to: * FALSE, an esoteric stack oriented programming language * false, a Unix utilityee also* Contradiction * Falsity * Lie * Falsework, a... ... Wikipedia

    true-falsetest- true false test (tro͞oʹfôlsʹ) n. A test in which statements are to be marked either true or false. * * * …Universalium

Books

  • The true-blue laws of Connecticut and New Haven and the false blue-laws invented by the Rev. Samuel Peters to which are added specimens of the laws and... blue-laws of England in the reign of James I, Trumbull J Hammond. The book is a reprint edition. Despite the fact that serious work has been done to restore the original quality of the publication, some pages may contain...