You are on page 1of 1

IT01003!

OCTOBER 1, 2012

Lab 1: V ariables and Arithmetic Operations


NOTES:
Syntax: Variable Declarations type name [, name ]n ; e.g. int an_integer_variable; double a_real_variable; int one_int_variable, another_int_variable; Variables are placeholders for values, just like in algebra. In C++ you must declare a variable before you can use it. To declare it you must give it a name and a type. The types you will use the most in C++ are: int for integer numbers, both positive and negative such as 500, 2 and -20; char for single characters, such as d and !; double for decimal or real numbers, both positive and negative, such as 0.05, 3.141 and -2.718. Variable names can be as short as a single letter or very long indeed. It helps to give variables descriptive names to help you remember what they are for. A variable name, or identier, can contain any of the following characters: upper and lower case letters, a-z, A-Z; the digits 0-9; the underscore character . Variable names may not start with a digit. Variable names are case sensitive, that is, upper and lower case letters are treated as being different. Some examples of valid variable names are: distance_to_tokyo i anotherCounter Some examples of invalid variable names are 2me2you (it starts with a number) dot.dot (it contains an invalid character) Syntax: Mathematical Operators +-*/() The standard ones are: + (addition); - (subtraction); * (multiplication); / (division). When there are multiple operators on a line some basic rules are followed: operators are looked at by the C++ compiler from right-to-left and a special order is followed. This order is known as operator precedence and means that multiply and divide are considered more important than add and subtract. For example; 6+4/2 gives a result of 8. This is the same as the way that they are used in mathematics. Quite often you want to clarify what you mean, you can use parentheses (brackets), ( and ), for this purpose. For example (6 + 4) / 2 gives a result of 5

QUESTIONS: Declare two integer variables as; int x=4; int y=2; Write a program to calculate and display the; 1) Multiplication of the two numbers (x x y) 2) Addition of the two numbers (x + y) 3) Subtraction of the two numbers ( x - y) 4)Division of the two numbers (x / y)

PAGE 1

You might also like