Statements
Statements form the basis of Kipper next to expressions, and
perform the basic operations and define the logic in your program. They can be easily identified
with their usage of either curly brackets ({ }
) or a semicolon (;
).
Every operation in a Kipper program must be contained in a statement to be valid. This also includes expressions, which can never be written alone.
List of statements in Kipper
Expression Statement
An expression statement is a simple expression that ends in a semicolon (;
) and
evaluates one or more expressions, without the involvement of any other statements.
Syntax
Examples
Common examples of expression statements are with increment or decrement expressions (
val++
/val--
) that simply add or subtract 1
to/from a value.
These often do not require any other expressions, and can simply be used as a whole statement, like
this:
Compound Statement
A compound statement is also called block of code, and defines, as already explained in variables, a scope for variables. Compound statements are commonly used together with other statements, such as if-statements, while-loops and for-loops.
Syntax
If Statement
This also has its own docs page, which can be accessed here.
If-statements or also if-else-statements make up an essential part of every program. These
statements check if a specified condition is met and react accordingly. If the specified condition
isn’t met, the program will execute a different code, which may be specified inside an
else
code block.
Syntax
They must have a single starting if
, may have multiple extending
else if
branches and can have a single ending else
branch, which is
evaluated if the previous condition were all false
.
Examples
For-loop Statement
Released in v0.10.0 - Please update your version to access this feature.
This also has its own docs page, which can be accessed here.
For-loops are statements with a loop-expression, a loop condition and a statement body that are executed as long as a specified condition is met.
For-loops have the unique attribute, unlike while-loops and
do-while-loops, of having an executable
LOOP_EXPRESSION
, which is evaluated at the end of every loop cycle. This
LOOP_EXPRESSION
can be used to do repeating tasks at the end of a loop cycle, like
calling a function or increasing a counter.
Syntax
The execution of a for-loop follows this schema:
- Evaluate
INIT_EXPRESSION
, if it exists (Only the first time). -
Check
CONDITION
, if it exists, before running theSTATEMENT
. IfCONDITION
isfalse
, then the loop will be stopped! - Run
STATEMENT
ifCONDITION
wastrue
. -
Evaluate
LOOP_EXPRESSION
after finishing the execution ofSTATEMENT
, and the loop was not stopped usingreturn;
orbreak;
.
While-loop Statement
Released in v0.10.0 - Please update your version to access this feature.
This also has its own docs page, which can be accessed here.
While-loops are code blocks that are executed multiple times as long as the specified
CONDITION
is met. The behaviour of while-loops is very similar to
do-while-loops, and in some cases it even might be better to use
do-while than while-loops, so watch out for opportunities to replace while-loops with do-while
loops.
Syntax
The execution of a while-loop follows this schema:
-
Check
CONDITION
, if it exists, before running theSTATEMENT
. IfCONDITION
isfalse
, then the loop will be stopped! - Run
STATEMENT
ifCONDITION
wastrue
.
Do-while-loop Statement
Released in v0.10.0 - Please update your version to access this feature.
This also has its own docs page, which can be accessed here.
Do-while-loops are loops with a similar behaviour to while-loops,
with the main difference being that do-while loops run the statement once before starting evaluating
its condition. Afterwards the statement is only run if the CONDITION
is met.
Syntax
The execution of a do-while-loop follows this schema:
- Run
STATEMENT
(Only the first time). -
Check
CONDITION
, if it exists, before running theSTATEMENT
again. IfCONDITION
isfalse
, then the loop will be stopped! - Run
STATEMENT
ifCONDITION
wastrue
.
Jump Statement
Jump statements are statements that can stop the execution of a function or a loop (while-loops, do-while-loops or for-loops) prematurely.
Jump statements must always have a valid target (parent statement) that can be determined at compile time, otherwise the statement is invalid and an error will be thrown.
These jump statements are:
-
break
- Stops a while-loop, do-while-loop or for-loop. -
return
- Returns from a functions with an optional specific return value (Must match function return type). -
continue
- Jumps directly to the execution of the next loop cycle and will compare the loop-condition again.