// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
/**
* @title whileLoop
* @dev Demonstrates the use of a while loop to iterate over a mapping in Solidity.
* The contract stores block numbers at different instances and provides a function
* to emit these stored values through an event.
*/
contract whileLoop {
// Mapping to store block numbers. The key is an incrementing counter, and the value is the block number.
mapping (uint => uint) blockNumber;
// Counter to keep track of the number of entries in the mapping.
uint counter;
// Event to emit the stored block number.
event uintNumber(uint);
/**
* @dev Stores the current block number in the mapping under the current value of `counter`.
* Increments `counter` after storing the block number.
*/
function setNumber() public {
blockNumber[counter++] = block.number;
}
/**
* @dev Emits the stored block numbers sequentially by iterating over the mapping
* with a while loop. It starts from the first stored block number and continues
* until it reaches the last stored block number based on the `counter`.
*/
function getNumbers() public {
uint i = 0; // Initialize the index for iteration.
// While loop to go through each stored block number.
while (i < counter) {
// Emit an event with the current block number.
emit uintNumber(blockNumber[i]);
i = i + 1; // Increment the index to move to the next block number.
}
}
}
//Deploy: