Skip to content

Latest commit

 

History

History
103 lines (74 loc) · 4.61 KB

do-while.md

File metadata and controls

103 lines (74 loc) · 4.61 KB

simple do while

This example demonstrates a simple do-while statement. This function will return aaaaa.

note: as incrementing/decrementing numbers is not supported in ASL the examples uses a string (and string concatenation) as a counter.

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let counter = "";
  do {
    counter = `${counter}a`;
  } while (counter != "aaaaa");
  return counter; //returns "aaaaa"
});

simple do always false

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let counter = "";
  do {
    counter = `${counter}a`;
  } while (false);
  return counter; //returns "1"
});

do while with break

This example demonstrates a do-while statement with a break statement. The do-while statement will break once the container equals aa, this function will return aa.

note: as incrementing/decrementing numbers is not supported in ASL the examples uses a string (and string concatenation) as a counter.

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let counter = "";
  do {
    counter = `${counter}a`;
    if (counter == "aa") {
      break;
    }
  } while (counter != "aaaaa");
  return counter; //returns "aa"
});

do while with early return

This example demonstrates a do-while statement with an early return. The function will return from within the do-while statement, this function will return aa.

note: as incrementing/decrementing numbers is not supported in ASL the examples uses a string (and string concatenation) as a counter.

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let counter = "";
  do {
    counter = `${counter}a`;
    if (counter == "aa") {
      return counter; //returns "aa"
    }
  } while (counter != "aaaaa");
  throw new Error("this should not happen");
});

do while with continue

This example demonstrates a do-while statement with a continue statement. The function will not add b to result in one of the iterations, this function will therefore return bbbb (4 x b instead of 5 x b).

note: as incrementing/decrementing numbers is not supported in ASL the examples uses a string (and string concatenation) as a counter.

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let counter = "";
  let result = "";
  do {
    counter = `${counter}a`;
    if (counter == "aa") {
      continue;
    }
    result = `${result}b`;
  } while (counter != "aaaaa");
  return result; //returns "bbbb"
});