逻辑 AND 运算符和嵌套 if 语句有什么区别?

What's the difference between a Logical AND operator and a nested if statement?

我以为会有 none 但由于某些原因,当我使用逻辑 AND 而不是嵌套 if 语句时,我的程序无法运行。我制作了一个具有保持功能的俄罗斯方块克隆,当玩家按下 C 键时 holds/stores 一个块。我有一个布尔值可以防止玩家无法控制地交换方块。

出于某种原因,这有效:

if (bKey[5]) //When C key is pressed
        {
            if (!pieceHold) //boolean to prevent player from holding the C key and swapping blocks constantly/uncontrollably
            {
                if (!bPieceHeld) //check to see if player is not holding a block
                {
                    r++;
                    nHoldPiece = nCurrentPiece; //set empty hold piece into the current piece
                    nHoldRotation = nCurrentRotation; 
                    nCurrentPiece = nNextPiece; //set the current piece into the next piece
                    
                    nNextPiece = rng[r - 1];
                    nCurrentX = nFieldWidth / 2;
                    nCurrentY = 0;
                    bPieceHeld = 1;
                }
                else if (bPieceHeld) //if player is already holding block, swap held block with the current block
                {
                    int tempPiece = nCurrentPiece;
                    nCurrentPiece = nHoldPiece;
                    nHoldPiece = tempPiece;

                    int tempRotation = nCurrentRotation;
                    nCurrentRotation = nHoldRotation;
                    nHoldRotation = tempRotation;
                }
            }
            pieceHold = 1; //set boolean to true if C is already held to prevent accidental double swapping
        }
else
            pieceHold = 0; //set boolean false if C key is held

但这不是:

if (!pieceHold && bKey[5])
    {
        if (!bPieceHeld)
        {
                r++;
                nHoldPiece = nCurrentPiece;
                nHoldRotation = nCurrentRotation;
                nCurrentPiece = nNextPiece;
                
                nNextPiece = rng[r - 1];
                nCurrentX = nFieldWidth / 2;
                nCurrentY = 0;
                bPieceHeld = 1;
        }
        else if (bPieceHeld)
        {
                int tempPiece = nCurrentPiece;
                nCurrentPiece = nHoldPiece;
                nHoldPiece = tempPiece;

                int tempRotation = nCurrentRotation;
                nCurrentRotation = nHoldRotation;
                nHoldRotation = tempRotation;
        }
        pieceHold = 1;
    }
    else
        pieceHold = 0;

玩家仍然可以不受控制地交换块,尽管 bool pieceHold 据说可以防止这种情况发生。这是为什么?

这个嵌套如果:

if (bKey[5]) 
{
   if (!pieceHold) 
   {
      // ...
   }
}

相当于:

if (bKey[5] && !pieceHold)

在你的例子中,你交换了条件,这不等同于嵌套的 if。

注意条件等价于嵌套if,因为&&会short-circuit。这意味着,仅当 bKey[5] 为真时才评估 !pieceHold

第一个代码片段中的外部 if 语句包含 else 部分

if (bKey[5]) 
        {
            //...
            pieceHold = 1; //set boolean to true if C is already held to prevent accidental double swapping
        }
else
            pieceHold = 0; //set boolean false if C key is held

所以

 if (bKey[5]) 
 //...
 else
 //..

不一样
if (!pieceHold && bKey[5])
//...
else
//...

注意这个if-else语句

            if (!bPieceHeld) //check to see if player is not holding a block
            //..
            else if (bPieceHeld) 

可以像

一样更简单地重写
            if (!bPieceHeld) //check to see if player is not holding a block
            //..
            else

也不像

那样手动交换整数
            int tempPiece = nCurrentPiece;
            nCurrentPiece = nHoldPiece;
            nHoldPiece = tempPiece;

            int tempRotation = nCurrentRotation;
            nCurrentRotation = nHoldRotation;
            nHoldRotation = tempRotation;

你可以写

           std::swap( nCurrentPiece, nHoldPiece );
           std::swap( nCurrentRotation, nHoldRotation );

这使您的代码更加清晰易读。