Cafe Fundamentals
This section covers fundamental concept of Structured Programming for any algorithm, that is Iteration, selection & Sub-sequence
with the syntax to implement in cafe.
#
CommentsCafe supports single line comment using #
Symbol and multi line comments using /*..*/
#
Variables & ConstantsAs cafe is dynamically typed, the variables can be declared using var
keyword and constants can be declared using const
keyword.
#
Data typesString : represents sequence of characters.
Number: : represents numeric values.
Boolean : represents boolean value either true
or false
.
#
OperatorsSymbol(s) | Description | Examples |
---|---|---|
= | Assign a value to a variable. | var a = 10; |
+ | Returns the sum of numeric operands or string concatenation. | cmd.println(2+3); # 5 cmd.println(“Hello” + “World”); # HelloWorld |
- | Subtracts the two operands, producing their difference. | cmd.println(4-2); # 2 |
* | Multiplication on numbers and strings. | cmd.println(4*2); # 8 cmd.println("Hello"*2) # HelloHello |
/ | Division on numbers. | cmd.println(4/2); # 2 |
% | Modulo on numbers. | cmd.println(8/3); # 2 |
** | Computes the power of a number. | cmd.println(2**3); # 8 |
// | Computes the floor of a number. | cmd.println(9//2); # 4 |
|| , && , !or , and, not | Logical operators. | cmd.println((true and true)); # true cmd.println(not true); # false |
== , != , > , >= , <, <= | Relational operators. | cmd.println((2 >= 3)); # false |
>> , >>> , ~ , << | Bitshift operators. | cmd.println((2 >> 1)); # 1 |
& , | , ^ , ~ | Bitwise operators. | cmd.println((2 & 2)); # 2 |
#
Control Flowif..else
#
The if statement executes a statement if a specified condition is evaluated true.
If the condition is false, another statement can be executed.
Multiple if...else
statements can be nested to create an else if
clause.
Syntax
Example
for
Loop#
The for
statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by ;
, followed by a statement (usually a block statement) to be executed in the loop.
Syntax
Example
{Object}
#
Cafe Objects are a collection of properties made up of key-value pairs. Keys are unique & valid identifiers. Values can be any valid type (primitives, Functions or Objects). Objects can be declared using {} notation.
Syntax
Example
Object properties can be accessed using the .
or []
operator.
Syntax
Example
Function
#
In CAFE, functions can be declared using keyword func
.
Syntax
name
The function name.
param
(Optional)
The argument names to be passed to the function.
statements
The statements which comprise the body of the function.
Example