Exercises
-
Assignments:
When writing a program we can store a value in different places in memory so that we can use them later when needed. The precise location in memory is chosen automatically by the computer. We need however to choose a label that points to that location.
Say we need to store an input value from the user, e.g.,31415. The first thing we do is choosing a label, sayx. Then we tell the computer that we will store a value on that memory position pointed to by that label. This is done using the keywordvarand the assignment symbol=in the following way.
Note the trailing semi-colonvar x = 31415 ;;.We can also assign a literal string to a variable. This is simply a string of characters, including whitespaces and what not, that we tell the computer to keep it exactly as we write it. In order to do so, we use double quotation marks
". For instance, say we want to assign a variable calledschoolthe value given by the stringDragon. We would do that in the following way:
We can also use single quotation marksvar school = "Dragon" ;'.
but we cannot mix single and double quotation marks in the same stringvar school = 'Dragon' ;var school = "Dragon' ; //THIS is WRONGVocabulary:-
We call those labels
x / schoolvariables. This so because we can always reassign 'x' a new value, say 23 (actually we would be reassigning a value to the memory location pointed to by 'x', but it's common to treat the label as if it were the memory location). So the precise value stored in the memory position denoted by 'x' could be 31415 at the beginning of our code, but somewhere towards the end the value stored could change to 23, without necessarily changing the label. - A string is a piece of text that we want the computer to process exactly as we have type it. This means we don't want the computer to worry about the characters we type in. We will see more about what that entails in the examples below.
- Write the code that assigns the value 31415 to a variable
x:
- Write the code that assigns the value 10 to the variable
z:
- Write the code that assigns the value -10 (negative ten) to the variable
negZ:
- Write the code that assigns the value 2 to the power of -23 to the variable
inpZ:
- Write the code that assigns one million times the value of variable invZ, i.e, the product of inpZ times 1000000, to the variable
nearOne:
- Write the code that assigns the value "exercise" to the variable
work:
- Write the code that assigns the value 'this is also a string!' to the variable
mindTheSpaces:
- Write the code that assigns the value 'Dragon' to the variable
school:
- We can also append text to a given string by using the
+sign, kind of like summing them. For example, the following code
is the same as if we would dovar school = "Dragon" + "Academy" ;
Notice there is no space between "Dragon" and "Academy" as we forgot to include a space after the 'n' (or one before the 'A').var school = "DragonAcademy" ;
We can also use the+sign with variables that contain strings:
In this example, the variablevar type = "High School" ; var school = "Dragon Academy" ; var fullName = type + school ;fullNamecontains the string literal"High SchoolDragon Academy". Notice there is something weird there: we missed a space between 'School' and 'Dragon'! Here is how we can do it so that the space gets included:
The change is in the last statement: we add 3 strings together at once: the string contained in the variablevar type = "High School" ; var school = "Dragon Academy" ; var fullName = type + " " + school ;type, the white-space literal given by" "and the string contained in the variableschool.
Write the code that assigns the value 'Keanu' to the variablegivenName:
- Write the code that assigns the value 'Reeves' to the variable
familyName:
- Write the code that assigns the value 'Jonny Mnemonic' to the variable
alias:
- Now use these last two variables to write the code that assigns the value 'Keanu Reeves aka. Jonny Mnemonic'
to the variable
fullName:
-
We call those labels