6 January 2012

Basic terms before learning coding


Compound Statements

Several statements can be grouped together as a compound statement, which begins
with an opening brace { and ends with a closing brace }. A compound statement can
appear anywhere a single statement could.

Although every statement in a compound statement must end with a semicolon, the
compound statement itself does not end with a semicolon. Here’s an example:
{
temp = a;
a = b;
b = temp;
}

This compound statement swaps the values in the variables a and b using a variable
named temp as a temporary holding place for one value.

Expressions

An expression is any part of a statement that returns a value, as in this simple example:
x = y + 13;

This statement makes the variable x equal to the variable y plus 13. So, if y equals
20, x equals 33. The entire statement also returns the final value of x, so it’s also an
expression. To understand this better, consider a more complex statement:
z = x = y + 13;

This statement consists of three expressions:
 The expression y 
+ 13 is stored in the variable x.

. The expression x
able z.
. The expression z



= y + 13 returns the value of x, which is stored in the vari-


= x = y + 13 returns the value of z, which is not stored.


The assignment operator = causes the operand on the left side of the operator to have
its value changed to the value on the right side of the operator.
Operand is a mathematical term referring to the part of an expression operated upon
by an operator.







The Full Text of Expression.cpp


#include <iostream>
int main()
{
int x = 0, y = 72, z = 0;
std::cout << “Before\n\nx: “ << x << “ y: “ << y;
std::cout << “ z: “ << z << “\n\n”;
z = x = y + 13;
std::cout << “After\n\nx: “ << x << “ y: “ << y;
std::cout << “ z: “ << z << “\n”;
return 0;
}

 This program produces the following output:
Before
x: 0 y: 72 z: 0

After
x: 85 y: 72 z: 85
Three variables are declared and given initial values, which are displayed on lines
5–6. In line 7, expressions assign values to x and z, in that order. The new values are
displayed in lines 8–9.


 Operators

An operator is a symbol that causes the compiler to take an action such as assigning
a value or performing multiplication, division, or another mathematical operation.

Assignment Operator
An expression consists of an assignment operator, an operand to its left called an
l-value, and an operand to its right called an r-value. In the expression grade = 95,
the l-value is grade, and the r-value is 95.

Constants are r-values but cannot be l-values. The expression 95 = grade is not per-
mitted in C++ because the constant 95 cannot be assigned a new value.

The primary reason to learn the terms l-value and r-value is because they may
appear in compiler error messages.


Mathematical Operators
There are five mathematical operators: addition (+), subtraction (-), multiplication
(*), division (/), and modulus (%). C++, like C, does not have an exponentiation oper-
ator to raise a value to a specified power. There is a function to perform the task.
Addition, subtraction, and multiplication act as you’d expect, but division is more
complex.

Integer division differs from ordinary division. When you divide 21 by 4, the result is
a real number that has a fraction or decimal value. By contrast, integer division pro-
duces only integers, so the remainder is dropped. The value returned by 21 / 4 is 5.

The modulus operator % returns the remainder value of integer division, so 21 % 4
equals 1. The integer division 21 / 4 is 5, leaving a remainder of 1.

Finding the modulus can be useful in programming. If you want to display a state-
ment every 10th time that a task is performed, the expression taskCount % 10 can
watch for this. The modulus ranges in value from 0 to 10. Every time it equals 0, the
count of tasks is a multiple of 10.

Floating-point division is comparable to ordinary division. The expression 21 / 4.0
equals 5.25.
 C++ decides which division to perform based on the type of the operands. If at least
one operand is a floating-point variable or literal, the division is floating point. Oth-
erwise, it is integer division.

0 comments:

Post a Comment