0% found this document useful (0 votes)
3 views5 pages

C++ Operators Week 4

The document provides an overview of C++ operators, categorizing them into arithmetic, assignment, comparison, and logical operators. It includes examples of how to use these operators in programs, demonstrating their functionality and differences, such as the distinction between assignment and equality operators. Additionally, it covers compound assignment operators and their effects on variable values.

Uploaded by

nutrireadsurppot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

C++ Operators Week 4

The document provides an overview of C++ operators, categorizing them into arithmetic, assignment, comparison, and logical operators. It includes examples of how to use these operators in programs, demonstrating their functionality and differences, such as the distinction between assignment and equality operators. Additionally, it covers compound assignment operators and their effects on variable values.

Uploaded by

nutrireadsurppot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C++ Operators Week 4

 Operators are used to performing operations on variables and values.


 C++ divides the operators into the following groups:
I. Arithmetic operators
II. Assignment operators
III. Comparison operators
IV. Logical operators
Arithmetic Operators
+ Addition Adds together two values x+y

- Subtraction Subtracts one value from x-y


another
* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y


% Modulus Returns the division remainder x%y

++ Increment Increases the value of a ++x


variable by 1
-- Decrement Decreases the value of a --x
variable by 1

// a program that performs the normal computation


#include <iostream>
using namespace std;

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

cout << "The value in variable a is:"<<a<<endl;


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;
}

Note the following operators:


 The % operator returns the reminder after dividing 2 values.
For example; int b=14;

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

// a program that demonstrate the modulus, increment and decrement operators


#include <iostream>
using namespace std;

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

b = 67%2; // variable b stores the reminder of dividing 67 by 2


c = ++a; // variable c stores value stored in variable a but after adding 1 to it
d=--d; //variable d values changes to 7 as it is decremented by 1

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

cout << "The current value in variable a is:"<<endl;


cout << a<<endl;
cout << " The current value stored in variable b is:"<<endl;
cout << b;
return 0;
}

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

Compound assignment operators


 Compound assignment operators modify the current value of a variable by performing
an operation on it.
 They are equivalent to assigning the result of an operation to the first operand. They
include; +=, -=,*=,/=

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

cout << "The initial value in variable a is:"<<endl;


cout << a<<endl;
a+=5;
cout << "The current value in variable a is:"<<endl;
cout << a<<endl;

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)

Operator Name Example

== 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

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);// 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

Operator Name Description Example


&& Logical and Returns true if both statements x < 5 && y < 10
are true
|| Logical or Returns true if one of the x < 5 || y < 4
statements is true
! Logical not Reverse the result, and returns !(x < 5 && y < 10
false if the result is true

 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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy