Getting started with Boolean in c ++

Source: Internet
Author: User

Boolean objects can be granted true or false text values. The corresponding relationship is the concept of true or false!

We usually use it to determine whether a condition is true or false, for example, the following code:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;

Void main (void)
{

But some people with unclear concepts do not know that Boolean objects can also be considered as integer objects, but they cannot be declared as signed, unsigned, short long, if your life is (short bool found = false;), it will cause compilation errors.

The concept of Integer type is as follows:
When an expression requires an arithmetic value, a Boolean object is implicitly converted to an int type, that is, an integer object. false indicates 0, and true indicates 1. See the following code!

#include <iostream>
#include <string>
using namespace std;

void main(void)
{
bool found = true;
int a = 1;
cout << a + found << endl;
cin.get();
}


The expression style like a + found is true, and the output value is 2!

So many people will ask if the pointer is okay? The answer is yes. This concept is also effective for pointers. Let's look at an example of using an integer pointer object as a Boolean object:

# Include <iostream>
Using namespace std;

Void main (void)
{
Int a = 1;
Int * pi;
Pi = &;

If (* pi)
{
Cout <"* pi is true" <endl;
}
Cin. get ();
}


In the above Code, * pi is converted to a boolean type of implicit pattern to true or true.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.