Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

If-else statements in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
If-else Mastery in Blockchain
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple if-else in Solidity
What is the output of this Solidity function when value = 10?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    function checkValue(uint value) public pure returns (string memory) {
        if (value > 10) {
            return "Greater";
        } else if (value == 10) {
            return "Equal";
        } else {
            return "Less";
        }
    }
}
A"Less"
B"Greater"
C"Equal"
DCompilation error
Attempts:
2 left
💡 Hint
Check the condition that matches exactly 10.
❓ Predict Output
intermediate
2:00remaining
If-else behavior with boolean in smart contract
What will the function return when flag = false?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract FlagCheck {
    function checkFlag(bool flag) public pure returns (string memory) {
        if (flag) {
            return "True";
        } else {
            return "False";
        }
    }
}
A"False"
BCompilation error
CRuntime error
D"True"
Attempts:
2 left
💡 Hint
Think about what happens when the flag is false.
❓ Predict Output
advanced
2:30remaining
Nested if-else output in Solidity
What does this function return when score = 75?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract ScoreCheck {
    function grade(uint score) public pure returns (string memory) {
        if (score >= 90) {
            return "A";
        } else {
            if (score >= 80) {
                return "B";
            } else {
                if (score >= 70) {
                    return "C";
                } else {
                    return "F";
                }
            }
        }
    }
}
A"A"
B"F"
C"B"
D"C"
Attempts:
2 left
💡 Hint
Check each condition from top to bottom.
🧠 Conceptual
advanced
2:30remaining
Error type from missing else in Solidity if-else
What error will this Solidity code produce when compiled?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    function check(uint x) public pure returns (string memory) {
        if (x > 5)
            return "High";
        else
            return "Low";
    }
}
ASyntaxError: 'else' without 'if'
BNo error, returns "Low" for x <= 5
CTypeError: incompatible types
DRuntime error: unreachable code
Attempts:
2 left
💡 Hint
Look at the placement of else after a return statement.
❓ Predict Output
expert
3:00remaining
Output of complex if-else with multiple conditions
What is the output of this Solidity function when value = 15?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Complex {
    function test(uint value) public pure returns (string memory) {
        if (value < 10) {
            return "Less than 10";
        } else if (value < 20) {
            if (value % 2 == 0) {
                return "Even and less than 20";
            } else {
                return "Odd and less than 20";
            }
        } else {
            return "20 or more";
        }
    }
}
A"Odd and less than 20"
B"Even and less than 20"
C"20 or more"
D"Less than 10"
Attempts:
2 left
💡 Hint
Check if 15 is less than 20 and if it is even or odd.

Practice

(1/5)
1.

What does an if-else statement do in a blockchain smart contract?

easy
A. It chooses between two actions based on a condition.
B. It repeats a set of instructions multiple times.
C. It stores data permanently on the blockchain.
D. It creates a new blockchain network.

Solution

  1. Step 1: Understand the role of if-else

    An if-else statement checks a condition and decides which code to run next.
  2. Step 2: Apply to blockchain context

    In smart contracts, it helps decide actions like approving transactions or rejecting them.
  3. Final Answer:

    It chooses between two actions based on a condition. -> Option A
  4. Quick Check:

    If-else = choose action [OK]
Hint: If-else picks one path out of two based on condition [OK]
Common Mistakes:
  • Confusing if-else with loops
  • Thinking if-else stores data
  • Believing if-else creates blockchains
2.

Which of the following is the correct syntax for an if-else statement in Solidity?

uint balance = 100;
easy
A. if (balance > 50) then { /* code */ } else { /* code */ }
B. if balance > 50 then { /* code */ } else { /* code */ }
C. if balance > 50: { /* code */ } else: { /* code */ }
D. if (balance > 50) { /* code */ } else { /* code */ }

Solution

  1. Step 1: Recall Solidity if-else syntax

    Solidity uses parentheses around conditions and braces for code blocks without 'then'.
  2. Step 2: Compare options

    Only if (balance > 50) { /* code */ } else { /* code */ } matches Solidity syntax: if (condition) { } else { }.
  3. Final Answer:

    if (balance > 50) { /* code */ } else { /* code */ } -> Option D
  4. Quick Check:

    Solidity if-else = parentheses + braces [OK]
Hint: Solidity if-else uses parentheses and braces, no 'then' [OK]
Common Mistakes:
  • Using 'then' keyword
  • Missing parentheses around condition
  • Using colons instead of braces
3.

What will be the output of this Solidity function when amount = 150?

function checkAmount(uint amount) public pure returns (string memory) {
  if (amount > 100) {
    return "Large amount";
  } else {
    return "Small amount";
  }
}
medium
A. Compilation error
B. "Small amount"
C. "Large amount"
D. No output

Solution

  1. Step 1: Check the condition with amount = 150

    Since 150 > 100, the if condition is true.
  2. Step 2: Determine which return runs

    The function returns "Large amount" because the if branch runs.
  3. Final Answer:

    "Large amount" -> Option C
  4. Quick Check:

    150 > 100 -> "Large amount" [OK]
Hint: Check condition value to pick correct return [OK]
Common Mistakes:
  • Confusing greater than with less than
  • Assuming else runs when if is true
  • Expecting syntax errors
4.

Find the error in this Solidity snippet:

function checkBalance(uint balance) public pure returns (string memory) {
  if balance > 0 {
    return "Positive";
  } else {
    return "Zero or Negative";
  }
}
medium
A. Missing parentheses around the if condition
B. Missing semicolon after return statements
C. Incorrect return type
D. Missing else keyword

Solution

  1. Step 1: Check if statement syntax

    In Solidity, the condition must be inside parentheses: if (condition).
  2. Step 2: Identify the error

    The code has if balance > 0 without parentheses, causing a syntax error.
  3. Final Answer:

    Missing parentheses around the if condition -> Option A
  4. Quick Check:

    if condition needs parentheses [OK]
Hint: Always put condition inside parentheses in if [OK]
Common Mistakes:
  • Omitting parentheses in if condition
  • Thinking semicolons are missing after return
  • Confusing return type errors
5.

You want to write a Solidity function that returns "Access granted" only if userRole is exactly "admin", otherwise returns "Access denied". Which code snippet correctly uses if-else to do this?

hard
A. if (userRole = "admin") { return "Access granted"; } else { return "Access denied"; }
B. if (userRole == "admin") { return "Access granted"; } else { return "Access denied"; }
C. if userRole == "admin" { return "Access granted"; } else { return "Access denied"; }
D. if (userRole.equals("admin")) { return "Access granted"; } else { return "Access denied"; }

Solution

  1. Step 1: Understand string comparison in Solidity

    Solidity uses == to compare strings for equality, not = or methods like equals().
  2. Step 2: Check syntax correctness

    if (userRole == "admin") { return "Access granted"; } else { return "Access denied"; } uses == with parentheses and braces correctly.
  3. Final Answer:

    if (userRole == "admin") { return "Access granted"; } else { return "Access denied"; } -> Option B
  4. Quick Check:

    Use == for string comparison in if [OK]
Hint: Use == to compare strings in Solidity if conditions [OK]
Common Mistakes:
  • Using single = instead of ==
  • Omitting parentheses in if
  • Using Java-style equals() method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard