Tutorial 3. Basic flow control
The DRAKON equivalent of the if statement is the Question icon.
Let's create a program module and add a main function to it. We are going to make a function called printNumber. That function will take a number and output the text representation of the number. We start using printNumber as if it existed.
Now, let's create the function. It will accept one argument called number.
We add a Question icon. If number is equal to 10, we print "ten." We also output number itself for reference. Let's build and run the project.
We open the console and see online one line.
10 "ten"
Our printNumber function can handle only value 10. Let's add support for all other values. If number is not equal to 10, we print "some other value."
We build and run again. Now, there are three lines in the output.
10 "ten" 20 "some other value" 30 "some other value"
Question icons can form complex decision trees. We will add another Question icon. If number is equal to 20, we print "twenty."
We should get a greater variety of results now. We build, reload, and check the console.
10 "ten" 20 "twenty" 30 "some other value"
Note that it is possible to swap the "Yes" and "No" labels on a Question icon. Right-click it and choose "Swap Yes and No." To preserve the logic of the algorithm, we must rearrange the paths on the flowchart. I extensively use Copy, Cut, and Paste operations to achieve this. I also have to rewire some of the lines. First, I click on a line (or its end). Then, I choose the destination by clicking on the desired yellow circle.
Let's build and run the project after this re-arrangement. As we expect, the core of the algorithm remained the same.
10 "ten" 20 "twenty" 30 "some other value"