0
0
Blockchain / Solidityprogramming~30 mins

For and while loops in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Counting Blocks with For and While Loops
📖 Scenario: Imagine you are working with a simple blockchain that stores blocks as numbers. You want to count and display blocks using loops.
🎯 Goal: Build a program that uses both for and while loops to count blocks in a blockchain.
📋 What You'll Learn
Create a list called blocks with the numbers 1 to 5
Create a variable called count and set it to 0
Use a for loop with variable block to iterate over blocks and increase count by 1 each time
Use a while loop to print numbers from 1 up to count
💡 Why This Matters
🌍 Real World
Counting blocks or transactions is common in blockchain programs to track progress or verify data.
💼 Career
Understanding loops is essential for blockchain developers to process data efficiently and automate tasks.
Progress0 / 4 steps
1
Create the blocks list
Create a list called blocks with the numbers 1, 2, 3, 4, and 5.
Blockchain / Solidity
Need a hint?

Use square brackets [] to create a list with the numbers inside separated by commas.

2
Create the count variable
Create a variable called count and set it to 0.
Blockchain / Solidity
Need a hint?

Just write count = 0 on a new line.

3
Use a for loop to count blocks
Use a for loop with variable block to iterate over blocks and increase count by 1 inside the loop.
Blockchain / Solidity
Need a hint?

Use for block in blocks: and inside the loop write count += 1.

4
Use a while loop to print numbers
Create a variable called i and set it to 1. Use a while loop to print i while it is less than or equal to count. Increase i by 1 each time inside the loop.
Blockchain / Solidity
Need a hint?

Start with i = 1. Use while i <= count: and inside print i and then increase i by 1.