0
0
Blockchain / Solidityprogramming~10 mins

For and while loops in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - For and while loops
Start
Initialize loop variable
Check loop condition
Execute loop body
Update loop variable
Check loop condition
Loops start by setting a variable, then check a condition. If true, they run the loop body and update the variable, repeating until the condition is false.
Execution Sample
Blockchain / Solidity
for (uint i = 0; i < 3; i++) {
    emit Log(i);
}

uint j = 0;
while (j < 3) {
    emit Log(j);
    j++;
}
This code runs a for loop and a while loop, each emitting a log with numbers 0 to 2.
Execution Table
StepLoop TypeVariableConditionActionOutput
1fori=00 < 3 (true)emit Log(0)0
2fori=11 < 3 (true)emit Log(1)1
3fori=22 < 3 (true)emit Log(2)2
4fori=33 < 3 (false)exit loop
5whilej=00 < 3 (true)emit Log(0), j++0
6whilej=11 < 3 (true)emit Log(1), j++1
7whilej=22 < 3 (true)emit Log(2), j++2
8whilej=33 < 3 (false)exit loop
💡 Loops stop when the condition becomes false (i or j reaches 3).
Variable Tracker
VariableStartAfter 1After 2After 3Final
iundefined0123
j01233
Key Moments - 3 Insights
Why does the for loop stop when i equals 3?
Because the condition i < 3 becomes false at i=3, so the loop exits as shown in execution_table row 4.
Why do we update the variable inside the while loop body?
In the while loop, the variable j is increased inside the loop body to eventually make the condition false, shown in rows 5-7.
Are the outputs from for and while loops the same here?
Yes, both loops emit logs with 0, 1, and 2, as seen in the Output column of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i at step 3?
A2
B3
C1
D0
💡 Hint
Check the Variable column for i at step 3 in the execution_table.
At which step does the while loop condition become false?
AStep 7
BStep 8
CStep 6
DStep 5
💡 Hint
Look at the Condition column for the while loop in execution_table rows 5-8.
If we change the for loop condition to i < 2, how many times will it run?
A3 times
B1 time
C2 times
D0 times
💡 Hint
Refer to the condition i < 3 in the execution_table and imagine changing 3 to 2.
Concept Snapshot
For and while loops repeat code while a condition is true.
For loop: initialize; check condition; run body; update variable.
While loop: check condition; run body; update variable inside.
Loops stop when condition is false.
Use loops to repeat tasks easily.
Full Transcript
This lesson shows how for and while loops work in blockchain programming. Both loops start by checking a condition. The for loop initializes a variable, checks if it is less than 3, runs the loop body emitting a log, then increases the variable. This repeats until the variable reaches 3, then the loop stops. The while loop starts with a variable at 0, checks if it is less than 3, runs the loop body emitting a log and increasing the variable inside the loop. It repeats until the variable reaches 3, then stops. The execution table shows each step with variable values, conditions, actions, and outputs. The variable tracker shows how variables i and j change over time. Key moments explain why loops stop and how variables update. The quiz asks about variable values and loop behavior based on the tables. The snapshot summarizes the loop structure and use.