Recall & Review
beginner
What is a for loop used for?
A for loop repeats a block of code a set number of times, like counting steps in a recipe.
Click to reveal answer
beginner
What does a while loop do?
A while loop keeps running code as long as a condition is true, like waiting for a traffic light to turn green.
Click to reveal answer
intermediate
How do you stop a while loop from running forever?
You make sure the condition will become false at some point, like counting down to zero.
Click to reveal answer
intermediate
Give an example of a for loop in blockchain smart contract code.
In Solidity, a for loop can count through an array of addresses to check balances or permissions.
Click to reveal answer
advanced
Why should loops be used carefully in blockchain smart contracts?
Because loops can use a lot of gas (cost) if they run many times, making the contract expensive to use.
Click to reveal answer
What is the main difference between a for loop and a while loop?
✗ Incorrect
For loops repeat a fixed number of times, while loops continue as long as a condition is true.
In blockchain smart contracts, why is it risky to use loops that run many times?
✗ Incorrect
Loops that run many times use more gas, which can cause the contract to run out of gas and fail.
Which loop is best when you know exactly how many times to repeat an action?
✗ Incorrect
For loops are ideal when the number of repetitions is known beforehand.
What happens if a while loop's condition never becomes false?
✗ Incorrect
If the condition never becomes false, the while loop runs forever, causing an infinite loop.
In Solidity, which keyword is used to start a for loop?
✗ Incorrect
The keyword 'for' is used to start a for loop in Solidity.
Explain how a for loop works and give a simple example in blockchain code.
Think about repeating actions a fixed number of times.
You got /4 concepts.
Describe why while loops can be risky in blockchain smart contracts and how to avoid problems.
Consider what happens if the loop never stops.
You got /4 concepts.