top of page
Explaining how variable bool work

In this lesson, we will explain how the comparison variable bool works, which compares two different variables

We can define the bool variable this way in the compiler
;bool older =ahmad>black
We put the variablebool Then we doYou name it then= The variable we want to compare thencomparison mark thenThe other variable we want to compare it against and then finalize it ;

 

Project: Who is the oldest

#include<iostream>//We start by placing the libraries
using namespace std;//We start by placing the libraries
int main(){//main function
int ahmed=25;//We define a variable for Ahmed's age
int soud=20;//We define a variable for soud's age
bool Equal= ahmed==soud;//We compare Ahmed and Saud if they are equal
bool NotEqual=ahmed!=soud;//We compare Ahmed and Saud in case they are not equal
bool Greater=ahmed>soud;//We compare Ahmed and Saud in the event that Ahmed is older than Saud
bool Less=ahmed<soud;//We compare Ahmed and Saud in the event that Ahmed is younger than Saud
bool LessOrEqual=ahmed<=soud;
//We compare Ahmed and Saud in the event that Ahmed is younger than or equal to Omar Saud
bool GreaterOrEqual=ahmed>=soud;
//We compare Ahmed and Saud in the event that Ahmed is greater than or equal to Omar Saud
cout<<"
="<<Equal<<endl;
cout<<"!="<<NotEqual<<endl;
cout<<">"<<Greater<<endl;
cout<<"<"<<Less<<endl;
cout<<"<="<<LessOrEqual<<endl;
cout<<">="<<GreaterOrEqual<<endl;
return 0;
}

output
=0
!=1
>1
<0
<=0
>=1

https://onlinegdb.com/gks4scAn2

We will see in the output that it only shows us the numbers 0 or one
Simply put, 0 means false and 1 means true

 && || Logical operators

There are two logical operators that compare two variables:
and : The translator's writing style is like this&&
In case And both variables must be true for the result to be correct
Only one wrong case is the answer wrong

true &&false= false
true &&true= true
or   :   His writing style is in ATranslated like this||
In case Or one of the two variables must be true for the result to be true

true 
|| false= true
false
|| false= false
NB:
1- It is not possible to compare three variables 
2- The comparison process must be between two variables only

Project: AndOr

#include<iostream>//We start by placing the libraries
using namespace std;//We start by placing the libraries
int main(){//main function
int ahmed=25;//We define a variable for Omar Ahmed
int sound=20;//We define a variable for Omar Saud
int jasem=25;//We define a variable for Jassim's age
cout<<(ahmed==soud)&&(ahmed==jasem);
// and&&only one of the two variables will be true, so the answer will be false if 
cout<<(ahmed>soud)||(ahmed==jasem);
//
or|| Only one of the variables will be true, so the answer will be true if 
return 0;
}

We will see in the output that only the numbers 0 or 1 are shown to us
Quite simply, 0 means false And 1 means correct

bottom of page