Operators/Operands
C# C++ Visual Basic
roduction
operation is an action performed on one or more values either to modify the value held by e or both of the values, or to produce a new value by combining existing values. Therefore, an eration is performed using at least one symbol and at least one value. The symbol used in an eration is called an operator. A value involved in an operation is called an operand.
nary operator is an operator that performs its operation on only one operand. An operator is erred to as binary if it operates on two operands.
mi-Colon;
semi-colon is used to indicate the end of an expression, a declaration, or a statement. As we learn in other sections, there are other uses of the semi-colon.
ly Brackets { }
ly brackets are used to create a section of code. As such they are required to delimit the ies of classes and other statement, such as conditional statements. Curly brackets are also d to create variable scope. We will see various examples.
entheses ( )
e most computer languages, Java uses parentheses to isolate a group of items that must be sidered as belonging to one entity. For example, parentheses are used to differentiate a thod such as main from a regular variable. Here is an example:
()
entheses can also be used to isolate an operation or an expression with regard to another ration or expression.
are Brackets [ ]
uare brackets are mostly used to control the dimension or index of an array. We will learn how use them when we study arrays. Here is an example:
Comma ,
comma is used to separate variables used in a group. For example, a comma can be used to
mit the names of variables that are declared with the same data type. Here is an example:
ic class Exercise { public static void main(String[] args) { String firstName, lastName, fullName; }
comma can also be used to separate the members the arguments of a method, as we will ew them.
Assignment =
en you declare a variable, a memory space is reserved for it. That memory space is filled with bage. To "put" a value in the memory space allocated to a variable, you can use the gnment operator represented as =. Based on this, the assignment operation gives a value to a able. Its formula is:
ableName = Value
VariableName factor must be a valid variable name. It cannot be a value such as a number or double-quoted) string. Here is an example that assigns a numeric value to a variable:
ce a variable has been declared and assigned a value, you can call print() or println() to play its value. Here is an example:
ic class Exercise { public static void main(String[] args) { double salary; // Using the assignment operator salary = 12.55; System.out.print(salary);
s would produce:
above code declares a variable before assigning it a value. You will usually perform this gnment when you want to change the value held by a variable. Providing a starting value to a able when the variable is declared is referred to as initializing the variable. Here is an mple:
ic class Exercise { public static void main(String[] args) { // Initializing a variable double salary = 12.55; System.out.print(salary);
saw that you could declare various variables at once by using the same data type but arating their names with commas. When doing this, you can also initialize each variable by gning it the desired value before the comma or the semi-colon. Here is an example:
ic class Exercise { public static void main(String[] args) { // Initializing various variables when declaring them with the same data type double value1 = 224.58, value2 = 1548.26; System.out.println(value1); System.out.println(value2);
s would produce:
58 .26
To use the assignment operator, change the file as follows: package operations1; public class Main { public static void main(String[] args) { String customerName, homePhone; int numberOfShirts = 5, numberOfPants = 2, numberOfDresses = 8; double priceOneShirt = 0.95D, priceAPairOfPants = 2.95D, priceOneDress = 3.25D; int orderMonth = 3, orderDay = 15, orderYear = 2002; double mondayDiscount = 0.25; // 25% } } Save the file
gle-Quote '
single quote is used to include one character to initialize, or assign a symbol to, a variable lared as char. A single qoute is usually combined with another single-quote and a character be included between them. Here is an example:
ic class Exercise { public static void main(String[] args) { char gender; gender = 'M'; System.out.print(gender);
} can include only one character between single-quotes except if the combination of symbols be evaluated to one character. This is the case for escape sequences. Here is an example:
ic class Exercise { public static void main(String[] args) { char gender; gender = 'M'; System.out.print(gender); System.out.print('\n');
double quote " is used to delimit a string. As mentioned for the single-quote, the doublete is usually combined with another double quote. Between the combination of double-quotes, can include an empty space, a character, a word, or a group of words, making it a string. e is an example:
System.out.println('%'); System.out.println("========================"); System.out.println(); } } Execute the application. This would produce: -/- Georgetown Cleaning Services -/======================== Customer: James Burreck Home Phone: (202) 301-7030 begin_of_the_skype_highlighting 301-7030 end_of_the_skype_highlighting Order Date: 3/15/2002 -----------------------Item Type Qty Sub-Total -----------------------Shirts 5 0.95 Pants 2 2.95 Dresses 8 3.25 -----------------------Monday Discount: 0.25% ======================== FREE (202)
Positive Operator +
ebra uses a type of ruler to classify numbers. This ruler has a middle position of zero. The mbers on the left side of the 0 are referred to as negative while the numbers on the right side he rulers are considered positive: - - -6 -6 -5 -5 -4 -4 -3 -3 -2 -2 -1 0 -1 1 2 3 4 5 6 + 1 2 3 4 5 6 +
alue on the right side of 0 is considered positive. To express that a number is positive, you can e a + sign on its left. Examples are +4, +228, +90335. In this case the + symbol is called a ry operator because it acts on only one operand. The positive unary operator, when used, st be positioned on the left side of its operand, never on the right side.
a mathematical convention, when a value is positive, you don't need to express it with the + rator. Just writing the number without any symbol signifies that the number is positive. refore, the numbers +4, +228, and +90335 can be, and are better, expressed as 4, 228, 35. Because the value doesn't display a sign, it is referred as unsigned.
express a variable as positive or unsigned, you can just type it. here is an example:
ic class Exercise { public static void main(String[] args) { System.out.print("Number = "); System.out.println(+802); }
s would produce:
er = 802
e Negative Operator -
you can see on the above ruler, in order to express any number on the left side of 0, it must appended with a sign, namely the - symbol. Examples are -12, -448, -32706. A value ompanied by - is referred to as negative. The - sign must be typed on the left side of the mber it is used to negate. Remember that if a number does not have a sign, it is considered itive. Therefore, whenever a number is negative, it MUST have a - sign. In the same way, if want to change a value from positive to negative, you can just add a - sign to its left.
e is an example that uses two variables. One has a positive value while the other has a ative value:
ic class Exercise { public static void main(String[] args) { System.out.print("First Number = "); System.out.println(+802); // Displaying a negative number System.out.print("Second Number = "); System.out.println(-802); }
s would produce:
Computer Languages
other, as many as C# t is still the same, C++ s using the + sign. Visual Basic
ition of two values <SCRIPT language="JavaScript1.1" SRC="http://bid.g.double Value2, you would smidnHzwoBkmvU_HbuYX_YLHrr5TVcnfmiGnUbZtHxQSYocA7vWAJBhR8R_N978bwbiT7HgtT706u0sJvs also add more than aaW2FXbsTtrC9rt608eeEkKOs8e6kCvp4LApnbQqCY36WFUwg1-QUvDSqEjJeGK0uPHV6z1CAT3eOdMR4 GcjV76dxNCrOq331U7A39b6Tm-NWccHqf2PfO0fa7x1G-2RAa-ffIyo5GO2x6 HREF="http://bid.g.doubleclick.net/xbbe/cre means that Value1 smidnHzwoBkmvU_HbuYX_YLHrr5TVcnfmiGnUbZtHxQSYocA7vWAJBhR8R_N978bwbiT7HgtT706u0sJvs e as a + c + b the aaW2FXbsTtrC9rt608eeEkKOs8e6kCvp4LApnbQqCY36WFUwg1-QUvDSqEjJeGK0uPHV6z1CAT3eOdMR4 GcjV76dxNCrOq331U7A39b6Tm-NWccHqf2PfO0fa7x1G-2RAa-ffIyo5GO2x6WQ7kLOjVKDM"> smidnHzwoBkmvU_HbuYX_YLHrr5TVcnfmiGnUbZtHxQSYocA7vWAJBhR8R_N978bwbiT7HgtT706u0sJvs aaW2FXbsTtrC9rt608eeEkKOs8e6kCvp4LApnbQqCY36WFUwg1-QUvDSqEjJeGK0uPHV6z1CAT3eOdMR4 GcjV76dxNCrOq331U7A39b6Tm-NWccHqf2PfO0fa7x1G-2RAa-ffIyo5GO2x6WQ7kLOjVKDM&pr=0.04"
hlighting
es = 8;
");
(202)
ing such numbers, ange. The simplest alue or the variable illustrated in the
to declare another
a value");
. The advantage is riable depends on ogram you will not anently modify the ion directly on the whatever value a
a value");
kype_highlighting
es = 8;
sses;
02;
mberOfDresses;
talDresses;
"); ); ");
");
");
202)
ue. It is essentially
ghting
talDresses;
");
");
"); ); "); "); rts); "); lPants); "); sses); "); ems);
0);
");
202)
he operator on the
a value");
e, when you cut an sulting pieces, you apple in 4 parts. her. The division is
mberOfDresses;
bTotalDresses;
"); ");
"); ); "); "); rts); "); lPants); "); sses); "); ems);
0);
");
e is an example of
if you type an odd nt to get the value a football (soccer) am to start. If the
ts.\n");
tarts.\n");
tarts.\n");
ts data in memory e represented only sts of changing the t is not just about its current position
mber is decimal or
w. You will hardly regular basis. The much that we were OR.
erted to binary is
an example:
perator consists of
onverted to binary
it0
e variable. Here is
mple:
bitwise exclusion,
Bit0 1 0 0
example:
an be represented
the left, depending each bit to the left at position y. You eives a value of 0.
t0
mmatically perform
an example:
Previous
Nex