Skip to content
This repository has been archived by the owner on Apr 27, 2023. It is now read-only.

Controls Flow #9

Open
8 tasks
TheAwiteb opened this issue Mar 19, 2023 · 0 comments
Open
8 tasks

Controls Flow #9

TheAwiteb opened this issue Mar 19, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@TheAwiteb
Copy link
Owner

TheAwiteb commented Mar 19, 2023

Controls Flow

In ocypode we will have this controls flow:

  • if - The if statement is used to execute a block of code if a condition is true.
  • else if - The else if statement is used to execute a block if the condition is false and its condition is true.
  • else - The else statement is used to execute a block if all the conditions (if/else if) is false.
  • switch - The switch statement is used to execute a block of code depending on the value of a variable.
  • for - The for statement is used to execute a block of code a certain number of times.
  • while - The while statement is used to execute a block of code while a condition is true.
  • loop - The loop statement is used to execute a block of code until is breaked.
  • break - The break statement is used to break a loop.

All the control flow statements are expressions, so they can be used as values. (the last expression of the block will be the value of the expression)

All the syntaxes are subject to change. Until the first release.

The examples may be contain a unimplemented function, but they will be implemented in the future.

if/else if/else

This example shows how to use the if/else if/else statements.

~get_stage<age>{<
    return if<lt<age><12>>{<
        "You are a child"
    >}
    else if<lt<age><20>>{<
        "You are a teenager"
    >}
    else{<
        "You are an adult"
    >};
>}

~main<argc><argv>{<
    println<foo<10>>; // prints: You are a child
    println<foo<19>>; // prints: You are a teenager
    println<foo<30>>; // prints: You are an adult
>}

You can also print it directly:

~main<argc><argv>{<
    age = 23;
    println<
        if<lt<age><12>>{<
            "You are a child"
        >}
        else if<lt<age><20>>{<
            "You are a teenager"
        >}
        else{<
            "You are an adult"
        >}
    >;
>}

switch

SOON

for

SOON

while

This example shows how to use the while statement.

~main<argc><argv>{<
    counter = 0;
    while<lt<counter.copy<>><10>>{<
        println<counter.copy<>>;
        counter = add<counter><1>;
    >}
>}

loop/break

This example shows how to use the loop and break statement.

~main<argc><argv>{<
    counter = 0;
    // prints 1, 2, 3, 4, 6, 7, 8, 9, 10
    loop{<
        counter = add<counter><1>;
        if<eq<counter.copy<>><10>>{<
            break;
        >} else if<ne<counter.copy<>><5>>{<
            println<counter.copy<>>;
        >};
    >}
>}
@TheAwiteb TheAwiteb added the enhancement New feature or request label Mar 19, 2023
@TheAwiteb TheAwiteb self-assigned this Mar 19, 2023
@TheAwiteb TheAwiteb added this to the v0.3.0 milestone Mar 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant