Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why doesn't C++ add extra = or! = to the conditional statement?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains why C++ should not add extra = or! in the conditional sentence, which interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "Why C++ does not add extra = or!" to the conditional statement.

ES.87: do not add extra = = or! = to conditional statements

Reason (reason)

Doing so avoids lengthy code and reduces the chances of some errors. Help improve the execution of the code and comply with habits.

Example (sample)

From the point of view of definition, the conditional judgment in if statement, while statement and for statement can get the result of true or false. The numeric value is compared with 0, and the pointer is compared with nullptr.

/ / These all mean "if `p` is not `nullptr`"

If (p) {...} / / good

If (p! = 0) {...} / / redundant `! = 0`; bad: don't use 0 for pointers

If (p! = nullptr) {...} / / redundant `! = nullptr`, not recommended

Usually, if (p) is read as a direct expression of the programmer's intention if p is legal, while if (p! = nullptr) is a lengthy expression.

Example (sample)

This rule is particularly useful when declaring the use as a condition.

If (auto pc = dynamic_cast (ps)) {...} / / execute if ps points to a kind of Circle, good

If (auto pc = dynamic_cast (ps); pc! = nullptr) {.} / / not recommendedExample (example)

Note that implicit conversions to bool are applied in conditions. For example:

Note that operations that can convert implicit types to Boolean types can be used in conditional statements. For example, S:

For (string s; cin > > s;) v.push_back (s)

This invokes istream's operator bool ().

This code makes use of the bool () operator of istream.

Note (Note)

Displaying integers and zeros is not usually verbose. The reason is that (unlike pointers and Boolean types) integers can usually express more than two meaningful values. In addition, 0 (zero) is usually used to indicate success. Therefore, it is best to compare integers as a special case.

Void f (int I)

{

If (I) / / suspect

/ /...

If (I = = success) / / possibly better

/ /...

}

Always remember that an integer can have more than two values.

Be sure to remember that integers can have more than two valid values.

Example, bad (negative example)

It has been noted that

It has been warned:

If (strcmp (p1, p2)) {.} / / are the two C-style strings equal? (mistake!)

Is a common beginners error. If you use C-style strings, you must know the functions well. Being verbose and writing

This is a common, beginner's mistake. If you use a C-style string, you must know the function. Keep it long and write

If (strcmp (p1, p2)! = 0) {.} / / are the two C-style strings equal? (mistake!)

Would not in itself save you.

It doesn't help.

Note (Note)

The opposite condition is most easily expressed using a negation:

Use! It is easier to express anti-logic:

/ / These all mean "if `p` is `nullptr`"

If (! P) {...} / / good

If (p = = 0) {...} / / redundant `= = 0`; bad: don't use `0` for pointers

If (p = = nullptr) {...} / / redundant `= = nullptr`, not recommendedEnforcement (implementation recommendation)

Easy, just check for redundant use of! = and = = in conditions.

Easy, you just need to check the redundant! = and = = in the conditional statement.

At this point, I believe that everyone has a better understanding of "why C++ should not add extra = or!" to the conditional statement, you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report