Logical Expressions - D Programming Language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Discord (community-run)
  - Forums
  - IRC
  - Wiki
  - Issues
  - Contributing
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# Logical Expressions

The actual work that a program performs is accomplished by expressions. Any part of a program that produces a value or a side effect is called an expression. It has a very wide definition because even a constant value like `42` and a string like `"hello"` are expressions, since they produce the respective constant values 42 and "hello".

Note: Don't confuse producing a value with defining a variable. Values need not be associated with variables.

Function calls like `writeln` are expressions as well because they have side effects. In the case of `writeln`, the effect is on the output stream by the placement of characters on it. Another example from the programs that we have written so far would be the assignment operation, which affects the variable that is on its left-hand side.

Because of producing values, expressions can take part in other expressions. This allows us to form more complex expressions from simpler ones. For example, assuming that there is a function named `currentTemperature` that produces the value of the current air temperature, the value that it produces may directly be used in a `writeln` expression:

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

That line consists of four expressions:

1. `"It's "`
2. `currentTemperature()`
3. `" degrees at the moment."`
4. The `writeln()` expression that makes use of the other three

In this chapter we will cover the particular type of expression that is used in conditional statements.

Before going further though, I would like to repeat the assignment operator once more, this time emphasizing the two expressions that appear on its left and right sides: the assignment operator (`=`) assigns the value of the expression on its right-hand side to the expression on its left-hand side (e.g. to a variable).

        temperature = 23      // temperature's value becomes 23

##### Logical Expressions

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

Logical expressions can have one of only two values: `false` that indicates falsity, and `true` that indicates truth.

I will use `writeln` expressions in the following examples. If a line has `true` printed at the end, it will mean that what is printed on the line is true. Similarly, `false` will mean that what is on the line is false. For example, if the output of a program is the following,

    There is coffee: true

then it will mean that "there is coffee". Similarly,

    There is coffee: false

will mean that "there is no coffee". I use the "... is ...: false" construct to mean "is not" or "is false".

Logical expressions are used extensively in conditional statements, loops, function parameters, etc. It is essential to understand how they work. Luckily, logical expressions are easy to explain and use.

The logical operators that are used in logical expressions are the following:

- The `==` operator answers the question "is equal to?". It compares the expression on its left side to the one on its right side and produces `true` if they are equal and `false` if they are not. By definition, the value that `==` produces is a logical expression.

  As an example, let's assume that we have the following two variables:

          int daysInWeek = 7;
          int monthsInYear = 12;

  The following are two logical expressions that use those values:

          daysInWeek == 7           // true
          monthsInYear == 11        // false

- The `!=` operator answers the question "is not equal to?". It compares the two expressions on its sides and produces the opposite of `==`.

          daysInWeek != 7           // false
          monthsInYear != 11        // true

- The `||` operator means "or", and produces `true` if any one of the logical expressions is true.

  If the value of the left-hand expression is `true`, it produces `true` without even looking at the expression that is on the right-hand side. If the left-hand side is `false`, then it produces the value of the right-hand side. This operator is similar to the "or" in English: if the left one, the right one, or both are `true`, then it produces `true`.

  The following table presents all of the possible values for both sides of this operator and its result:

  Left expression | Operator | Right expression | Result
  --- | --- | --- | ---
  false | || | false | false
  false | || | true | true
  true | || | false (not evaluated) | true
  true | || | true (not evaluated) | true

      import std.stdio;

      void main() {
          // false means "no", true means "yes"

          bool existsCoffee = false;
          bool existsTea = true;

          writeln("There is warm drink: ",
                  existsCoffee || existsTea);
      }

  Because at least one of the two expressions is `true`, the logical expression above produces `true`.

- The `&&` operator means "and", and produces `true` if both of the expressions are true.

  If the value of the left-hand expression is `false`, it produces `false` without even looking at the expression that is on the right-hand side. If the left-hand side is `true`, then it produces the value of the right-hand side. This operator is similar to the "and" in English: if the left value and the right value are `true`, then it produces `true`.

  Left expression | Operator | Right expression | Result
  --- | --- | --- | ---
  false | && | false (not evaluated) | false
  false | && | true (not evaluated) | false
  true | && | false | false
  true | && | true | true

          writeln("I will drink coffee: ",
                  wantToDrinkCoffee && existsCoffee);

  Note: The fact that the `||` and `&&` operators may not evaluate the right-hand expression is called their short-circuit behavior . The ternary operator `?:`, which we will see in a later chapter, is similar in that it never evaluates one of its three expressions. All of the other operators always evaluate and use all of their expressions.

- The `^` operator answers the question "is one or the other but not both?". This operator produces `true` if only one expression is `true`, but not both.

  Warning: In reality, this operator is not a logical operator but an arithmetic one. It behaves like a logical operator only if both of the expressions are `bool`.

  Left expression | Operator | Right expression | Result
  --- | --- | --- | ---
  false | ^ | false | false
  false | ^ | true | true
  true | ^ | false | true
  true | ^ | true | false

  For example, the logic that represents my playing chess if only one of my two friends shows up can be coded like this:

          writeln("I will play chess: ", jimShowedUp ^ bobShowedUp);

- The `<` operator answers the question "is less than?" (or "does come before in sort order?").

          writeln("We beat: ", theirScore < ourScore);

- The `>` operator answers the question "is greater than?" (or "does come after in sort order?").

          writeln("They beat: ", theirScore > ourScore);

