Chapter 2: How to write a C++ program
C++ have three unique features that sets it apart from most of the programming languages:
      Expressions
      Pointers
      Classes
In this chapter we will discuss the very first of these concepts, i.e. Expressions. We have already used expressions in Mathematics, but the way to use expressions is unique to “C++” and its precursor “C”. The concept of expressions is tied to the concept of precedence, operators, statements and associativity.
2.1 Expressions
Expression, in a C++ program, is a sequence of operators and operands that eventually reduces to a single value. For example, 10 * 2.
In the following example, the expression reduces to 20. In C++, final value can be of any data type, other than void.
      Operators
Operators are the language specific syntactical tokens that require some action to be performed. Many operators are derived from the concepts of Mathematics. For example “Sign of Multiplication (*)” is an operator used in C++. It multiplies two numbers.
Every programming language has unique operators to perform unique operations.
      Operand
For any defined operator, there may be one or more than one operands. Operand has to receive any operator’s action. In above example, (10 * 2), Multiplier and Multiplicand are the operands of Multiplication.
There is no limit of operand sets and operators to form an expression. The only rule is that when program will evaluate the expression, the answer should be a single value, that may represent the expression.
      Primary Expressions in a C++ Program
In C++, most initiatory kind of expressions are Primary Expressions.It contains just one operand and no operator. We have to remember that operand can be a name, parenthetical expression or a constant in Primary Expression.
      Names
It is an identifier which defines a function, a variable or any other object in C++.
      Constants
Another type of primary expressions are Constants.Constants are the pre recognized or declared data whose value is unchangeable during the compilation and execution of a program.
      Parenthetical Expressions
Last kid of primary expression is parenthetical expression.It is a primary expression because its value is always reducible to a single value. So, the complex expression in a parenthetical expression may be bound to make it a primary expression.
      Binary Expressions in C++
Binary expressions in C++ are typically formed by operand-operator-operand relation. These expressions are the most common. Any two numbers subtracted, multiplied, divider or added are written with the operator between two operands. Or may be in algebraic expressions. Most common types or binary expressions are:
      Additive Expressions
First type of binary expressions is additive expressions. In this kind of expression, second operand is added to the first operand or the second operand is subtracted from the first operand. It depends upon the operator, that is used. These kinds of expressions use parallel algebraic notations for example, a + 18 and b - 90. Here are two sample programs to show such kind of expressions:
For Addition :
#include <iostream>
using namespace std;
int main()
{
// Declaration of integers
int firstinteger, secondinteger, sum;
// Printing input and output commands   
cout << "Enter any integer: ";
cin >> firstinteger;
cout << "Enter another integer: ";
cin >> secondinteger;
// Sum of two integers is stored in the variable "sum"
sum = firstinteger + secondinteger;
// Printing sum of first and second integer
cout << firstinteger << " + " <<  secondinteger << " = " << sum;    
return 0;
}
For Subtraction :
#include <iostream>
using namespace std;
int main()
{
// Declaration of integers
int firstinteger, secondinteger, sub;
// Printing input and output commands 
cout << "Enter any integer: ";
cin >> firstinteger;
cout << "Enter another integer: ";
cin >> secondinteger;
// Subtraction of second integer from first is stored in the variable "sub"
sub = firstinteger - secondinteger;
// Printing the subtraction  of second integer from first integer
cout << firstinteger << " - " <<  secondinteger << " = " << sub;    
return 0;
}
      Multiplicative Expressions
This expression is known as multiplicative expression because of its first operator, i.e. Multiplication. We consider it on top, in binary expressions. Its value is calculated as the product of two operands, i.e. 5 * 2 = 10.
In such expressions, division is a little more complex. In division if both operands are integers, the result would be the integral value of quotient. It would be expressed as an integer, i.e. 5 / 2 = 2. Here are two sample programs to show such kind of expressions:
For Multiplication :
#include <iostream>
using namespace std;
int main()
{
// Declaration of integers
int firstinteger, secondinteger, mul;
// Printing input and output commands   
cout << "Enter any integer: ";
cin >> firstinteger;
cout << "Enter another integer: ";
cin >> secondinteger;
// Product of two integers is stored in the variable "mul"
mul = firstinteger * secondinteger;
// Printing sum of first and second integer
cout << firstinteger << " * " <<  secondinteger << " = " << mul;    
return 0;
}
For Division :
#include <iostream>
using namespace std;
int main()
{
// Declaration of integers
int firstinteger, secondinteger, div;
// Printing input and output commands 
cout << "Enter any integer: ";
cin >> firstinteger;
cout << "Enter another integer: ";
cin >> secondinteger;
// Integral value of quotient in division of first integer and second integer is stored in the variable "div"
div = firstinteger / secondinteger;
// Printing the subtraction  of second integer from first intege r
cout << firstinteger << " / " <<  secondinteger << " = " << div;    
return 0;
}
      Assignment Expressions
Assignment expression is an expression which usually evaluates the operands on the right side of an equation and automatically places its value to the variable on the left side.There are two types of assignment expressions:
      Simple Assignment
Simple assignment is a form of assignment expressions which is present in the form of algebraic expressions such as x = 60 , y = n + 20 , z = x + y.
The thing to remember, in a simple assignment, is that the left operand should be a single variable. In such expressions, the value of the right side is evaluated and it becomes the value of the entire expression. In the following table there are some examples of simple assignment and how its value is calculated.
Expression
Value of “n”
Value of “m”
Value of Expression
Result of Expression
n = m -1
20
15
14
14
n = m + 20
20
15
35
35
n = m * 0
20
15
0
0