Lesson 1 of 0
In Progress

Class-18

 

Class 18 – Blockchain & Smart Contract Basics

Class Slides: Class 18.1

This lecture explains events in Solidity, looping structures (for, while, and do-while loops), and control flow statements (break and continue). These concepts help in writing efficient smart contracts that interact with external applications and handle repetitive tasks dynamically.

Key Topics Covered:

1. Events in Solidity

  • Events allow smart contracts to communicate with external applications through the Ethereum Virtual Machine (EVM) logging system.
  • They store transaction logs on the blockchain, enabling external services (like dApps) to track contract activities.
  • Events cannot be accessed within contracts but are useful for monitoring state changes.

2. Declaring and Emitting Events

  • Events are declared using the event keyword and triggered using the emit keyword.
  • They are used to log important contract activities, such as value transfers, function calls, or contract updates.

3. Solidity Loops
Loops are used to execute a block of code repeatedly as long as a given condition holds true. Solidity supports the following loops:

  • For Loop: Executes a block of code for a fixed number of iterations.
  • While Loop: Repeats a code block while a condition remains true.
  • Do-While Loop: Executes a code block at least once before checking the condition.

4. Infinite Loops in Solidity

  • If a loop lacks a termination condition, it can become infinite, consuming all provided gas and causing the transaction to fail.
  • Solidity’s gas constraints prevent infinite loops from crashing the blockchain but may result in transaction failures.

5. Break and Continue Statements

  • Break: Exits the loop immediately when a condition is met.
  • Continue: Skips the current iteration and moves to the next one.

This lecture highlights the role of events in logging blockchain transactions and how Solidity loops control execution flow. It also discusses best practices for using break and continue statements to improve contract efficiency and avoid unnecessary gas consumption.

You cannot copy content of this page