Variables are a memory space which we assign a name and where we can store data.
Our first example program was able to write "Hello". The second one allowed us to add two preset numbers. But this situation is not "usual" in real world: most times we will manipulate data that the user has typed or which have been obtained in previous calculations.
That's why we need variables, memory areas where the data and temporary results will be saved. As a first example, let's see how we would sum two integer numbers preset in the program.
1.5.1. Definition of variables: integers
To use a variable, we must declare it first: we will assign it a name, and we will indicate the data type if will be, too.
The first data type we will use is an "integer number" (no decimal places), which is declared using the word "int":
int firstNumber;
This command reserves space enough to store an integer number, which may hold different values, and which we refer to with the name "firstNumber."
1.5.2. Assigning values
We can give a value to that variable in our program:
firstNumber = 234;
Or we can give it an starting value ("initialize it") before the program begins, when we declare it:
int firstNumber = 234;
Or we can even declare and initialize more than one variable at a time
int firstNumber = 234, secondNumber = 567;
(This line reserves space for two variables, which we will use to store integer numbers; one of them is called firstNumber and has a starting value of234 and the other variable is called secondNumber and has 567 as starting value).
After that, we can make operations with variables, just as we did with the numbers::
sum = firstNumber + secondNumber;
1.5.3. Showing the value of a variable on screen
We know how to display a number or the result of a simple operation on screen:
System.Console.WriteLine(3+4);
but the syntax is very similar for a variable:
System.Console.WriteLine(sum);
Also, if we want to display text and also the value of the variable, we can indicate it in quotes, specifying {0} where we want the value of the variable to be displayed:
System.Console.WriteLine("The sum is {0}", sum);
If we want to display more than one variable, we will place them after the text and we will specify where each one of them must be shown, using {0}, {1} and so on:
System.Console.WriteLine("The sum of {0} and {1} is {2}",
firstNumber, secondNumber, sum);
We know enough to create a program which can sum two numbers using variables:
public class Example02
{
public static void Main()
{
int firstNumber;
int secondNumber;
int sum;
firstNumber = 234;
secondNumber = 567;
sum = firstNumber + secondNumber;
System.Console.WriteLine("The sum of {0} and {1} is {2}",
primerNumero, segundoNumero, sum);
}
}
Let's review what it does:
- (We still skip the details of what "public", "class", "static" and "void" mean).
- Main() indicates where the body of the program begins, and the body of the program is delimited between curly braces ( "{" and "}" )
- int firstNumber, reserves space for an integer number, which we will refer to as "firstNumber".
- int secondNumber, prepares space for another integer number, which we will call "secondNumber".
- int sum; reserves space to store a third integer, called "sum".
- firstNumber = 234, sets the value for the first number to be added.
- secondNumber = 567, sets the value for the second number.
- sum = firstNumber + secondNumber, calculates the sum of those two numbers and stores the result in another variable, instead of displaying it directly on screen.
- System.Console.WriteLine ("The sum of {0} and {1} is {2}", firstNumber, secondNumber, sum), displays the text and the values of these three variables (the two starting numbers and their sum).
Suggested exercise: Create a program to calculate the product of the numbers 121 and 132, using variables.
No comments:
Post a Comment