C++Builder Learn How To Use Booleans In C++

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Learn How To Use Booleans In C++
By Yilmaz Yoru March 26, 2021

In programming, there are some of the parameters which have two values, as same as 0 and 1 bits in our computers. For these 1 and 0; Yes and No, On and Off, true and false, Enabled or Disabled, etc.. variables there are Boolean operands. We use Booleans, these kinds of switches to check most of the parameters, components, variables in classes, etc. bool data type is used C++ for these variables and it can take the values 1 (true) or 0 (false)

Here is an example to use Booleans;
C++:
#include <iostream>
#include <string>
 
int main(int argc, char** argv)
{
   bool parameter;
  
   parameter = true;
   std::cout << isCodingFun;  // Output is 1 , means true
 
   parameter = false;
   std::cout << isFishTasty;  // Outputs is 0 ,  means false
 
   return 0;
}
Boolean expressions are used in comparison and it is a C++ expression that returns a boolean value 1 (true) or 0 (false). We can check a boolean variable if it is true or false like this,
C++:
bool parameter=true;
if ( parameter ) std:cout << "parameter is true";
We can use comparison operators with if clauses, such as the equal to (==) operator to find out if an expression (or a variable) is true, also that means they are equal.
C++:
int a=5, b=5;
 
if ( a==b ) std:cout << "a is equal to b\n";
Here a==b term is a Boolean and return true or false, if clause checks if it is true or false and if it is true that means it is equal to. Same here, we can use greater than or lower than as below,
C++:
int a=5, b=7;
 
if ( a>b ) std:cout << "a is greater than b\n";
if ( a<b ) std:cout << "a is lower than b\n";