Short circuit operation && and ||

First, the logical operator && (short circuit and)
features: as long as it encounters false or equivalent to false, it will short circuit, and as long as it is short circuit, it will not continue to be executed later. If there is a short circuit, the value that caused the short circuit is obtained. If there is no short circuit, the second value is obtained.

console.log( true && true ); // true
console.log( 123 && '中国'); // 中国
console.log( false && true ); // false
console.log( true && false); // false
console.log(1 && 0); // 0
console.log( undefined && 0); // undefined
console.log(null && 1); // null


Second, the logical operator || (short circuit or)
feature: as long as it encounters true or equivalent to true, it will be short circuited, and will not continue to execute as long as it is short circuited. If there is a short circuit, the value that caused the short circuit is obtained. If there is no short circuit, the second value is obtained.

console.log( true || true ); // true
console.log( 123 || '中国'); // 123
console.log( false || true ); // true
console.log( true || false); // true
console.log(1 || 0); // 1
console.log( undefined || 0); // 0
console.log(null || 1); // 1

 

Seeking  1+2+...+n , it is required not to use multiplication and division, for, while, if, else, switch, case and other keywords and condition judgment statements (A? B: C).

 

class Solution {
public:
    int sumNums(int n) {
        n && (n += sumNums(n - 1));
        return n;

    }
};

 

Guess you like

Origin www.cnblogs.com/lau1997/p/12676362.html