CS4998: Blockchain Development Textbook
  • CS4998: Blockchain Development
  • Prerequisites
  • Introduction
    • Blockchain Theory
      • Bitcoin and the UTXO Model
      • Ethereum and the State-Based Model
    • Remix - A First Glance
    • Hello World!
      • Solidity File Structure
      • Primitive Values & Types
      • Contract Structure
      • Functions
      • Data Structures
      • Summary & Exercises
    • Hello World! Pt. 2
      • Control Flow
      • Interfaces and Inheritance
      • Constructors
      • Contract Interactions
      • Modifiers
      • Dynamic Arrays and Strings
        • Dynamic Arrays
        • Strings
      • Errors
      • Events
      • Units and Global Variables
      • Default Functions
  • Local Development
    • Node Providers
    • Interacting With On-Chain Contracts
    • Migrating to Foundry & VS Code
      • The Basics of Forge
      • Installing and Using Dependencies
      • Cast
      • Anvil
  • Understanding the EVM
    • The Ethereum Virtual Machine
      • A First Look at Computers
      • The Turing Machine
      • EVM Data Structures
      • Operation Codes (Opcodes)
      • Gas
      • Contract Compilation
      • Contract Runtime
    • Gas Optimizations
  • Yul & Advanced EVM Topics
    • Yul
    • Metamorphism
    • Bitwise Manipulations
  • Correctness
    • Security
    • Types of Testing
  • ERC Standards
    • Why ERCs?
    • ERC20
    • ERC721
    • ERC777
    • ERC1155
  • Frequently Used Smart Contracts
    • OpenZeppelin
    • Uniswap
    • Multisignature Contracts
    • AAVE/Compound
  • MEV & Advanced Blockchain Theory
    • Consensus Mechanisms vs Sybil Resistance Mechanisms
    • Maximal Extractable Value (MEV)
    • Looking Past The EVM
  • Etcetera
    • Developer Practices
    • Spring 2023 Past Resources
Powered by GitBook
On this page
  • If/Else If/Else Statements
  • While Loops
  • Do While Loops
  • For Loops
  • Aside: break and continue
  1. Introduction
  2. Hello World! Pt. 2

Control Flow

Logic is no longer sequential!

Control flow statements allow for our smart contracts to execute certain logic repeatedly or to execute only a certain block of logic.

If/Else If/Else Statements

The general structure for if/else if/else structures is as follows:

if (E) {
    // Insert logic here
} else if (E) {
    // Insert logic here
} else {
    // Insert logic here
}

where E is a boolean expression.

While Loops

The general structure for while loops is as follows:

while (E) {
    // Insert logic here
}

where E is a boolean expression.

Do While Loops

In contrast to while loops, do-while loops executes the logic contained within the loop body first before checking if the loop condition holds. As an example, consider the following psueodocode:

x <- 0
do
    x <- x + 1
while x < 0

After x is initialized to 0, we run one iteration of the do-while block and then check afterwards whether x < 0. Although the loop condition was never satisfied in the first place, we still end with x = 1.

In Solidity, do-while loops are as follows:

do {
    // Insert logic here
} while (E)

For Loops

Finally, we will cover for loops. The syntax for loop variables is as follows:

for (A; B; C) {
    // Insert logic here
}

where A is the loop variable, B is the loop condition, and C is the loop updater. An example of a for-loop in Solidity is as follows:

uint x = 0;
for (uint i = 0; i < 5; i++) {
    x += 1;
}

Using uint As The Loop Variable Type

Although not required, using uint as the loop variable type brings the advantage of being able to index arrays without having to do any sort of explicit type conversions.

Aside: break and continue

At times, we may not want for our loops to continue executing until termination. For this, Solidity provides us with the following keywords:

  • break: exit out of the loop completely

  • continue: move onto the next iteration

Examples of these keywords are as follows:

while (E) {
    if (E') {
        break;
    }
}

for (uint i = 0; i < 5; i++) {
    if (E) {
        continue;
    }
}

where E, E' are arbitrary boolean expressions.

PreviousHello World! Pt. 2NextInterfaces and Inheritance

Last updated 1 year ago