Bird
Raised Fist0
Blockchain / Solidityprogramming~5 mins

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

Choose your learning style10 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.

      Practice

      (1/5)
      1. Which statement best describes a for loop in blockchain programming?
      easy
      A. It repeats code while a condition is true.
      B. It runs only once and then stops.
      C. It repeats code a fixed number of times.
      D. It stops the program immediately.

      Solution

      1. Step 1: Understand the for loop purpose

        A for loop is designed to repeat code a set number of times, often counting through a range or list.
      2. Step 2: Compare with other loop types

        A while loop repeats while a condition is true, not a fixed count. The other options do not describe for loops correctly.
      3. Final Answer:

        It repeats code a fixed number of times. -> Option C
      4. Quick Check:

        for loop = fixed repeats [OK]
      Hint: For loops count repeats; while loops check conditions [OK]
      Common Mistakes:
      • Confusing for loops with while loops
      • Thinking for loops run only once
      • Believing for loops stop the program
      2. Which of the following is the correct syntax for a while loop in blockchain programming?
      easy
      A. while (condition) { // code }
      B. while condition: // code
      C. while condition { // code }
      D. while (condition): // code

      Solution

      1. Step 1: Identify correct syntax style

        In blockchain programming languages like Solidity, the while loop uses parentheses for the condition and curly braces for the code block.
      2. Step 2: Check each option

        while (condition) { // code } matches the correct syntax: while (condition) { // code }. Others miss parentheses or use incorrect punctuation.
      3. Final Answer:

        while (condition) { // code } -> Option A
      4. Quick Check:

        Correct while syntax uses parentheses and braces [OK]
      Hint: Remember: while(condition) { code } uses parentheses and braces [OK]
      Common Mistakes:
      • Omitting parentheses around condition
      • Using colon instead of braces
      • Mixing syntax from other languages
      3. What will be the output of this code snippet?
      uint sum = 0;
      for (uint i = 1; i <= 3; i++) {
      sum += i;
      }
      return sum;
      medium
      A. 6
      B. 3
      C. 0
      D. Error

      Solution

      1. 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.
      2. Step 2: Calculate the final sum

        Sum after loop: 0+1=1, 1+2=3, 3+3=6.
      3. Final Answer:

        6 -> Option A
      4. Quick Check:

        1+2+3 = 6 [OK]
      Hint: Add numbers from 1 to 3 to get sum quickly [OK]
      Common Mistakes:
      • Stopping loop early
      • Adding wrong values
      • Confusing sum initialization
      4. Identify the error in this code snippet:
      uint i = 0;
      while (i < 3)
      i++;
      medium
      A. Condition should be i <= 3
      B. Missing braces {} for while loop body
      C. Variable i should start at 1
      D. Incorrect increment operator

      Solution

      1. Step 1: Check loop body syntax

        The while loop lacks braces {}, so only the next statement is inside the loop.
      2. Step 2: Understand impact of missing braces

        Without braces, the loop runs only the single statement i++;. This is valid but can cause confusion or errors if more statements are added.
      3. Final Answer:

        Missing braces {} for while loop body -> Option B
      4. Quick Check:

        Always use braces for clarity in loops [OK]
      Hint: Use braces {} to clearly define loop body [OK]
      Common Mistakes:
      • Assuming multiple statements run without braces
      • Changing variable start unnecessarily
      • Altering loop condition wrongly
      5. You want to create a loop that counts down from 5 to 1 and stores each number in an array. Which code snippet correctly uses a 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); }
      hard
      A. Infinite loop due to i++ with i > 0
      B. Counts up from 1 to 5
      C. Infinite loop due to i++ with i >= 1
      D. Counts down from 5 to 1 correctly

      Solution

      1. 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.
      2. 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.
      3. Final Answer:

        Counts down from 5 to 1 correctly -> Option D
      4. Quick Check:

        Decrement i with i-- and condition i > 0 for countdown [OK]
      Hint: Use i-- and i > 0 to count down safely [OK]
      Common Mistakes:
      • Using i++ when counting down
      • Wrong loop condition causing infinite loops
      • Confusing counting up vs down
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard