Code Examples

Provide some simple code examples to showcase the language’s functionality. Link to more examples if needed.

Note

Extension: file_name.flint

Single-comments: // this is a single line Comments

Multi-comments: /* this is a multi-line comment */

Code Examples

Here are some simple Flint code examples:

Variables declaration and printing:

var greeting = "Hello, Flint!";
print greeting;     // prints: Hello, Flint!

Simple arithmetic operations:

var a = 10;
var b = 20;
var sum = a + b;
print sum;        // prints: 30

Conditional statements:

var a = 10;
var b = 20;
if (a > b) {
    print "a is greater than b";
} else {
    print "b is greater than a";
}

Loops:

  1. while loop:

var i = 0;
while (i < 5) {
    print i;
    i = i + 1;
}
  1. for loop (while loop with syntactic sugar):

for (var i = 0; i < 5; i = i + 1) {
    print i;
}

Functions:

fn add(a, b) {
    return a + b;
}

var sum = add(10, 20);  // function call
print sum;

Recursion:

fn factorial(n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

var result = factorial(5);
print result;