- The `<=` operator answers the question "is less than or equal to?" (or "does come before or the same in sort order?"). This operator is the opposite of the `>` operator.

          writeln("We were not beaten: ", theirScore <= ourScore);

- The `>=` operator answers the question "is greater than or equal to?" (or "does come after or the same in sort order?"). This operator is the opposite of the `<` operator.

          writeln("We did not beat: ", theirScore >= ourScore);

- The `!` operator means "the opposite of". Different from the other logical operators, it takes just one expression and produces `true` if that expression is `false`, and `false` if that expression is `true`.

          writeln("I will walk: ", !existsBicycle);

##### Grouping expressions

The order in which the expressions are evaluated can be specified by using parentheses to group them. When parenthesized expressions appear in more complex expressions, the parenthesized expressions are evaluated before they can be used in the expressions that they appear in. For example, the expression "if there is coffee or tea, and also cookie or scone; then I am happy" can be coded as follows:

    writeln("I am happy: ",
    (existsCoffee || existsTea) && (existsCookie || existsScone));

If the sub expressions were not parenthesized, the expressions would be evaluated according to operator precedence rules of D (which have been inherited from the C language). Since in these rules `&&` has a higher precedence than `||`, writing the expression without parentheses would not be evaluated as intended:

    writeln("I am happy: ",
    existsCoffee || existsTea && existsCookie || existsScone);

The `&&` operator would be evaluated first and the whole expression would be the semantic equivalent of the following expression:

    writeln("I am happy: ",
    existsCoffee || (existsTea && existsCookie) || existsScone);

That has a totally different meaning: "if there is coffee, or tea and cookie, or scone; then I am happy".

Almost nobody can memorize all of the operator precedence rules. For that reason, for code correctness and clarity, it would be helpful to use parentheses even when not necessary.

The operator precedence table will be presented later in the book.

##### Reading `bool` input

All of the `bool` values above are automatically printed as `"false"` or `"true"`. It is the same in the opposite direction: `readf()` automatically converts strings `"false"` and `"true"` to `bool` values `false` and `true`, respectively. It accepts any combination of lower and uppercase letters as well. For example, `"False"` and `"FALSE"` are converted to `false` and `"True"` and `"TRUE"` are converted to `true`.

Note that this is the case only when reading into `bool` variables. Otherwise, the input would be read as-is without conversion when reading into a `string` variable. (As we will see later in the strings chapter, one must use `readln()` when reading strings.)

##### Problemler

1. We've seen above that the `<` and the `>` operators are used to determine whether a value is less than or greater than another value; but there is no operator that answers the question "is between?" to determine whether a value is between two other values.

   Let's assume that a programmer has written the following code to determine whether `value` is between 10 and 20. Observe that the program cannot be compiled as written:

       import std.stdio;

       void main() {
           int value = 15;

           writeln("Is between: ",
                   10 < value < 20);        // ← derleme HATASI
       }

   Try using parentheses around the whole expression:

           writeln("Is between: ",
                   (10 < value < 20));      // ← derleme HATASI

   Observe that it still cannot be compiled.

2. While searching for a solution to this problem, the same programmer discovers that the following use of parentheses now enables the code to be compiled:

           writeln("Is between: ",
                   (10 < value) < 20);      // ← compiles but WRONG

   Observe that the program now works as expected and prints "true". Unfortunately, that output is misleading because the program has a bug. To see the effect of that bug, replace 15 with a value greater than 20:

           int value = 21;

   Observe that the program still prints "true" even though 21 is not less than 20.

   Hint: Remember that the type of a logical expression is `bool`. It shouldn't make sense whether a `bool` value is less than 20. The reason it compiles is due to the compiler converting the boolean expression to a 1 or 0, and then evaluating that against 20 to see if it is less.

3. The logical expression that answers the question "is between?" must instead be coded like this: "is greater than the lower value and less than the upper value?".

   Change the expression in the program according to that logic and observe that it now prints "true" as expected. Additionally, test that the logical expression works correctly for other values as well: for example, when `value` is 50 or 1, the program should print "false"; and when it is 12, the program should print "true".

4. Let's assume that we can go to the beach when one of the following conditions is true:
   - If the distance to the beach is less than 10 miles and there is a bicycle for everyone
   - If there is fewer than 6 of us, and we have a car, and at least one of us has a driver license

   As written, the following program always prints "true". Construct a logical expression that will print "true" when one of the conditions above is true. (When trying the program, enter "false" or "true" for questions that start with "Is there a".).

       import std.stdio;

       void main() {
           write("How many are we? ");
           int personCount;
           readf(" %s", &personCount);

           write("How many bicycles are there? ");
           int bicycleCount;
           readf(" %s", &bicycleCount);

           write("What is the distance to the beach? ");
           int distance;
           readf(" %s", &distance);

           write("Is there a car? ");
           bool existsCar;
           readf(" %s", &existsCar);

           write("Is there a driver license? ");
           bool existsLicense;
           readf(" %s", &existsLicense);

           /* Replace the 'true' below with a logical expression that
            * produces the value 'true' when one of the conditions
            * listed in the question is satisfied: */
           writeln("We are going to the beach: ", true);
       }

   Enter various values and test that the logical expression that you wrote works correctly.

... çözümler

Copyright © 1999-2026 by the D Language Foundation | Page generated by Ddoc on Mon Aug 17 18:57:13 2026