<< Chapter < Page | Chapter >> Page > |
Examples:
\n : move to the next line
\t : move to the next tab
The bool Data Type
The C++ bool type can have two states expressed by the built-in constants true (which converts to an integral one) and false (which converts to an integral zero). All three names are keywords. This data type is most useful when a program must examine a specific condition and, as a result of the condition being either true or false, take a prescribed course of action.
Most programs perform arithmetic calculations. Arithmetic operators are used to perform mathematical calculations, such as addition, subtraction, multiplication, and division in C++.
A simple arithmetic expression consists of an arithmetic operator connecting two operands in the form:
operand operator operand
Examples:
3 + 7
18 – 3
12.62 + 9.8
12.6/2.0
Example
#include<iostream.h>
int main()
{
cout<<"15.0 plus 2.0 equals "<<(15.0 + 2.0)<<'\n'
<<"15.0 minus 2.0 equals "<<(15.0 - 2.0)<<'\n'
<<"15.0 times 2.0 equals "<<(15.0 * 2.0)<<'\n'
<<"15.0 divided by 2.0 equals "<<(15.0 / 2.0)<<'\n';
return 0;
}
The output of the above program:
15.0 plus 2.0 equals 17
15.0 minus 2.0 equals 13
15.0 times 2.0 equals 30
15.0 divided by 2.0 equals 7.5
The division of two integers yields integer result. Thus the value of 15/2 is 7.
Modulus % operator produces the remainder of an integer division.
Example:
9%4 is 1
17%3 is 2
14%2 is 0
Expressions containing multiple operators are evaluated by the priority, or precedence, of the operators.
Operator precedence defines the order in which an expression evaluates when several different operators are present. C++ have specific rules to determine the order of evaluation. The easiest to remember is that multiplication and division happen before addition and subtraction.
The following table lists both precedence and associativity of the operators.
Example: Let us use the precedence rules to evaluate an expression containing operators of different precedence, such as 8 + 5*7%2*4. Because the multiplication and modulus operators have a higher precedence than the addition operator, these two operations are evaluated first (P2), using their left-to-right associativity, before the addition is evaluated (P3). Thus, the complete expression is evaluated as:
An expression is any combination of operators and operands that can be evaluated to yield a value. An expression that contains only integer values as operands is called an integer expression, and the result of the expression is an integer value. Similarly, an expression containing only floating-point values (single and double precision) as operands is called a floating-point expression, and the result of the expression is a floating point value (the term real expression is also used).
One of the most important aspects of programming is storing and manipulating the values stored in variables . A variable is simply a name chosen by the programmer that is used to refer to computer storage locations. The term variable is used because the value stored in the variable can change, or vary.
Notification Switch
Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?