For and while loops in Blockchain / Solidity - Time & Space Complexity
When we use loops in blockchain code, the time it takes to run depends on how many times the loop repeats.
We want to know how the running time grows as the number of loop steps increases.
Analyze the time complexity of the following code snippet.
function processTransactions(transactions) {
for (let i = 0; i < transactions.length; i++) {
while (transactions[i].needsProcessing) {
process(transactions[i]);
transactions[i].needsProcessing = false;
}
}
}
This code goes through a list of transactions and processes each one if it needs processing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The outer
forloop runs once for each transaction. - Secondary operation: The inner
whileloop runs until the current transaction is processed, usually once per transaction. - Dominant operation: The
forloop over all transactions dominates because it scales with the number of transactions.
As the number of transactions grows, the total steps grow roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times processing steps |
| 100 | About 100 times processing steps |
| 1000 | About 1000 times processing steps |
Pattern observation: The work grows directly with the number of transactions.
Time Complexity: O(n)
This means if you double the number of transactions, the time to process roughly doubles.
[X] Wrong: "The inner while loop makes this code run much slower, like squared time."
[OK] Correct: The inner loop usually runs only once per transaction, so it does not multiply the total steps by the number of transactions.
Understanding how loops affect time helps you explain your code clearly and shows you can think about efficiency in blockchain programs.
"What if the inner while loop could run multiple times per transaction? How would the time complexity change?"
Practice
for loop in blockchain programming?Solution
Step 1: Understand the
Aforloop purposeforloop is designed to repeat code a set number of times, often counting through a range or list.Step 2: Compare with other loop types
Awhileloop repeats while a condition is true, not a fixed count. The other options do not describeforloops correctly.Final Answer:
It repeats code a fixed number of times. -> Option CQuick Check:
forloop = fixed repeats [OK]
- Confusing for loops with while loops
- Thinking for loops run only once
- Believing for loops stop the program
while loop in blockchain programming?Solution
Step 1: Identify correct syntax style
In blockchain programming languages like Solidity, thewhileloop uses parentheses for the condition and curly braces for the code block.Step 2: Check each option
while (condition) { // code } matches the correct syntax:while (condition) { // code }. Others miss parentheses or use incorrect punctuation.Final Answer:
while (condition) { // code } -> Option AQuick Check:
Correctwhilesyntax uses parentheses and braces [OK]
- Omitting parentheses around condition
- Using colon instead of braces
- Mixing syntax from other languages
uint sum = 0;
for (uint i = 1; i <= 3; i++) {
sum += i;
}
return sum;
Solution
Step 1: Analyze the loop iterations
The loop runs with i = 1, 2, 3. Each time, sum adds i: sum = 0+1, then 1+2, then 3+3.Step 2: Calculate the final sum
Sum after loop: 0+1=1, 1+2=3, 3+3=6.Final Answer:
6 -> Option AQuick Check:
1+2+3 = 6 [OK]
- Stopping loop early
- Adding wrong values
- Confusing sum initialization
uint i = 0;
while (i < 3)
i++;
Solution
Step 1: Check loop body syntax
Thewhileloop lacks braces {}, so only the next statement is inside the loop.Step 2: Understand impact of missing braces
Without braces, the loop runs only the single statementi++;. This is valid but can cause confusion or errors if more statements are added.Final Answer:
Missing braces {} for while loop body -> Option BQuick Check:
Always use braces for clarity in loops [OK]
- Assuming multiple statements run without braces
- Changing variable start unnecessarily
- Altering loop condition wrongly
for loop to do this in blockchain programming?A) for (uint i = 5; i > 0; i--) { arr.push(i); }
B) for (uint i = 1; i <= 5; i++) { arr.push(i); }
C) for (uint i = 5; i >= 1; i++) { arr.push(i); }
D) for (uint i = 5; i > 0; i++) { arr.push(i); }Solution
Step 1: Analyze each loop's counting direction
Counts down from 5 to 1 correctly counts down from 5 to 1 using i-- and condition i > 0. Counts up from 1 to 5 counts up. Options C and D increase i but have conditions that cause infinite loops.Step 2: Confirm correct loop behavior
Counts down from 5 to 1 correctly correctly pushes 5,4,3,2,1 into the array and stops when i reaches 0.Final Answer:
Counts down from 5 to 1 correctly -> Option DQuick Check:
Decrement i with i-- and condition i > 0 for countdown [OK]
- Using i++ when counting down
- Wrong loop condition causing infinite loops
- Confusing counting up vs down
Ready to prove it?
Pass the test and this pattern moves unknown
