Skip to content

Types & Control Flow

Once functions and variables make sense, the next step is understanding:

  • what values exist
  • how conditions work
  • how loops work

Built-in types

TypeMeaning
int64-bit signed integer
u8unsigned 8-bit byte-sized integer
booltrue or false
strstring value
voidno return value
ptr Tpointer to T
result Tfallible value

byte is just another spelling of u8.

The most important control-flow rule

Conditions must already be boolean.

Valid:

cneg
if x > 5 {
    println(x);
}

Invalid:

cneg
if x {
    println(x);
}

That rule applies to:

  • if
  • while
  • the condition inside if expressions

explicit conditions only

If a condition is not bool, the compiler reports E3005.

Ordinary if / else

cneg
if x > 5 {
    println(x);
} else {
    println(0);
}

Use this when you want control flow.

if expressions

cnegative also supports a narrow value-producing if form:

cneg
let kind:int = if x > 5 { 1 } else { 0 };

Use this when you want to choose one value or another.

Current rules:

  • else is required
  • both branches must produce a value
  • both branches must resolve to the same type

Loops

While

Use while when the condition should be checked each time.

cneg
while x < 10 {
    x = x + 1;
}

Range for

Use this for simple counted loops.

cneg
for i:int in 0..10 {
    println(i);
}

Infinite loop

Use loop when you want a plain repeat-forever block.

cneg
loop {
}

Integer rule today

  • int is the normal arithmetic type
  • int supports +, -, *, /, and %
  • u8 is the byte-sized storage/value type
  • byte is just another spelling of u8
  • integer literals fit into u8 automatically when a u8 is expected
  • fitting integer literals also compare cleanly against u8 and byte values
  • arithmetic still stays int-only for now

Output rule today

  • print(...) writes without an implicit newline
  • println(...) writes the value and appends a newline

A small example with both int and byte

cneg
fn:int main() {
    let x:int = 9;
    let b:byte = 0;

    if x % 2 == 1 {
        println(x);
    }

    if b == 0 {
        return 0;
    }

    return 1;
}

Next step

Continue to Structs & Arrays.

cnegative docs track the current v0.5.2 compiler surface.