Guide for code.iTriangle.cz TODO Basic UI and the first look TODO Measuring and experimenting with sensors TODO Blockly programing and basic of robotics Visual programing blocks – Logic Boolean algebra is a mathematical system that has two values: true false Boolean values (also called conditions) are used in these control blocks, which contain examples: conditional blocks repeat blocks One of the many examples from those pages is: If the value of variable x is greater than 100, the condition is true and the text "What a big number!" is displayed. If the value of x is less than 100, the condition is false and "That's not very big." is displayed. Boolean values can also be stored in variables and passed on to procedures in the same way as numbers, text or list values. Blocks If a block expects a Boolean value as an input, it usually interprets an absent input as false. An example is provided below. Non-Boolean values cannot be directly plugged in where Boolean values are expected, although it is possible (but inadvisable) to store a non-Boolean value in a variable, then plug it into the input. Neither of these practices are recommended, and their behaviour could change in future versions of Blockly. Values A single block, with a drop-down menu specifying either true or false can be used to get a boolean value: Comparisons There are six comparison operators. Each takes two inputs (usually numbers) and returns a true or false condition depending on how the inputs compare to each other. The six operators are as follows: is equal to, does not equal, is less than, is less than or equal to, is greater than, is greater than or equal to. Logical operations The and block will return true only if both of its two inputs are also true. The or block will return true if either of its two inputs are true. not The not block converts its Boolean input into its opposite. For example, the result of: is false. As mentioned above, if no input is provided, a value of true is assumed and the following block produces the value false: Leaving an input empty is not recommended, however.Visual programing blocks - IfElse Conditional statements are central to computer programming. They make it possible to express statements like: If there is a path to the left, turn left. If score = 100, print "Well done!". See additional information about conditional statements. Blocks If blocks The simplest conditional statement is an if block, as shown: When run, this will compare the value of the variable x to 100. If it is larger, "What a big number!" will be printed. Otherwise, nothing happens. If-Else blocks It is also possible to specify that something should happen if the condition is not true, as shown in this example: As with the previous block, "What a big number!" will be printed if x > 100; otherwise, "That's not very big." will be printed. An if block may have zero or one else sections but not more than one. If-Else-If blocks It is also possible to test multiple conditions with a single if block by adding else if clauses: The block first checks if x > 100, printing "What a big number!" if it is. If it is not, it goes on to check if x = 42. If so, it prints "That's my lucky number." Otherwise, nothing happens. An if block may have any number of else if sections. Conditions are evaluated top to bottom until one is satisfied, or until no more conditions are left. If-Else-If-Else blocks As shown here, if blocks may have both else if and else sections: The else section guarantees that some action is performed, even if none of the prior conditions are true. An else section may occur after any number of else if sections, including zero. Block Modification Only the plain if block appears in the toolbox: To add else if and else clauses, the user needs to click on the gear icon, which opens a new window: The user can then drag else if and else clauses into the if block, as well as reordering and removing them. When finished, the user should click on the minus sign, which closes the window, as shown here: Note that the shapes of the blocks allows any number of else if subblocks to be added but only up to one else block.Visual programing blocks – Lists As in everyday speech, a Blockly list is an ordered collection of items, such as a "to do" list or a shopping list. Items in a list may be of any type, and the same value may appear more than once in a list. List Creation create empty list The simplest list is the empty list, which is created with the create empty list block: create list with Basic use The create list with block allows the user to specify the initial values in a new list. In this example, a list of words is created and placed in a variable named letters: For this document, we'll denote this list as ["alpha", "beta", "gamma"], and we will refer below to the variables defined in this section. This shows the creation of a list of numbers: This creates a list of colours: A list with values of different types is less common, but possible : Changing the number of inputs To change the number of inputs, click on the gear icon plus symbol. This opens a new window: You can then drag item sub-blocks from the left side of the window into the if block on the right side to add a new input, as shown: The new item was added to the bottom in this example, but it can be added at any position on the list. Similarly, unwanted item sub-blocks can be dragged from the if block to the left and removed. create list with item The create list with item block lets you create a list that has a certain number of copies of an item. For example, the following blocks set the variable words to the list containing ["very", "very", "very"]. Checking a list's length is empty The value of an is empty block is true if its input is the empty list and false if it is anything else (including a non-list). IS THIS TRUE? The value of the following blocks would be false because the variable colours is not empty: it has three items. Note the similarity to the "is empty" block for text. length of The value of the length of block is the number of elements in the list used as an input. For example, the value of the following blocks would be 3 because colour has three items. Note that the length of block tells you how many items are in the list, not how many different items are in it. For example, the following has the value 3, even though words consists of three copies of the same text (["very", "very", "very"]). Note the similarity to the "length of" block for text. Finding items in a list These blocks find the position of an item in a list. For example, the following has a value of 1 because the first appearance of "very" is as the beginning of the words list (["very", "very", "very"]). The result of the following is 3 because the last appearance of "very" in words is in position 3. If the item is nowhere in the list, the result is the value 0, as in this example: These blocks are analogous to the ones for finding letters in text. Getting items from a list Getting a single item Recall the definition of the list colours: The following block gets the colour blue because it is the second element in the list (counting from the beginning on the right): The following gets green because it is the second element counting from from the end on the right: The next block gets the first element, red: The next gets the last element, yellow: This block randomly selects an item from the list, returning any of red, blue, green or yellow with equal likelihood. Getting and removing an item A drop-down menu on the in list ... get block changes it to in list ... get and remove, which provides the same output but also modifies the original list: This example sets the variable first letter to "alpha" and leaves letters as: ["beta", "gamma"]. Removing an item Selecting "remove" on the drop-down menu causes the plug at the left of the block to disappear: This removes the first item from letters. Getting a sublist The in list ... get sublist block is similar to the in list ... get block except that it extracts a sublist, rather than an individual item. There are several options how the start and end of the sublist can be specified: In this example, a new list first letters is created. This new list has two elements: ["alpha", "beta"]. Note that this block does not modify the original list. Adding items to a list in list ... set The in list ... set block replaces the item at a specified location in a list with a different item. For the meaning of each of the drop-down menu options, see the previous section. The following example does two things: The list words is created with 3 items: ["very", "very", "very"]. The third item in the list is replaced by "good". The new value of words is ["very", "very", "good"]. in list ... insert at The in list ... insert at block is obtained from the drop-down menu on the in list ... set block: It inserts a new item into the list at the specified location, before the item previously at that location. The following example (built on an earlier one) does three things: The list words is created with 3 items: ["very", "very", "very"]. The third item in the list is replaced by "good". The new value of words is ["very", "very", "good"]. The word "you're" is inserted at the beginning of the list. The final value of words is ["You're", "very", "very", "good"]. Splitting strings and joining lists make list from text The make list from text block splits the given text into pieces using a delimiter: In the above example, a new list is returned containing three items of text: "311", "555", and "2368". make text from list The make text from list block joins a list into a single text using a delimiter: In the above example, a new text is returned with the value: "311-555-2368". Related blocks Printing a list The print block in the Text category can display lists. The result of the following program is shown in the alert box: Doing something for each item in a list The for-each block in the Control category performs an operation on every item in a list. For example, these blocks individually print each item in the list: This does not remove the items from the original list. See also the examples of loop termination blocks.Visual programming blocks – Loops The Control category contains blocks that control whether other blocks placed in their body are run. (For example, in the "repeat" block below, the body contains the "print" block and its input.) There are two types of control blocks: IfElse (described on its own page) and this one, which controls how many times the body is run and, in some cases, the value of a variable used within the body. These structures are called loops since the body is repeated (possibly) multiple times, reminiscent of a rope containing loops. Each pass through the loop is called an iteration. For more information, see https://en.wikipedia.org/wiki/Control_flow#Loops. Blocks for Loop Creation repeat The simplest "repeat" block runs the code in its body the specified number of times. For example, the following block will display "Hello!" ten times. repeat while Imagine a game in which a player rolls a die and adds up all of the values rolled as long as the total is less than 30. The following blocks implement this game: A variable named total gets an initial value of 0. The loop begins with a check that total is less than 30. If so, the blocks in the body are run. A random number in the range 1 to 6 is generated (simulating a die roll) and stored in a variable named roll. The number rolled is displayed. The variable total increases by roll. At end of the loop, control returns to step 2. When the loop completes, any subsequent blocks (not shown) would be run. In our example, the loop would end after random numbers in the range 1 to 6 had been displayed a certain number of times, and the variable total would contains the sum of these numbers, which would be guaranteed to be at least 30. For more information, see https://en.wikipedia.org/wiki/While_loop. repeat until "Repeat while" loops repeat their bodies while a condition is true. Repeat-until loops are similar except that they repeat their bodies until a condition is true. The following blocks are equivalent to the previous example because the loop contains until total is greater than or equal to 30. count with The count with block (called a for loop in most programming languages) advances a variable from the first value to the second value by the increment amount (third value), running the body once for each value. For example, the following program displays the numbers 1, 3, and 5. The following two loops will each display the numbers 5, 3, and 1. The first value can be larger than the second. The behavior is the same whether the increment amount (third value) is positive or negative. for each The for each block (see https://en.wikipedia.org/wiki/Foreach) is similar, except instead of giving the loop variable values in a numeric sequence, it uses the values from a list in turn. The following program displays each element of the list: "alpha", "beta", "gamma". Loop Termination Blocks Most loops run until the terminating condition (in the case of repeat blocks) is met or until all values have been taken by the loop variable (in the case of count with and for each loops). Two rarely needed but occasionally useful blocks provide additional means for controlling loop behavior. Although the examples below show for each loops, they can be used with any type of loop. continue with next iteration The continue with next iteration (called continue in most programming languages) causes the remaining code in the body to be skipped and for the next iteration (pass) of the loop to begin. The following program displays "alpha" on the first iteration of the loop. On the second iteration, the continue with next iteration block is run, skipping the display of "beta". On the final iteration, "gamma" is displayed. break out of loop The break out of loop block provides an early exit from a loop. The following program displays "alpha" on the first iteration and "breaks out" of the loop on the second iteration when the loop variable is equal to "beta". The third item in the list is never reached. Visual programming blocks – Text Examples of items of text are: "thing #1" "March 12, 2010" "" (empty text). Text can contain letters (which may be lower-case or upper-case), numbers, punctuation marks, other symbols and blank spaces between words. (The non-Blockly term for all of these different types of text is character.) Blocks Text creation The following block creates the text "hello" and stores it in the variable named greeting. The create text with block combines (concatenates) the value of the greeting variable and the new text "world" to create the text "helloworld". Note that there is no space between them, since none was in either original text. To increase the number of text inputs, click on the gear icon, which changes the view to: Additional inputs are added by dragging an "item" block from the gray toolbox on the left into the "join" block. Text modification The to ... append text block adds the given text to the specified variable. In this case, it changes the value of the variable greeting from "hello" to "hello, there!" Text length The length of blocks count the number of letters, numbers, etc., in each text. The length of "We're #1!" is 9, and the length of empty text is 0. Checking for empty text The is empty block checks whether the given text is empty (has length 0). The result is true in the first case and false in the second. Finding text These blocks can be used to check whether an item of text is in another item of text and, if so, where it appears. For example, this asks for the first occurrence of "e" in "hello". The result is 2. This asks for the last occurrence of "e" in "hello", which is also 2. Whether first or last is selected, this block will give the result 0 since "hello" does not contain "z". Extracting text Extracting a single character This gets "b", the second letter in "abcde": This gets "d", the second to last letter in "abcde": This gets "a", the first letter in "abcde": This gets "e", the last letter in "abcde": This gets any of the 5 letters in "abcde" with equal probability: None of these modify the text on which the extraction is performed. Extracting a region of text The in text ... get substring block allows a region of the text to be extracted, starting with either: letter # letter # from end the first letter and ending with: letter # letter # from end the last letter In the following example, "abc" is extracted. Adjusting text case This block creates a version of the input text that is either: UPPER CASE (all letters upper-case) lower case (all letters lower-case) Title Case (first letters upper-case, other letters lower-case) The result of the following block is "HELLO". Non-alphabetic characters are not affected, including text in languages with no letter case, such as Chinese. Trimming (removing) spaces The following block removes space characters from: the beginning of the text the end of the text both sides of the text The result of the following block is "hi   there". (Spaces in the middle of the text are not affected.) Displaying text The print block causes the input value to be displayed in a pop-up window, as shown: If the code is exported as JavaScript, Python or Dart, it will be displayed on the console (screen). It is never sent to a printer, as the name might suggest. Getting input from the user The following block creates a pop-up window that prompts the user to enter a name. The result is stored in the variable name: This is what the current version of the pop-up window looks like: There is also a version of the block for getting a number from the user: Visual programming blocks – Variables We use the term variable in the same way as it is used in mathematics and other programming languages: a named value that can be changed (varies). Variables can be created in several different ways. Every count with and for each block uses a variable and defines its values. These values can only be used in thes blocks. A traditional computer science term for these are loop variables. User-defined functions (also known as "procedures") can define inputs, which creates variables that can be used only within the function. These are traditionally called "parameters" or "arguments". Users may create variables at any time through the "set" block. These are traditionally called "global variables". Blockly does not support local variables. Default names While users can choose any name for a variable, core Blockly provides a default name, "item", as shown in the below picture. Some applications provide other default values, such as "value", also shown below. Drop-down menu Clicking on a variable's drop-down symbol (triangle) gives the following menu: The menu provides the following options. the names of all variables defined in the program. "Rename variable...", which changes the name of this variable wherever it appears in the program. Selecting this opens a small window that prompts the user for the new name with the text: "Rename all %1 variables to:", where %1 is replaced by the old name (here "item"). "New variable...", which allows the user to enter a new name for the variable without replacing or changing variables with the old name (here "item"). Selecting this opens a small window that prompts the user for the new name with the text "New variable name:". Blocks Set The set block assigns a value to a variable, creating the variable if it doesn't already exist. For example, this sets the value of the variable named "age" to 12. Get The get block provides the value stored in a variable, without changing it. It is possible, but a bad idea, to write a program in which a get appears without a corresponding set. Example Consider the following example of code: The first row of blocks creates a variable named "age" and sets its initial value to the number 12. The second row of blocks gets the value 12, adds 1 to it, then stores the sum (13) into the variable. The final row displays the message: "Happy birthday! You are now 13"