If and Else
Part of the Fundamentals section of Coddy's Solidity journey. Lesson 18 of 53.
An if statement runs its block only when its condition is true. Add else to supply the alternative path. The condition goes in parentheses and each branch uses braces. Only one branch of this pair runs.
The statements in this excerpt run inside the provided main function.
uint256 stock = 8;
if (stock > 0) {
console.log("available");
} else {
console.log("empty");
}Stock is positive, so the first branch prints available. If stock were zero, the else branch would print empty. Branching turns a boolean decision into different actions.
Exactly one branch of an if/else pair runs.
Challenge
EasyPrint open when remaining is greater than zero; otherwise print full.
The tests pass arguments to main in this order: uint256 remaining. Each test starts with fresh contract state.
Use console.log for the requested output. Print only the requested values, one per line, with no extra labels unless the task specifies them.
Try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "forge-std/console.sol";
contract Main {
function main(uint256 remaining) external view {
// Write your solution here.
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
Practice on your own: Solidity playground