Control Flow Statements¶
Basically, you can control the flow of your Pineapple code with any of the following:
-
if
,elif
andelse
-
for
loop -
while
loop -
break
,continue
orreturn
If-elif-else statements¶
Similar to Python, all you need is indentation (tab), you don't need those curly brackets. For example:
1 2 3 4 5 6 7 8 | if he.isCrazy you.callAmbulance elif he.isInDanger you.goHelp(he) else you.doNothing |
Note
Every test expression must have type of Boolean.
Tips
.isCrazy
and other similar construct are just functions.
Test expression chaining¶
Sometimes, a single test expression is not enough to express what you really wanted. In such situation, you can use the following functions:
-
.and
-
.or
-
.not
For example:
1 2 3 4 5 6 7 8 | if he.isYoung.and(he.isNaughty) "He is a kid".show elif sky.isBlue.or(air.isFresh) "I am happy!".show elif current.isLunchTime.not "Continue working . . .".show |
For statements¶
For statements is used to iterate over a list.
For example:
1 2 | for x in [1,2,3,4] x.show |
While loop¶
While loop is use to loop some code until certain condition is met.
1 2 | while not file.atEOF file.readline.show |