Cover photo

Head First Solidity - 4 - Control Flow

In programming, control flow refers to the order in which statements are executed or evaluated. In Solidity, control flow statements are used to control the execution of the program.

There are several types of control flow statements in Solidity, including if-else statements, for loops, while loops, and do-while loops. Let's take a closer look at each of these statements.

If-else statement

An if-else statement is used to execute a block of code if a condition is true, and a different block of code if the condition is false. Here's the basic syntax for an if-else statement in Solidity:

if (condition) {
  // code to execute if condition is true
} else {
  // code to execute if condition is false
}

Here's an example of an if-else statement in action:

function checkNumber(uint num) public pure returns (string memory) {
  if (num > 10) {
    return "The number is greater than 10";
  } else {
    return "The number is less than or equal to 10";
  }
}

For loop

A for loop is used to execute a block of code repeatedly for a fixed number of times. Here's the basic syntax for a for loop in Solidity:

for (initialization; condition; increment) {
  // code to execute repeatedly
}

Here's an example of a for loop in action:

function sum(uint[] memory numbers) public pure returns (uint) {
  uint total = 0;
  for (uint i = 0; i < numbers.length; i++) {
    total += numbers[i];
  }
  return total;
}

While loop

A while loop is used to execute a block of code repeatedly as long as a condition is true. Here's the basic syntax for a while loop in Solidity:

while (condition) {
  // code to execute repeatedly
}

Here's an example of a while loop in action:

function countdown(uint start) public pure returns (uint[] memory) {
  uint[] memory arr = new uintUnsupported embed;
  uint i = start;
  while (i > 0) {
    arr[start - i] = i;
    i--;
  }
  return arr;
}

Do-while loop

A do-while loop is used to execute a block of code repeatedly at least once, and then as long as a condition is true. Here's the basic syntax for a do-while loop in Solidity:

do {
  // code to execute repeatedly
} while (condition);

Here's an example of a do-while loop in action:

function factorial(uint num) public pure returns (uint) {
  uint result = 1;
  do {
    result *= num;
    num--;
  } while (num > 0);
  return result;
}

Ternary operator

The ternary operator allows you to assign a value to a variable based on a condition. It has the following syntax:

condition ? value1 : value2

Here, condition is the expression that is evaluated to a Boolean value, and value1 and value2 are the values that are returned depending on whether condition is true or false, respectively.

Here's an example of the ternary operator in action:

function checkNumber(uint num) public pure returns (string memory) {
  return num > 10 ? "The number is greater than 10" : "The number is less than or equal to 10";
}

In this example, if num is greater than 10, the function returns the string "The number is greater than 10". Otherwise, it returns the string "The number is less than or equal to 10".

The ternary operator can also be nested inside other expressions, allowing for more complex logic. Here's an example:

function getGrade(uint score) public pure returns (string memory) {
  return score >= 90 ? "A" : (score >= 80 ? "B" : (score >= 70 ? "C" : "F"));
}

In this example, the function takes a numerical score value and returns a letter grade based on the following scale: 90-100 is an "A", 80-89 is a "B", 70-79 is a "C", and anything below 70 is an "F". The nested ternary operators allow for this logic to be expressed in a concise way.

https://embed.0xecho.com.ipns.page/?color-theme=light&desc=&has-h-padding=true&has-v-padding=true&modules=comment%2Clike%2Ctip%2Cdislike&receiver=0x3e14a3Db52f6e8816C31A530ed4Ce778eACF988a&target_uri=dapp%2Fmirror%2FRnhQXediAjbyLo8DALlI65dw6bgA42ZF8JwtacARzsA&height=800&display=iframe