Skip to content

Control Flow

Statements: Block or Expression

Every control-flow statement in Cera accepts either a block body { } or a single statement/expression as its body:

// Block body
if (x > 0)
{
Console.WriteLine("positive");
DoSomething();
}
// Single-statement body — no braces needed
if (x > 0)
Console.WriteLine("positive");

This applies to if, else, while, for, and other control-flow constructs.

If / Else

if (x > 0)
Console.WriteLine("positive");
else if (x < 0)
Console.WriteLine("negative");
else
Console.WriteLine("zero");
// With block bodies
if (x > 0)
{
Console.WriteLine("positive");
x = x - 1;
}
else
{
Console.WriteLine("not positive");
}

Branch Prediction Hints

Cera supports branch prediction hints that help the optimizer produce better code layout. Place likely, unlikely, or const between if and the opening parenthesis:

if likely / if unlikely

Hint to the compiler which branch is the common case. The optimizer uses these to reorder machine code blocks so the hot path falls through without extra jumps.

int SafeDivide(int a, int b)
{
if unlikely (b == 0)
return -1; // rare error path — pushed to end of function
return a / b; // hot path — falls through naturally
}
int Process(int x)
{
if likely (x > 0)
return x * 2; // common path
return 0;
}

if const

Evaluate a boolean condition at compile time and eliminate the dead branch entirely. No code is generated for the branch that can never execute:

if const (true)
{
return 42; // only this branch is compiled
}
else
{
return -999; // completely eliminated — no code emitted
}

if const currently supports true and false literals. Non-constant conditions fall back to normal branch emission.

Ternary Operator

string result = age >= 18 ? "adult" : "minor";

While Loops

int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
// Single-statement body
while (i > 0)
i--;

For Loops

C-style for loops:

for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
// Single-statement body
for (int i = 0; i < 10; i++)
Console.WriteLine(i);

Range-based iteration over arrays:

int[] numbers = [1, 2, 3, 4, 5];
for (int n : numbers)
Console.WriteLine(n);

Break and Continue

for (int i = 0; i < 100; i++)
{
if (i % 2 == 0)
continue; // skip even numbers
if (i > 10)
break; // stop at 10
Console.WriteLine(i);
}

Switch

Cera’s switch statement supports integer, float, string, and type pattern cases. The compiler detects the variant automatically based on the first case value.

Integer Switch

Use break or return to exit each case:

int result = 0;
switch (n)
{
case 1:
result = 10;
break;
case 2:
result = 20;
break;
default:
result = -1;
break;
}

Dense integer switches (≥4 cases with values close together) are automatically optimized to a jump table for O(1) dispatch instead of a chain of comparisons.

Float Switch

switch (temperature)
{
case 0.0:
return "freezing";
case 100.0:
return "boiling";
case -273.15:
return "absolute zero";
default:
return "other";
}

String Switch

switch (command)
{
case "start":
return 1;
case "stop":
return 2;
case "pause":
return 3;
default:
return 0;
}

Type Switch

Match on the runtime type of a value. The compiler detects a type switch when case labels start with a type name:

void Describe(object value)
{
switch (value)
{
case int i:
Console.WriteLine($"int: {i}");
break;
case string s:
Console.WriteLine($"string: {s}");
break;
case float f:
Console.WriteLine($"float: {f}");
break;
default:
Console.WriteLine("other");
break;
}
}

Next Steps

Learn how to build your own types with types and methods.