5-2. Logical expression and short-circuit problem

  1. And: && (about two expressions are true only for true)

  2. Or: || (about at least one of the two expressions is true only for true)

  3. non:! (Equivalent to negate, to really make false non-operational, non-operational for false do is true)

  4. int m = 20;
      	int n = 30;
      
      	if (m == 20 && n == 30)
      	{
      		cout << "m == 10 , n == 30" << endl;
      	}
      	if (m == 20 || n == 40)
      	{
      		cout << "两个表达式至少一个为真" << endl;
      	}
      	if (!(m < 20))
      	{
      		cout << "!(m < 20)" << endl;
      	}
      	/*结果:
      
      	m == 10 , n == 30
      	两个表达式至少一个为真
      	!(m < 20)
      	*/
    
  5. Short-circuit problem

  • Mainly for logic and logical or
  • For a logical and &&: if the first expression is detected is false, then the rest of the expression will not be checked.
  • For a logical or ||: if the detected first expression is true, then the rest of the expression does not check.
  1. By short-circuiting problem to determine whether the function of a class is initialized, it is empty

    MyClass *myClass;
    ...
    //myClass.process()
    if(myClass != NULL && myClass.process())
    {
        ...
    }
    

Published 56 original articles · won praise 51 · views 6513

Guess you like

Origin blog.csdn.net/qq_43367829/article/details/105423007