Statements, Variables and Conditional Statements
Statements
Each line of code that is executed or read in a computer program is called a statement.
In the first program we made it was literally one statement.
TextWindow.WriteLine("Hello World")
Many of the statements we use will use already coded functions and properties that belong to specific classes. The classes we use are the first key wordslike TextWindow. Inside this class or object there are a number of different functions and properties we can access. This would be the second part of the statement or the second key words in the statement separated from the first by the “.”.
The final part of the statement includes the argumentthat is placed in parenthesis, “()” in a function (if it takes an argument). In the example above it is “Hello World”.
Variables
We have talked about variables a number of times, but the clearest definition I found in a dictionary is An element, feature, or factor that is liable to vary or change. But what does that mean. Think of a variable being an empty box that you can place things in. Because there can many different boxes we identify them by giving them names. So now I can differentiate box1 from box2.
There are different types of variables:
a)String variable – which contains words
b)Numeric variable – which contains numbers (Real, Whole, etc)
All variables should be named in a way that you would get a good understanding of what it is. For example a variable for a persons name could be namePerson1
Notice the variable starts with a small letter, but to make it easier to read you can use capitals at the beginning of other words.
When creating a variable you cannot:
- Include periods or punctuation that will cause the program to have a syntax error (only use letters, numbers and underscores)
- Do not use key words
String variables:
When we want a variable (var1) to hold a name (say like “George”) we assign the variable George. In small basic this looks like this:
var1= “George”
But if we were to get if from user input we would use
var1 = TextWindow.Read()
The user would enter George and we would have the same result as above. The question is…… why didn’t the user have to put quotes around the name George like we did initially? This is because the function returns the entire name within brackets and puts it in the variable as if it had quotes around it.
What would happen if we didn’t use quotes? Well if you could get the statement to “run” you would see that the variable only contains the letter G. This is because characters (each letter) is considered an individual thing and is represented by an individual byte within a computer program and so we need to tell the computer that we want to keep these letters together.
Numeric Variables
Are a much simpler to understand, in the sense that we do not have to worry about quotes for them. As you remember we can count up to 255 using one byte, so the computer knows to use multiple numbers together to represent the number which you mean. If we use a number larger than 255 or a number that contains decimals the language automatically make concession for this.
Conditional Statements
We have already used conditional statements before to make decisions in our programs. In small basic we have the key words
If,Then, Else, and Endif
It is used :
If (predicate) Then
(consequent)
Else
(alternative)
EndIf
An example:
TextWindow.Write("Enter a number: ")
num = TextWindow.ReadNumber()
remainder = Math.Remainder(num, 2)
If (remainder = 0) Then
TextWindow.WriteLine("The number is Even")
Else
TextWindow.WriteLine("The number is Odd")
EndIf
Remember, the condition has to be “true” in order for it to take place.
Commenting
To make sure your logic is clear, you are required to make a comment about what you are trying to do prior to the bit of code that does it. This is often how we identify our variables (although they should be named well)
Branching
Small basic allows you to include a Goto statement by allowing you to place a label and then return to that label.
Although this is an option in this language, in most languages it is considered poor practise and therefore it can only be used if there is no other way around the obstacle.
An example of its use is below:
i = 1
start:
TextWindow.WriteLine(i)
i = i + 1
If (i < 25) Then
Goto start
EndIf