The if Statement Solutions - D Programming Language
Description: D Programming Language
Keywords: 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.

# The if Statement Solutions

#### The if Statement Dersi Problem Çözümleri

1. The statement `writeln("Washing the plate")` is written indented as if to be within the `else` scope. However, because the scope of that `else` is not written with curly brackets, only the `writeln("Eating pie")` statement is actually inside the scope of that `else`.

   Since whitespaces are not important in D programs, the plate statement is actually an independent statement within `main()` and is executed unconditionally. It confuses the reader as well because of having been indented more than usual. If the plate statement must really be within the `else` scope, there must be curly brackets around that scope:

       import std.stdio;

       void main() {
           bool existsLemonade = true;

           if (existsLemonade) {
               writeln("Drinking lemonade");
               writeln("Washing the cup");

           } else {
               writeln("Eating pie");
               writeln("Washing the plate");
           }
       }

2. We can come up with more than one design for the conditions of this game. I will show two examples. In the first one, we apply the information directly from the exercise:

       import std.stdio;

       void main() {
           write("What is the value of the die? ");
           int die;
           readf(" %s", &die);

           if (die == 1) {
               writeln("You won");

           } else if (die == 2) {
               writeln("You won");

           } else if (die == 3) {
               writeln("You won");

           } else if (die == 4) {
               writeln("I won");

           } else if (die == 5) {
               writeln("I won");

           } else if (die == 6) {
               writeln("I won");

           } else {
               writeln("ERROR: ", die, " is invalid");
           }
       }

   Unfortunately, that program has many repetitions. We can achieve the same result by other designs. Here is one:

       import std.stdio;

       void main() {
           write("What is the value of the die? ");
           int die;
           readf(" %s", &die);

           if ((die == 1) || (die == 2) || (die == 3)) {
               writeln("You won");

           } else if ((die == 4) || (die == 5) || (die == 6)) {
               writeln("I won");

           } else {
               writeln("ERROR: ", die, " is invalid");
           }
       }

3. The previous designs cannot be used in this case. It is not practical to type 1000 different values in a program and expect them all be correct or readable. For that reason, it is better to determine whether the value of the die is within a range:

           if ((die >= 1) && (die <= 500))

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