3.4 Boolean Type
In our daily life, we need to use a variable of type int to represent bus 216; A variable of type float is required to represent 3.5 yuan a pound of tomato, and sometimes it needs to represent a data, which is the logical state:
"Have you passed the C + + exam this time?" ”
"Does he really love me?" ”
The "over-and-over" and "love-not-love" are all representations of a logical judgment. Compared with the bus line and the price of tomato which we used to represent in the numerical data type, there are many different values, and the logic judgment state has the particularity of "either or". For exam status, either "over" or "not", both must be selected. Because of this, in C + +, we also use a special data type--Boolean type--to express this "either" or "logical judgment State." Variables of the Boolean type can only be given a value of TRUE or FALSE, respectively, to indicate the true and false of the logic, to be and not. The descriptor for the Boolean type is "bool". The C + + standard does not specify the length of the Boolean type data, and the Boolean type in Visual C + + is 1 bytes. For example:
// Boolean variable bPass, indicating whether the exam passed
// Assign a value of true to indicate that the exam passed
bool bPass = true;
Numeric type data such as int are used primarily for calculations, and Boolean types are used primarily to preserve the results of logical judgments, or for conditional structures or looping structures (described in detail in the 4th chapter) to control the execution of a program. For example:
cout << "Please enter your score:" << endl;
// an int variable that holds the input score
int nScore = 0;
// enter the score
cin >> nScore;
// Save the passed or failed bool type variable
// The default state is false, indicating that it failed
bool bPass = false;
// Use conditional structure for logical judgment
// determine if the input score is greater than or equal to 60
if (nScore> = 60)
{
// Save the result of logical judgment
// If the input score is greater than or equal to 60, the assignment is true,
// means the exam passed, otherwise it will keep its initial value of false, which means it failed
bPass = true;
}
// In the conditional structure, depending on the value of bPass,
// Control the execution path of the program
if (bPass)
{
// If bPass is true, the output test passes
cout << "Congratulations, you passed the exam" << endl;
}
else
{
// If bPass is false, the output test fails
cout << "Sorry, you did not pass the exam" << endl;
}
In this code, bpass the variable of type bool, first to save the result of the logical judgment, and to record the logical judgment result that the input score Nscore is greater than or equal to 60. If Nscore is greater than or equal to 60,bpass, it is assigned true, otherwise it retains its initial value of false, indicating that Nscore does not have a value greater than or equal to 60. Thus, a variable of type bool of Bpass saves the result of the logical judgment of whether the nscore is greater than or equal to 60.
Using the BOOL type variable to save the result of logical judgment is not only to save, but also to control the program execution flow in the condition or loop structure. In the subsequent if condition structure, the Bpass is also used as the basis for conditional judgment, if the value of Bpass is true, the prompt to pass the test is output, otherwise, the output of the test failed. In this way, our program has a different execution path depending on the value of the BOOL type variable Bpass.
Best practice: Eliminate implicit conversions of type bool and integral type
Although the bool type variable has only two logical values, true and false, the value of the BOOL type variable is implicitly converted to an integer number when it is used where a numeric arithmetic value is required. If the value of the bool type variable is false, then the value of the participating operation after the conversion is 0, and vice versa is 1. Conversely, if we assign a numeric value (integer or decimal) to a variable of type bool, an implicit conversion will also occur from the numeric data to the value of the bool type. The value 0 is converted to false, and any other value other than 0 is converted to true. For example:
bool a = 4; // 4 is converted to true, the value of a is true
int b = a; // a is converted to 1, b is 1
int c = a + b; // a is converted to 1 to participate in the operation, and the value of c is 2
This conversion happens to be very covert, and some compilers don't even give a warning message. Once the code's behavior is very subtle, it means that it is likely to hide some of the errors that are not easily detectable. So we should try to avoid assigning values to variables of type bool, or using variables of type bool for arithmetic operations to prevent this "sneaky" trick from happening.
3.5 characters and string types
"What's your license plate number?" ”
"Shaanxi A-82103"
We know that programming languages are essentially used to abstract, describe, and express the real world. In the face of the real world of various numerical data (for example, the bus route 216, indicating the tomato price of 3.5 yuan a pound), we can use the numerical data type defined previously described variables (int nno,float fprice) to abstract and express. In addition, there is another type of data in the real world-text data, such as the license plate number in the example above, "Shan A-82103" is a literal data. To express this type of literal data, C + + provides two data types, character types and string types, that are designed to abstract and express literal data. Where the character type is used to express a single character, such as ' a ', ' 0 ', and so on, while multiple characters are concatenated together to form a string, we use the string type to express.
3.5.1 character types
When we learn English, we always start from ABC single letter, and then we can concatenate together to form a complete sentence. To express a more complex literal data in C + + with strings, we also have to start with a single character that makes up a string. In C + +, we use character types to abstract and express a single common character whose type specifier is "char". For example:
// Define the character type variable cA and assign it with the character constant ‘A’
char cA = ‘A’; // The cA variable represents the uppercase alphabetic character ‘A’
char cB;
cB = ‘-’; // cB variable represents the symbol character ‘-’
// output character 'A'
cout << cA << endl;
Although a variable of a character type always appears as a character (assignment and output), in essence, it can actually be thought of as an integer data type that takes up less memory space and has a smaller range of values. It occupies only 1 bytes of memory space, and the corresponding value range is reduced to -128~127. To save memory resources, we can use the char type to express when we need to express a smaller integer value. As with integer data types, character types can also be decorated with the signed/unsigned keyword to form an unsigned character type. Therefore, the char type can also participate in arithmetic operations. For example:
// Assign the ASCII value corresponding to the capital letter character 'A' to the character type variable c
// equivalent to char c = ‘A’;
char c = 65;
// The variable t represents the invisible control character '\ t', which means outputting a Tab control
char t = ‘\ t’;
// loop output 26 uppercase alphabetic characters
for (int i = 0; i <26; ++ i)
{
// print the character represented by c
cout << c << t;
// add 1 to the character type variable to make it the next character in the ASCII table
c = c + 1;
}
Know more: Use wchar_t to denote Chinese characters
Because the char character type has a limited range of values, it can only be used to represent a limited number of characters in the Anscii character table, including common alphabetic characters and control characters. If you want to represent a larger range of characters, such as the ' Shan ' character in the example above, Char will appear beyond. To compensate for the lack of char types, C + + provides another character data type, wchar_t, which takes up 2 bytes of memory space and has a wider range of values, so it can represent a wider range of characters and, of course, a Chinese character. For example, you can output a Chinese character in the following way. However, it is important to note that because we have extended Chinese characters in our code, our code files are required to be saved using the UTF-8 encoding format.
// define a wchar_t type character variable
// and assign a value to it with a Chinese character
wchar_t cChs = L‘ Zeng ’;
// Set the area of wcout output object and output Chinese characters
wcout.imbue (locale ("chs"));
wcout << cChs << endl;
It is worth reminding that the L prefix in front of a character constant or string constant means that characters or strings here are encoded with the Wide-character character set (the wide character set, usually the Unicode character set). If the L prefix is not used, it is encoded using the Multibyte-character character set (multibyte character set). So, if we are going to initialize or assign a wide character (wchar_t) or wide string (wstring) variable with a character or string constant, we should prefix the character or string constant with an L. Conversely, if you initialize or assign a variable of type char and string, you do not need to add an L prefix.
Hello, C + + (10) Did you pass the C + + exam this time? Boolean data type representing logical judgments in C + +