Page 34 - Touchcode_C6_Flipbook
P. 34
PERFORMING OPERATIONS ON VARIABLES
A variable is a storage area that holds data. Its data type defines its size, layout, range of
values, and the set of operations that can be performed on it.
Once you declare a variable with a specific data type, you can perform operations on it
using suitable operators.
Operator: It is a symbol which operates on a value assigned to a variable. Example: +,-, *, \.
Operand: It is a value on which the operator performs an operation.
Arithmetic Operators
Arithmetic operation combines two or more numeric expressions by using the arithmetic
operators. The arithmetic operators perform operations such as: addition, subtraction,
multiplication, division, and modulus. In the table given below, we are taking, value of
variable a= 10, and of b=30:
Operation Symbol Description Example
Addition + Adds one operand to the other a+b= 40
Subtraction - Subtracts second operand from the first a-b= -20
Multiplication Multiplies one operand by the second a*b= 300
Exponentiation Raises first operand to power of second a ** b = 10** 30
Division / Divides first operand by the second b/a= 3
Divides first operand by the second, removes
Floor Division // a // b = 0
decimal part
Modulus % Divides the first operand by the second, b%a= 0
and returns the remainder
Assignment Operators
An assignment operator assigns a new value to a variable.
In the table given below, we are taking, value of variable a=10, and b=30.
Operator Symbol Description Example
Simple assignment = Assigns value to the left c = a + b (c = 10 + 30)
operator operand from right operand. will assign the value of
c to a + b (c = 40)
Add AND assignment += Adds the right operand to the b + = a (b + = 10) is
operator left operand and assign the equivalent to b = b + a
result to the left operand. (b = 30 + 10) (b = 40)
Subtract AND -= Subtracts the right operand b – = a (b– = 10) is
assignment operator from the left operand and equivalent to b = b – a
assigns the result to the left (b=30-10) (b=20)
operand.
32 Touchcode-VI

