Min menu

Pages

VB

Logical operators in C++

 


What operators are used in setting logical conditions? (Logical Operatorsin C++

the name of the employeehis codeExampleExplanation of the code
AND&&(a && b)Are value  a and  b equal  true ?
Here, the two conditions must be met to return true
OR||(a || b)Is the value  a or  b or both equal  true ?
Here it is sufficient for one of the two conditions to be fulfilled to return true
NOT!!aIs the value  a not worth  true ?
If the answer is yes, it will return true


Explain the operators that are used to set Boolean conditions in C++

worker  && (AND Operator) in C++

The operator  && is used to execute a specific code if the first condition and the second condition are met.

  • That is, if the result of the first condition is equal  true and the result of the second condition is equal, the  true code will be executed.

  • If the result of both conditions is not equal  true , the code will not run.


The first example is to execute a certain code if the first condition and the second condition are met.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 10;
		  int b = 20;
	  
		  // equal to 20 the print command b will be executed equal to 10, and the value of the variable a The next condition means that if the value of the variable is
		  if( a == 10 && b == 20 )
		  {
		  cout << "The first and the second conditions return true";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    The first and the second conditions return true
		

We note that he executed the printing command because the answer to the two conditions in the sentence  if is true.


second example

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 10;
		  int b = 20;
	  
		  // equal to 50 the print command b will be executed equal to 10, and the value of the variable a the following condition means that if the value of the variable is
		  if( a == 10 && b == 50 )
		  {
		  cout << "The first and the second conditions return true";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                  

We note that the print command was not executed because the answer to the second condition in the sentence  if is false.



worker  || (OR Operator) in C++

An operator  || is used to execute a specific code if at least one of the conditions set is met.
So here it is sufficient for one of the two conditions to return the value  true in order for the placed orders to be executed.


The first example is to execute a specific code if at least one of the conditions is met

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 10;
		  int b = 20;
	  
		  // equal to 50 the print command b will be executed equal to 10, or the value of the variable a The following condition means that if the value of the variable
		  if( a == 10 || b == 50 )
		  {
		  cout << "One of the conditions return true";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    One of the conditions return true
		

Execute the print command because the answer to the first condition in the sentence  if is true.


second example

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 10;
		  int b = 20;
	  
		  // equal to 50 The print command b will be executed equal to 50, or the value of the variable a The following condition means that if the value of the variable
		  if( a == 50 || b == 50 )
		  {
		  cout << "One of the conditions return true";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                  

We note that the print command was not executed because the answer to the first and second conditions are included in the sentence if  is false.



worker  ! (NOT Operator) in C++

An operator  ! is used to execute a specific code if any condition that is set is not met.
That is, if one or all of the placed conditions return the value  false , the placed orders will be executed.


The first example of executing a given code if any condition that was set is not met.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 10;
	  
		  // not equal to 10 the print command a will be executed the following condition means that if the value of the variable is
		  if( !(a == 10) )
		  {
		  cout << "The condition return false";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                  

The print command was not executed because the answer to the condition is true.


second example

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 10;
	  
		  // not equal to 20 the print command a will be executed the following condition means that if the value of the variable is
		  if( !(a == 20) )
		  {
		  cout << "The condition return true";
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    The condition return true
		

Execute the print command because the answer to the condition is false.