C++ Operators Week 4
C++ Operators Week 4
int main ()
{
int a, b,c,d; // declare variable a , b,c,d of type int
a = 30+20; // variable a stores the sum of 30 and 20
b = 30-10; // variable b stores the balance of 30 minus 10
c = 30*2; // variable c stores the product of 30 and 2
d=30/2; //variable d stores the results of dividing 30 by 2
1
int r=b%3; variable r will hold(2), the reminder after dividing 14 by 3
++ increments a variable value by 1.
-- Decrements a variable value by 1.
b++ is the same as b=b+1
b--is same as b=b-1
The increment and decrement operators can be placed either before the variable or
after. For example b++ or ++b
int main ()
{
int a, b,c,d; // declare variable a , b,c,d of type int
d=8;
a = 30; // variable a stores 30
cout << "The value in variable a is:"<<a<<endl;// value of variable a (30) before any
increment
cout << "The value in variable a is:"<<a<<endl;// value of variable a(31) after a was
incremented by 1
cout << “The value in variable b is:”<<b<<endl;
cout << " The value in variable c is:"<<c<<endl;
cout << “The value in variable d is:”<<d<<endl;
return 0;
}
Assignment Operators
Assignment operators are used to assign values to variables.
We use the assignment operator (=) to assign a value to a variable.
For example ,
int x; // we declare a variable called x
int y;// we declare a variable called y;
x=10; // we assign or store value 10 in that variable
y=x; // we assign the valued currently held by the variable x to variable y
a program example
/note how variables are assigned their values and changes that occur
#include <iostream>
using namespace std;
int main ()
2
{
int a, b; // declare variable a and b of type int
a = 10; // assign value 10 to variable a
b = 4; // assign value 4 to variable b
a = b; // now assign the value stored in b to variable a
b = 7; // assign variable b value 10
NB : you can assign the same value to multiple variables in a single assignment statement
For example: a=b=c=d=15;// all the 4 variables will be assigned value 15
For example ;
x += 3 // same as x = x + 3, it adjust the current value of x by adding 3
x -= 3// same as x = x – 3, it adjust the current value of x by subtracting 3
x *= 3// same as x = x * 3, it adjust the current value of x by multiplying by 3
x /= 3 // same as x = x / 3, it adjust the current value of x by subdividing x by 3
so ,
int x=10; // store value 10 in variable x
x+=5; //change the value stored in x by adding 5 to the currently stored value(10),
x now stores 15
example in a program;
#include <iostream>
using namespace std;
int main ()
{
int a, b; // declare variable a and b of type int
a = 10; // assign value 10 to variable a
b = 4; // assign value 4 to variable b
3
cout << " The initial value stored in variable b is:"<<endl;
cout << b<<endl;
b*=3; // same as b=b*3,multplies the initial value of b(4) by 3,thus the new value
stored in b is 12
cout << "The current value in variable of b is:"<<endl;
cout << b<<endl;
a/=3;
//add the output statement here to show the current value of a
b-=6;
// add an output statement here to display the current value of b
return 0;
}
Comparison Operators
Comparison operators are used to compare two values.
Two expressions can be compared using relational and equality operators. For example,
to know if two values are equal or if one is greater than the other.
The result of such an operation is either true or false i.e Boolean value 0(false) or 1(true)
== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
NB:
The assignment operator (operator =, with one equal sign) is not the same as the equality
comparison operator (operator ==, with two equal signs); the first one (=) assigns the value
on the right-hand to the variable on its left, while the other (==) compares whether the values
on both sides of the operator are equal
#include <iostream>
using namespace std;
int main ()
{
int a, b,c,d; // declare variable a and b of type int
a = 10; // assign value 10 to variable a
b = 4; // assign value 4 to variable b
c=20;
d=10;
cout <<(a<b);// is a less than b? the outcome is false as 10 is not less than b
cout << (a>b);// is a greater than b? the outcome is true as 10 is greater than 4
cout << (a==d);// is a equal to be d? the outcome is true as 10 is equal to10
cout <<(a>=b);// is a greater than or equal to b? the outcome is true as 10 is greater than 4
4
cout << (a<=c); // is a less than or equal to c? the outcome is true as 10 is less than 20
cout << (a!=c);// a not equal to c? the outcome is true as they are not equal
return 0;
}
Logical Operators
Logical operators are used to determine the logic between variables or values
The logical operators && 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 its operands are true.
The operator || corresponds to the Boolean logical operation OR, which yields true if
either of its operands is true, thus being false only when both operands are false
The operator ! is the C++ operator for the Boolean operation NOT. It has only one
operand, to its right, and inverts it, producing false if its operand is true, and true if its
operand is false. Basically, it returns the opposite Boolean value of evaluating its
operand.
An example in a program
example using a program;
#include <iostream>
using namespace std;
int main ()
{
int a, b,c,d; // declare variable a and b of type int
a = 10; // assign value 10 to variable a
b = 4; // assign value 4 to variable b
c=20;
d=10;
cout << (a<b|| c>d);// the statement returns true as one of the condition is true(c>d)
cout << (a>b &&b<d);// it returns true as both conditions are true
cout <<(! a==c);// it returns true as the condition is false(a is not equal to c)
cout << (a>=b);// is a greater than or equal to b? the outcome is true as 10 is greater than 4
cout <<(c<=a); // is c less than or equal to a? the outcome is false as 20 is not less than 10
return 0;
}