c 语言bool 类型数据_C ++中的bool数据类型
c 語言bool 類型數據
In C++ programming language, to deal with the Boolean values – C++ added the feature of the bool data type. A bool variable stores either true (1) or false (0) values.
在C ++編程語言中,為了處理布爾值– C ++添加了bool數據類型的功能 。 布爾變量存儲true ( 1 )或false ( 0 )值。
Note that, In C++, true and false are the inbuilt keywords and they represent 1 and 0 respectively.
請注意,在C ++中, true和false是內置關鍵字,它們分別表示1和0。
So, whenever we need to work with such variables in which we have to store only two values i.e. the variable to hold status like, ON/OFF, YES/NO, TRUE/FALSE, etc we can use bool type variable.
因此,每當需要使用這樣的變量時,我們只需要存儲兩個值即可,即要保持狀態的變量,例如ON / OFF,YES / NO,TRUE / FALSE等,我們可以使用bool類型變量 。
Syntax:
句法:
bool variable_name;Example 1:
范例1:
#include <iostream> using namespace std;int main() {bool var1 = true;bool var2 = false;bool var3 = 1;bool var4 = 0;//printing the valuescout << "var1 : " << var1 << endl;cout << "var2 : " << var2 << endl;cout << "var3 : " << var3 << endl;cout << "var4 : " << var4 << endl;return 0; }Output:
輸出:
var1 : 1 var2 : 0 var3 : 1 var4 : 0Example 2:
范例2:
#include <iostream> using namespace std;int main() {bool status = true;if (status)cout << "It's true..." << endl;elsecout << "It's false..." << endl;status = false;if (status)cout << "It's true..." << endl;elsecout << "It's false..." << endl;return 0; }Output:
輸出:
It's true... It's false...Note: Any non-zero value considers as true and zero considers as false.
注意:任何非零值均視為true , 零則視為false 。
Example 3:
范例3:
#include <iostream> using namespace std;int main() {bool x = true;cout << "x : " << x << endl;x = -1;cout << "x : " << x << endl;x = -123.45f;cout << "x : " << x << endl;x = "Hello";cout << "x : " << x << endl;x = 123.456f;cout << "x : " << x << endl;x = 0;cout << "x : " << x << endl;x = NULL;cout << "x : " << x << endl;return 0; }Output:
輸出:
x : 1 x : 1 x : 1 x : 1 x : 1 x : 0 x : 0Also read: Use of bool in C language
另請閱讀: 在C語言中使用bool
翻譯自: https://www.includehelp.com/cpp-tutorial/bool-data-type-in-cpp.aspx
c 語言bool 類型數據
總結
以上是生活随笔為你收集整理的c 语言bool 类型数据_C ++中的bool数据类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大脑应对危机的模式_危机的完整形式是什么
- 下一篇: duration java_Java D