PRB - Variables (Lesson)
Variables
Getting Specific
Now let's look at some specifics related to programming. Our first look is at the word variable. We examined this word in terms of math equations and the use of one letter variables.
Well in computer science we prefer to be more specific and explanatory.
Rather than x and y, we would prefer to use words like cost to describe what is happening. In computer science we write our code to allow others to understand what we have written easily.
sayingOne = AP CS uses collaboration and discovery techniques for learning
cost = nbr * nbrOfItems + 10
The variables: sayingOne, cost, nbr, nbrOfItems are all variables, just like x and y, but they are more descriptive of what is happening. This equation is still linear, just like y = mx + b, the standard equation of a line, but y, m , and x are more descriptive of the values being used and b is the constant 10. Everything works the same for calculating the math answers.
Let's look at one of the variables that I wrote: nbrOfItems. This type of writing is called camelCase. The rule for camelCase is for a variables and algorithms (procedures or organized instructions set together in a group; we will talk about these in another module), is to start with a lower case letter for the first portion and then for each word change capitalized the first letter. This algorithm, rule, is great for reading as your eyes catch quickly where each word starts and eyes and brain quickly decipher. Procedures (methods) will be discussed in a later module.
Adding Variable in Alice
Now let's examine how to add a variable in Alice and use the variable. We will begin with a string variable. Open your Alice and follow along with the video below.