Grade11 Short Note
Grade11 Short Note
2. Problem solving skill: skills on how to solve real world problem and represent the
solution in understandable format
. C++ is case sensitive. Small letter and capital letters are different
for C++
E.g: variable Age is not identical with variable age.
. Another rule that you have consider when inventing your own
identifiers is that they can not match any keyword of the C++
language nor your compiler’s specific ones, which are reserved
keywords. The standard reserved keywords are :
constants
A constant is any expression that has a fixed value.
. Like variables, constants are data storage locations in the
computer memory. But, constants, unlike variables their
content cannot be changed after the declaration.
. C++ provide two types of constants: literal and symbolic
constants.
. Literal constants: is a value typed directly in to the program
wherever it is needed.
int num=43;
43 is a literal constant in this statement
Cont--------------------------
. Symbolic constant:- is a constant that is represented by a
name, similar to that of variables. But unlike a variable, its
value can’t be changed after initialization.We have two ways to
declare symbolic constant. These are using #define and the
const keyword.
Example :-
Cont--------------------------------------------------------------------------
1. #include<iostream.h> 2. #include<iostream.h>
int main() { #define x 9.5
sum=X+Y; sum=x+y;
Cout<<sum; cout<<sum;
return 0; return 0;
} }
Cont-------------------------------------------------------------
3. #include<iostream.h>
int main() {
float X=3.5;
float y=7.4;
X=7.8;
float sum;
sum=X+Y;
cout<<sum;
return 0;
}
Operators
. An operator is a symbol that makes the machine to take an action.
.different operators act on one or more operands and can also have
different kinds of operators.
. C++ provide several categories of operands:
Assignment operator
Arithmetic operator
Relational operator
Increment/decrement operator
Logical operator
Conditional operator
Comma operator
The sizeof operator
1. Assignment operator(=)
The assignment operator assign a value to a variable .
The assignment operator always takes place from right to left,
and never the other way round.
X=5;
This statement assigns the integer value 5 to the variable x.
x=y;
This statement assigns to variable x the value contained in
variable y. The value of x at the moment this statement is
executed is lost and replaced by the value of y.
Example :-
#include<iostream.h>
int main(){
int a, b;
a=10 ;
b=4;
a=b;
cout<<“a:”<<endl;
cout<<a<<endl;
cout<<“b:”<<endl;
. Assignment operators are expression that can be evaluated. That
means that the assignment itself a value. For example
y= 2+(x=5);
In this expression, y is assigned the result of adding 2 and the value of
another assignment expression (which has itself a value of 5).it is
roughly equivalent to:
x=5;
y=2+x;
With the final result of assigning 7 to y.
The following expression is also valid in c++.
x=y=z=5;
It assigns 5 to the all three variables : x, y and z; always from right to
left.
2. Arithmetic operators(+, -, *, /, %)
The five arithmetic operation supported by C++ are:
Operator description
+ addition
_ subtraction
* multiplication
/ division
% modulo
Operations of addition, subtraction, multiplication and division
corresponds literally to their respective mathematical
operators. the last one, modulo operator, represented by a
percentage sign(%), gives the remainder of a division of two
values. For example
x=11%3;
results in variable x containing the value of 2, since dividing
11 by 3 result in 3, with the remainder of 2.
3.Compund assignment operator(+=, -*, /=, %=, >>=, <<=)
Expression equivalent to
y+=x; y=y+x;
x-=5; x=x-5;
x/=y; x=x/y;
price*=units+1; price=price*( units+1);
Example :
#include<iostream.h>
int main()
{
int a, b=3;
a=b;
a+=2; // equivalents to a=a+2;
cout<<a ;
return 0;
}
4. Increment/decrement operator (++, --)
The increase operator (++) and the decrease
operator(--)increase and decrease by one the value stored in
a variable. They are equivalent to += and -=,respectively.
Example 1. x=3;
y=++x; //x contains 4, y contains 4
Example 2. x=3;
y=x++; // x contains 4, y contains 3
5. Relational operator(==, !=, >, <, >=, <=)
Operator description
== equal to
!= not equal to
> Greater than
< less than
>= greater than or equal to
<= less than or equal to
Example :- suppose that a=2, b=3 and c=6, then:
1. (a==5) //evaluates to false, since a is not equal to 5
2. (a*b>c) // evaluates to true, since (2*3>=6) is true
3. (b+4>a*c) // evaluates false, since (3+4>2*6) is false
4. ((b=2)==a) // evaluates to true
6. Logical operator (!,&&,||)
. The operator ! is the C++ operator for the Boolean operation
NOT.it has only one operands, to its right ,and inverts it, producing
false if its operands is true, and true if its operands is false.
For example
!(5==5) // evaluates to false
!(6<=4) // evaluates to true
!(true) // evaluates to false
!(false) // evaluates to true
. The logical operator && and || are used when evaluating two
expressions to obtain a single relational result. The operator &&
corresponds to the Boolean logical operation AND which yields true if
both it operands are true, and false otherwise.
&& operator (and)
a b a&&b
true true true
true false false
false true false
false false false
The operator || corresponds to the Boolean logical operation
OR, which yields true if either of it operands are true, thus
being false only when both operands are false.
|| operator (or)
a b a||b
true true true
true false true
false true true
false false false
For example:
1. ((5==5)&& (3>6)) //evaluates to false(true && false)
2. ((5==5)||(3>6)) // evaluate to true ( true||false)
7. Comma operator(,)
The comma operator (,) is used to separate two or more
expression that are included where only one expression is
expected. When the set of expression has to be evaluated for a
value .for example
a= (b=3, b+2);
would first assign the value 3 to b, and then assign b+2 to
variable a. so at the end, variable a would contain the value 5
while variable b would contain value 3.
8. sizeof
This operator accepts one parameter, which can be either a
type or a variable, and returns the size in bytes of that type .
x= sizeof (char);
Here , is assigned the value 1, because char is a type with a
size of one byte.