![]() |
|
|
Understanding the Different Types of Variables/
Learn the Basics
Data
Types (variables)
The
Terminology
Declaring
Data Types and Using the Various Operators
Rules
for Naming Data Types
C++
Libraries
Declaring Data Types and Using the Various Operators To declare a variable in C++, use the following syntax: (data type) (variable
name) = (assignment); For example, int PhoneNumber = 1234567; The above example will declare the variable PhoneNumber as an integer and assign to it 1234567. Local variables are not initialized by default, so if you do not assign anything to them they will simply store what is known as garbage. For example: int PhoneNumber; might store something like 80173694 in PhoneNumber. Oh, and never forget to include a semicolon at the end of each line in C++. The compiler removes any spaces when compiling, therefore, semicolons tell it where lines end. Various arithmetic operators can be used with integers or doubles in C++. These include: +,-,/,*,%. The last one is known as modulus. What this operator does is that it finds the remainder of two integers by multiplying the first by the second. Don't worry, we won't be covering this in this course. Here is an example of using arithmetic operators:
int number1=2; answer in this case is 5. Other than arithmetic operators, there are operators that do various other functions in C++. The most important two are discussed below: >> Input Operator << Insertion Operator In C++, input and output are controlled using the keywords cin and cout, respectively. Input occurs when a user, say, enters a number from the keyword, and output is what you see on the screen. These two keywords along with the operators above hold the responsibility of getting values from the user and then displaying others on screen. If we were to write a program that would get the user's age and then display our own age, here is what a segment from the program would look like:
int userage=0; As you can see, the variables are declared in the beginning. Even though we're going to input a value for userage, it is always considered good programming to declared integers as 0. The third line displays the message between the quotes. The cout keyword is used with the Insertion Operator to display something on screen. The fourth line uses cin, which places the value entered by the user on the previous line in the variable userage. This is all it takes to obtain values from users. The fifth and sixth lines simply display both ages. Notice that the Insertion Operator is used to separate the different parts or sections of each of the two lines. If we were to enter a message after the userage on the fifth line, this is how the line would look: cout << "You are " << userage << " years old"; The final line of the code displays a blank line by using the endl function. Comparison operators are used to compare values:
Note that '==' is used for comparing two values and not '='. The reason is because '=' is used to transfer the value of the variable on the right to the variable on the left, whereas '==' is used only for comparison. |