The Ternary Operator ?: Solution - 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 Ternary Operator ?: Solution

#### The Ternary Operator `?:` Dersi Problem Çözümleri

Although it may make more sense to use an `if-else` statement in this exercise, the following program uses two `?:` operators:

    import std.stdio;

    void main() {
        write("Please enter the net amount: ");

        int amount;
        readf(" %s", &amount);

        writeln("$",
                amount < 0 ? -amount : amount,
                amount < 0 ? " lost" : " gained");
    }

The program prints "gained" even when the value is zero. Modify the program to print a message more appropriate for zero.

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