G
  • Home
  • Playground
  • Gardener image

    Gardener

    Learn programming in a fun and easy way.

    About Gardener

    Gardener is a simple programming language suitable for learning how to write programs. If you are new to programming, the Gardener language is the perfect place to start.

    Gardener language allows you to control a gardener who can walk around the garden, planting and picking plants.

    Using the Gardener language, you will now learn to execute commands, repeat commands multiple times, conditionally execute commands and create small subroutines (functions). These structures (statements) exist in almost every professional language, and understanding a simple language like Gardener will allow you to understand other languages more easily.

    Gardener language is English only. Do not use letters, numbers, symbols, etc. that do not exist in English.

    You can try running the educational examples below by clicking the execute button (play_circle) above the code example and see how the code works. It will change to silver while the programme is running and back to black when the programme has finished.

    You can use playground to test your programming skills.

    fnc turnRight { repeat 3 { turnLeft } } step // comment repeat 3 { plant } turnRight if not isPlant { plant }

    If you have any questions or suggestions, please send me a message.

    Comment

    Comments are parts of the program that are not executed, comments are ignored. Comments exist only for developers to document their code.

    You can specify comments by double slash //. Everything on the line after // will be ignored.

    Basic commands

    Basic commands tell the gardener to do something (e.g. move forward, pick a plant). Each command must be on a separate line. Spaces before or after are ignored.

    step

    The step command moves the gardener one step forward in the direction the gardener is facing.

    If the gardener is facing the wall, the programme will stop with an error.

    turnLeft

    The turnLeft command turns the gardener to the left.

    plant

    The plant command plants a plant at the location where the gardener is standing.

    If the maximum number of plants is reached, the programme will stop with an error. The default maximum is 10.

    pick

    The pick command picks up a plant from where the gardener is standing.

    If there is no plant to pick up, the programme will stop with an error.

    plant plant step //this is a comment turnLeft turnLeft plant step pick

    You can use playground to test your programming skills.

    Conditions

    Conditions allow the developer to test whether something is true or false. All conditions have a similar format:

    if <condition> { <statement(s)> }

    Conditions are created by:

    • line with if keyword, followed by space(s), followed by condition to test, followed by space(s), followed by opening curly bracket {
    • list of commands or other conditions or other parts of the programme (zero or more)
    • line with closing curly bracket }

    isWall

    The isWall condition is true, if the gardener is facing a wall. Otherwise it is false.

    isNorth

    The isNorth condition is true, if the gardener is facing north (up). Otherwise it is false.

    isPlant

    The isPlant condition is true if there is a plant where the gardener is standing. Otherwise it is false.

    not

    not has a special meaning, it negates the condition.

    E.g. not isWall condition is true, if the gardener is not facing a wall. Otherwise it is false.

    // following condition will be executed, // gardener is not facing a wall if not isWall { step } turnLeft // following condition will be executed, // gardener is facing a wall // `isWall` is true if isWall { plant plant } // following condition will not be executed, // gardener is facing a wall // `not isWall` is false if not isWall { step }

    You can use playground to test your programming skills.

    repeat - repeat command multiple times

    repeat statements allow the developer to repeat a statement multiple times.

    repeat <number> { <statement(s)> }

    repeat statement is created by:

    • line with repeat keyword, followed by space(s), followed by number of repetition, followed by space(s), followed by opening curly bracket {
    • list of commands or other conditions or other parts of the programme (zero or more)
    • line with closing curly bracket }

    // make 2 steps, turn back // and repeat that 3 times repeat 3 { step step turnLeft turnLeft }

    You can use playground to test your programming skills.

    while - repeat command while condition is met

    while statements allow the developer to repeat a statement while a condition is met.

    while <condition> { <statement(s)> }

    while statement is created by:

    • line with while keyword, followed by space(s), followed by condition to test, followed by space(s), followed by opening curly bracket {
    • list of commands or other conditions or other parts of the programme (zero or more)
    • line with closing curly bracket }

    Conditions are the same as in if statements.

    // plant 3 plants // and pick them all up repeat 3 { plant } while isPlant { pick } // go to the end while not isWall { step }

    You can use playground to test your programming skills.

    Functions

    Functions represent multiple statements that can be defined as one command. E.g. Gerdener only knows `turnLeft` but does not know, how to turn back. You can create new command e.g. `turnBack` (that would be the function) and you can call this new command instead of calling `turnLeft` twice every time you want to turn back.

    fnc <functionName> { <statement(s)> }

    fnc statement is created by:

    • line with fnc keyword, followed by space(s), followed by function name, followed by space(s), followed by opening curly bracket {
    • list of commands or other conditions or other parts of the programme (zero or more)
    • line with closing curly bracket }

    fnc turnBack { turnLeft turnLeft } fnc twoSteps { repeat 2 { step } } // never executed fnc threeSteps { repeat 3 { step } } // program starts here twoSteps turnBack twoSteps turnBack

    Unlike other statements, functions are not executed until they are called. Functions are 'declared', which means they are just added to the language. You must call functions to execute them.

    In the following example, nothing happens, the function is only declared but never called.

    fnc turnBack { turnLeft turnLeft }

    You must call the function to execute it.

    fnc turnBack { turnLeft turnLeft } turnBack // function call

    You can use playground to test your programming skills.

    Nesting

    `Nesting` means placing statements in other statements. In following example there is repeat statement nested in while statement.

    while not isWall { repeat 3 { plant } step }

    You can nest any statement inside any other statement. The only exception is functions, functions cannot be nested, they can only be declared at the top level. Following code would not work.

    repeat 3 { fnc back { // error turnLeft turnLeft } }
    fnc steps { fnc back { // error turnLeft turnLeft } step }

    You can use playground to test your programming skills.

    We use cookies and similar technologies to analyze website traffic using Google Analytics. This helps us understand how visitors interact with our site.

    More info

    This website utilizes Google Analytics, a web analytics service provided by Google, Inc. Google Analytics uses cookies to help us analyze how users interact with our site. The information generated by the cookie about your use of the website (including your IP address) will be transmitted to and stored by Google. We use this information to compile reports on website activity and provide other services relating to website activity and internet usage.

    Analytics cookies helps us improve our service.

    Do you have any question? Suggestion?

    Title (*):

    Email:

    Message (*):

    (*) Mandatory fields.