0
0
Blockchain / Solidityprogramming~5 mins

For and while loops in Blockchain / Solidity - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AFor loops run forever; while loops run once.
BFor loops run a set number of times; while loops run until a condition is false.
CWhile loops run a set number of times; for loops run until a condition is false.
DThere is no difference.
In blockchain smart contracts, why is it risky to use loops that run many times?
AThey make the contract run faster.
BThey reduce the contract's security.
CThey can cause the contract to run out of gas and fail.
DThey automatically stop after 10 iterations.
Which loop is best when you know exactly how many times to repeat an action?
AFor loop
BIf statement
CWhile loop
DSwitch statement
What happens if a while loop's condition never becomes false?
AThe loop runs forever (infinite loop).
BThe loop runs once.
CThe program crashes immediately.
DThe loop stops automatically.
In Solidity, which keyword is used to start a for loop?
Arepeat
Bloop
Cwhile
Dfor
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.