ChardScript Booleans
Booleans represent one of two values: true
or false
.
Boolean Values
In programming you often need to know if an expression is true
or false
.
You can evaluate any expression in ChardScript, and get one of two answers, 1
or 0
, 1
is true
and 0
is false
.
When you compare two values, the expression is evaluated and ChardScript returns the Boolean answer:
Example
echo(10 > 9)
echo(10 == 9)
echo(10 < 9)
When you run a condition in an if statement, ChardScript returns true
or false
:
Example
Print a message based on whether the condition is true
or false
:
call a = 200
call b = 33
if b > a then
echo("b is greater than a")
else
echo("b is not greater than a")
end
Functions can Return a Boolean
You can create functions that returns a Boolean Value:
Example
Print the answer of a function:
group myFunction()
give true
end
echo(myFunction())
You can execute code based on the Boolean answer of a function:
Example
Print "YES!" if the function returns true, otherwise print "NO!":
group myFunction()
give true
if myFunction() then
print("YES!")
else
print("NO!")
end
ChardScript also has many built-in functions that return a boolean value, likeisnumber()
, isstring()
,islist()
andisgroup()
functions.
Example
Check if an object is an number or not:
call x = 200
echo(isnumber(x))
Example
Check if an object is an string or not:
call x = "Hello World!"
echo(isstring(x))
Example
Check if an object is an list or not:
call x = ["apple", "banana"]
echo(islist(x))
Example
Check if an object is an function or not:
group my_function()
give "Return from a function"
end
echo(isgroup(x))
Last updated