Next freebie in 28 Hours*
Developers use number variables to save and store information (numbers) that they want to access and reuse in the future. Learn how to create different kinds of number variables and see how they can make your life easier as your code gets more complex.
We discussed strings as variables earlier. We all know how to declare a string variable and how to assign data to that variable. The method of declaring and assigning a Number variable is the same. The only thing is we use a different keyword here during declaration: 10 guesses what! If you said "Number," you win the booby prize. This will ensure that the variable is of type Integer (Number) and can take data that is in the form of digit(s) that are of type Number.
When it comes to assigning data to the variable, we can assign only what it takes. So data assigned in form of digits is easily understood by the application and it compiles easily when needed. But when we assign data of type string to this variable, the application causes an error (pukes, remember?) at the time of compilation. The application cannot recognize a string for that variable because it was declared to be Number. Therefore, the compiler wants the data contained by the variable to be of type Number. Any data type other than integer (Number) is considered as wrong input from the user and therefore causes an error. While dealing with numbers, another area that needs to be looked into is the correct syntax. We see that the syntax for dealing with variables is the same for both strings and numbers, but it isn't exactly the same. When it comes to a Number, there is a small change: no single or double quotes here while they are compulsory for a String.
Here is an example of declaring a Number variable:
var numNutts: i
Now you have an integer variable all ready to store your numbers! Easy as that.
Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.