Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Short-circuiting in conditions in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Short-circuiting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of short-circuit AND in Solidity
What is the output of this Solidity code snippet when executed?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    function check() public pure returns (string memory) {
        bool a = false;
        bool b = true;
        if (a && b) {
            return "Both true";
        } else {
            return "Short-circuited";
        }
    }
}
A"Short-circuited"
B"Both true"
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Remember that in AND conditions, if the first value is false, the second is not checked.
Predict Output
intermediate
2:00remaining
Output of short-circuit OR in Solidity
What will this Solidity function return when called?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    function check() public pure returns (string memory) {
        bool a = true;
        bool b = false;
        if (a || b) {
            return "Short-circuited OR";
        } else {
            return "Both false";
        }
    }
}
A"Short-circuited OR"
BCompilation error
C"Both false"
DRuntime error
Attempts:
2 left
💡 Hint
In OR conditions, if the first value is true, the second is not checked.
🔧 Debug
advanced
2:00remaining
Identify the error caused by short-circuiting in Solidity
What error will this Solidity code produce when the function is called?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    uint x = 0;
    function check() public returns (uint) {
        if (x != 0 && (10 / x) > 1) {
            return 1;
        } else {
            return 0;
        }
    }
}
AReturns 1 always
BNo error, returns 0
CCompilation error due to division
DDivision by zero runtime error
Attempts:
2 left
💡 Hint
Think about how short-circuiting prevents division by zero.
🧠 Conceptual
advanced
1:30remaining
Understanding short-circuit evaluation in smart contracts
Why is short-circuit evaluation important in smart contract conditions?
AIt disables all conditional checks
BIt always causes runtime errors to be ignored
CIt makes contracts run slower but safer
DIt reduces gas costs by avoiding unnecessary computations
Attempts:
2 left
💡 Hint
Think about how blockchain charges for computation.
Predict Output
expert
2:30remaining
Output of complex short-circuit condition in Solidity
What is the output of this Solidity function when called?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    uint public count = 0;
    function check() public returns (string memory) {
        if ((increment() > 0) || (increment() > 1)) {
            return "True";
        } else {
            return "False";
        }
    }
    function increment() internal returns (uint) {
        count += 1;
        return count;
    }
}
A"False" with count = 2
B"True" with count = 2
C"True" with count = 1
D"False" with count = 1
Attempts:
2 left
💡 Hint
Remember OR short-circuits after the first true condition.

Practice

(1/5)
1. What does short-circuiting mean in blockchain condition checks using && and ||?
easy
A. It stops checking further conditions once the result is decided.
B. It always checks all conditions regardless of the first result.
C. It reverses the order of condition checks.
D. It only works with numeric conditions.

Solution

  1. Step 1: Understand short-circuiting concept

    Short-circuiting means the program stops checking conditions as soon as the final result is known.
  2. Step 2: Apply to && and ||

    For &&, if the first condition is false, no need to check further. For ||, if the first condition is true, no need to check further.
  3. Final Answer:

    It stops checking further conditions once the result is decided. -> Option A
  4. Quick Check:

    Short-circuiting = Stops early [OK]
Hint: Stop checking when outcome is certain [OK]
Common Mistakes:
  • Thinking all conditions always run
  • Confusing && and || behavior
  • Assuming short-circuiting only applies to numbers
2. Which of the following is the correct syntax for short-circuit AND in a blockchain smart contract condition?
easy
A. if (condition1 | condition2) { execute(); }
B. if (condition1 & condition2) { execute(); }
C. if (condition1 || condition2) { execute(); }
D. if (condition1 && condition2) { execute(); }

Solution

  1. Step 1: Identify correct AND operator syntax

    The short-circuit AND operator is &&, which checks the second condition only if the first is true.
  2. Step 2: Compare options

    if (condition1 && condition2) { execute(); } uses &&, correct for short-circuit AND. Options B and D use single & or | which are bitwise operators, not short-circuit. if (condition1 || condition2) { execute(); } uses OR operator.
  3. Final Answer:

    if (condition1 && condition2) { execute(); } -> Option D
  4. Quick Check:

    Short-circuit AND = && [OK]
Hint: Use double ampersand for AND short-circuit [OK]
Common Mistakes:
  • Using single & instead of &&
  • Confusing AND (&&) with OR (||)
  • Using bitwise operators by mistake
3. Consider this blockchain code snippet:
bool a = false;
bool b = true;
if (a && (b = false)) { }
return b;

What is the returned value?
medium
A. false
B. Syntax error
C. true
D. Runtime error

Solution

  1. Step 1: Analyze short-circuit AND behavior

    Since a is false, the b = false part is not executed due to short-circuiting.
  2. Step 2: Determine final value of b

    b remains true because the assignment did not happen. So, return b; returns true.
  3. Final Answer:

    true -> Option C
  4. Quick Check:

    False && skips second = b stays true [OK]
Hint: False && skips right side, no assignment [OK]
Common Mistakes:
  • Assuming b becomes false anyway
  • Ignoring short-circuit effect
  • Thinking code throws error
4. Find the error in this blockchain condition:
if (isValid || checkStatus()) {
execute();
}

where checkStatus() is a function that must run always.
medium
A. Function checkStatus() may not run due to short-circuiting.
B. No error, code is correct.
C. Syntax error: missing semicolon.
D. Logical AND should be used instead of OR.

Solution

  1. Step 1: Understand short-circuit OR behavior

    If isValid is true, checkStatus() will not run because of short-circuiting.
  2. Step 2: Identify problem with function call

    Since checkStatus() must always run, using || causes it to sometimes skip, which is an error.
  3. Final Answer:

    Function checkStatus() may not run due to short-circuiting. -> Option A
  4. Quick Check:

    OR short-circuit skips right if left true [OK]
Hint: OR skips right if left true, function may not run [OK]
Common Mistakes:
  • Assuming function always runs
  • Confusing syntax error with logic error
  • Using AND instead of OR without reason
5. You want to check two blockchain conditions: isOwner and hasBalance. You must run logAccess() always, but only execute transfer() if both conditions are true. Which code correctly uses short-circuiting?
hard
A. if (isOwner && hasBalance) { logAccess(); transfer(); }
B. logAccess(); if (isOwner && hasBalance) { transfer(); }
C. if (isOwner & hasBalance) { logAccess(); transfer(); }
D. if (isOwner && hasBalance && logAccess()) { transfer(); }

Solution

  1. Step 1: Identify when logAccess() must run

    logAccess() must run always, so it should be called outside the condition.
  2. Step 2: Check condition for transfer()

    transfer() runs only if both isOwner and hasBalance are true, so use && inside the if.
  3. Step 3: Analyze options

    if (isOwner && hasBalance) { logAccess(); transfer(); } puts logAccess() inside the if, so it only runs if conditions true. if (isOwner && hasBalance && logAccess()) { transfer(); } calls logAccess() inside condition, so it may not run due to short-circuiting. if (isOwner & hasBalance) { logAccess(); transfer(); } uses single & which is bitwise, not short-circuit. logAccess(); if (isOwner && hasBalance) { transfer(); } calls logAccess() first always, then conditionally transfer().
  4. Final Answer:

    logAccess(); if (isOwner && hasBalance) { transfer(); } -> Option B
  5. Quick Check:

    Call always outside, conditionally inside [OK]
Hint: Call always-run functions outside conditions [OK]
Common Mistakes:
  • Putting always-run function inside condition
  • Using bitwise & instead of &&
  • Calling functions in wrong order