After you've learned the basics of programming, you will begin connecting inputs, such as buttons or sensors, and outputs, such as screens and lights, to your Propeller Activity Board WX. You may want to write a program that makes a decision. For instance, you may want your program to turn on a light when you press a button.
In this program, you will use the Terminal receive text block located under COMMUNICATE > TERMINAL and the length of string block from OPERATORS > STRINGS to make a program that decides if an individual’s first name is shorter or longer than 7 letters.
You will not be able to use the delete/backspace key, so if you make a mistake, just reload your program and start over. Now, your program will display how many letters long the name is that you entered.
Use spaces when typing text in your block to help make your program more readable in the Terminal. If you don’t add spaces to text that appears before and after a number that you are trying to print in the middle of a statement, it will look like this:
By adding spaces, the output is easier to read:
These extra spaces will print this in the terminal:
Below is the if...do block, found in the CONTROL category:
The if...do block only works if you attach another block that makes a comparison. For example, the compare values block from the OPERATORS > NUMBERS category as shown here:
To use this block, insert two values and select a comparison to make between them from the drop-down menu. The block evaluates the comparison statement and provides a value of 1 if the statement is true, or 0 if the statement is false.
The = symbol is called a relational operator. Here is the list of relational operators in the compare values drop down menu:
= Equal to
≠ Not equal to
< Less than
≤ Less than or equal to
> Greater than
≥ Greater than or equal to
Sometimes, you may need to do one action if a condition is true, and a different action if it is false. In its original form, the if...do block only completes an action if the condition is true. You can, however, alter the if...do block.
The if...do block is a unique mutating block, meaning you can change how it works. Click on the blue gear icon in front of the word “if,” and a box will pop up for selecting options:
or
What if you want to test whether the name you entered is exactly 7 letters long, in addition to testing whether it is greater or less than 7 letters long? You can mutate the if...do block again to include an else if condition: