📜 ⬆️ ⬇️

Textbook on the programming language D. Part 5

The fifth part of the translation is D Programming Language Tutorial by Ali Çehreli . In this part the chapter Logical Expressions is translated. Chapter material is designed for beginners.
  1. Part 1
  2. Part 2
  3. Part 3
  4. Part 4


Boolean expressions


The actual work that the program performs is in expressions. Any part of a program that creates a value or a side effect is called an expression. It is defined by a broad concept, because even a constant value, say 42, and a string value, for example “hello”, are expressions, since they create these corresponding constants 42 and “hello”.

Important: Do not confuse creating a value with a variable declaration. Values ​​do not need to bind to variables.
')
Function calls, such as writeln , are expressions, by the way, because they have side effects. In the case of writeln , this effect is superimposed on the standard output stream by printing characters on it. Another example of the programs that we wrote earlier may be an assignment operation, which changes the variable that is on the left.

Because of the creation of values, expressions may participate in other expressions. This allows us to form more complex expressions from simpler ones. For example, suppose that there is a function called currentTemperature , which returns the value of the current air temperature, this value that it produces can be directly used in the writeln expression:

writeln("It's ", currentTemperature()," degrees at the moment."); 

This line contains the following:
  1. “It's”
  2. currentTemperature ()
  3. "degrees at the moment.
  4. writeln () is an expression that uses the previous three.

In this chapter, we will discuss the specific types of expressions that are used in conditional statements.

Before going further I would like to repeat the assignment statement again, now emphasizing these two expressions that are on the left and right of it. The assignment operator (=) assigns the value of the specified expression to the right of the expression to the left (for example, a variable).
 temperature = 23 // temperature's value becomes 23 


Boolean expressions


Logical expressions are expressions that are used in Boolean algebra. Logical expressions are what computer programs do, making decisions like "if the answer is yes, I will save the file."

Boolean expressions can have one of two values: false , meaning false, and true , meaning true.

I will use writeln expressions in the following examples. If the line prints true at the end, it will mean that what is printed on this line is true. Similarly, false will mean that what is printed is false. For example, if this program output is:
There is coffee: true
it will mean "there is coffee." Similarly:
There is coffee: false
would mean "there is no coffee." Please note that in reality the “is” located on the left does not mean that there is coffee. I use the construct "... is ...: false" to mean "this is not so" or "this is false." Logical expressions are actively used in predicates, cycles, function arguments, etc. It is important to understand how they work. Fortunately, logical expressions are very simple to explain and easy to use.

Below are the logical operators that are used in logical expressions:

Expression grouping


The order in which these expressions are evaluated can be specified using parentheses. They can also be grouped. When expressions in brackets fall into more complex expressions, the values ​​of these expressions are calculated before they are used in the expressions in which they fall. For example, this expression “if there is coffee or tea, as well as a cookie or a bun, then I am happy” can be programmed in approximately the following way:
 writeln("I am happy: ", (existsCoffee || existsTea) && (existsCookie || existsScone)); 
If these nested expressions were not enclosed in parentheses, then they will be executed taking into account the operators' priorities according to rules D (which were inherited from the C language). Since the conjunction operator && priority is higher than that of the disjunction operator || , writing an expression without parentheses will not be calculated as expected.
 writeln("I am happy: ", existsCoffee || existsTea && existsCookie || existsScone); 
The conjunction operator && will execute first, and the entire expression will be semantically equivalent to the following expression:
 writeln("I am happy: ", existsCoffee || (existsTea && existsCookie) || existsScone); 
This has a different meaning: “If there is coffee or tea with cookies or a bun, then I'm happy.”


Reading bool type input


All bool values ​​above are printed as “false” or “true”. But this does not work in the opposite direction: these lines are “false” and “true” are not automatically read as values false and true . For this reason, this input must be read as strings and then converted to values ​​of type bool .

Since in one of the exercises below you need to enter “false” and “true”, I have to use the features of D, which I have not explained to you yet. I have described below a method that converts the specified string input data to bool data. This method will solve this problem by running to , which is declared in the std.conv module. (You may see a ConvException error if you type anything other than "false" or "true.")

I hope that all parts of the code that are in main () in the following programs are understandable at this stage. read_bool () is the method in which there are new, for you, language features. Although I put in comments for what he does, you can ignore this method. Still, it must be in the program code for compilation and correct operation.

Exercises


… decision
  1. Since the compiler takes 10 <value already as an expression, it expects a comma after it to accept it as an argument to writeln . Using parentheses around the whole expression will not work, because at this time the closing bracket will be expected in the same expression.
  2. Grouping this expression as (10 <value) <20 will remove the compile error, because in this case the first part will be calculated and then its result will be compared with <20

    We know that the value of a logical expression, such as 10 <value , will be false or true . false and true take the values ​​0 and 1, respectively, in integer expressions. We will look at automatic type conversion in the following chapters. As a result, the whole expression will be equivalent to either 0 <20 or 1 <20 , which both return true .
  3. The expression "greater than the minimum value and less than the maximum" can be programmed as follows:
     writeln("Is between: ", (value > 10) && (value < 20)); 
  4. “Are there bikes for everyone” can be programmed as a personCount <= bicycleCount or so bicycleCount> = personCount . The rest of this logical expression can be immediately transferred to the program code from the exercise:
     writeln("We are going to the beach: ", ((distance < 10) && (bicycleCount >= personCount)) || ((personCount <= 5) && existsCar && existsLicense) ); 
    Note the location of this disjunction operator (||) makes reading easier by separating these two basic predicates.

Source: https://habr.com/ru/post/244525/


All Articles