0% found this document useful (0 votes)
0 views7 pages

Lecture 03

The document provides an overview of various operators in C++, including arithmetic, assignment, comparison, bitwise, logical, and conditional operators, along with examples. It also covers string manipulation and mathematical functions available in C++ through the cmath library. Additionally, it includes example code snippets demonstrating the usage of these operators and functions.

Uploaded by

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

Lecture 03

The document provides an overview of various operators in C++, including arithmetic, assignment, comparison, bitwise, logical, and conditional operators, along with examples. It also covers string manipulation and mathematical functions available in C++ through the cmath library. Additionally, it includes example code snippets demonstrating the usage of these operators and functions.

Uploaded by

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

Operators: Arithmetic operators

Operator Name Description Example


+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
-- Decrement Decreases the value of a variable by 1 --x
++ Increment Increases the value of a variable by 1 ++x

Operators: Assignment Operators


Operator Name Example Same As
= Simple Assignment x=5 x=5
+= Add AND Assignment x += 3 x=x+3
-= Subtract AND Assignment x -= 3 x=x-3
*= Multiply AND Assignment x *= 3 x=x*3
/= Divide AND Assignment x /= 3 x=x/3
%= Modulus AND Assignment x %= 3 x=x%3

Operators: Comparison Operators/ Relational Operators


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

Operators: Bitwise Operators


Operators: Bitwise Assignment Operators
Operators: Logical Operators
Operator Name Description Example
&& Logical Returns true if both statements x < 5 && x < 10
and are true
|| Logical Returns true if one of the x < 5 || x < 4
or statements is true
! Logical Reverse the result, return false if !(x < 5 && x < 10)
not the result is true

Example:
#include<iostream>
using namespace std;

int main(){
//bool isCPPHard = true;
//bool CPPEnjoyable = false;
//cout << isCPPHard;
//cout << CPPEnjoyable;
int x = 10;
int y = 9;
//cout << (x > y);
//cout << (x == 10);
//cout<< (x < 5 && x < 10);
cout<< (y < 5 || y < 10);
return 0;
}
C++ Conditions:
If condition:
Syntax: if (condition) {
}
Example: int x = 20; int y = 18;
if (x>y){
cout<<x<<" is greater than "<<y<<endl;
return 0;
}
If … else condition:
Syntax: if (condition) {
}
else {
}
If … else if … else condition:
Syntax: if (condition 1) {
}
else if (condition 2) {
}
else {
}
Example 01:
int CurrentTime;
cout<<"Please Enter the Current Time (24hr format) :"
cin>>CurrentTime;
if (time < 12) {
cout << "Good morning.";
} else if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}

Example 02: Find the larger value from the three integers entered by user from
the keyboard.
#include <iostream>
using namespace std;
int main() {
int a, b,c;
cout << "Please enter three values :";
cin>>a>>b>>c;

if(a>b && a>c){


cout<<a<<" is larger value";
}
else if(b>a && b>c){
cout<<b<<" is larger value";
}
else{
cout<<c<<" is larger value";
}

return 0;
}
Working with strings:
To work with strings, first include the string library:
#include <string>
Concatenation: using +
Append: firstVariable.append(lastVariable)
String length: variable_Name.size() or variable_Name.length()
Access to string: variable_Name[index]
Change the string characters: variable_Name[index] = 'new_Char'
User input string: getline(cin, variable_Name) to get full strings
Special characters: \', \" , and \\

Example:
#include <iostream>
#include <string>
using namespace std;

int main(){
string firstName = "RAIN MAN"
string lastName = "RAJA"
string firstName = firstName + lastName;
cout << fullName;
// cout<<firstName <<" "<<lastName<<endl;
// string fullName = firstName.append(lastName);
// cout<<fullName;
// cout<<firstName.append(lastName);
// string x = "100";
// string y = "333";
// string z = x+y;
// cout<<z;
// cout<<firstName.size();
// cout<<fullName.length()<<"\n";
// cout<<fullName[4]<<endl;
// fullName[15]='Z';
// cout<<fullName;

// string fullName;
// cout << "Type your full name: ";
// // cin>>fullName;
// getline (cin, fullName);
// cout << "Your name is: " << fullName<<endl;
return 0;
}

Working with math:


To use all mathematical operations, first include the cmath library:
#include <cmath>
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x i.e. cos-1
asin(x) Returns the arcsine of x i.e. sin-1
atan(x) Returns the arctangent of x i.e. tan-1
cbrt(x) Returns the cube root of x
ceil(x) Returns the value of x rounded up to its nearest integer
cos(x) Returns the cosine of x
𝑒 𝑥 +𝑒 −𝑥
cosh(x) Returns the hyperbolic cosine of x i.e. 2

exp(x) Returns the value of ex (e = 2.718281….)


expm1(x) Returns ex -1
fabs(x) Returns the absolute value of a floating x
fdim(x, y) Returns the positive difference between x and y
floor(x) Returns the value of x rounded down to its nearest integer
hypot(x, y) Returns sqrt(x2 +y2) w/o intermediate overflow or underflow
log(x) Return the log of x as natural log or e base log or ln
log10(x) Return the log of x as 10 base log.
max(x,y) Return maximum value from x and y
min(x,y) Return minimum value from x and y
fmax(x, y) Returns the highest value of a floating x and y
fmin(x, y) Returns the lowest value of a floating x and y
fmod(x, y) Returns the floating point remainder of x/y
pow(x, y) Returns the value of x to the power of y
round(x) Returns the rounded value of x
sin(x) Returns the sine of x (x is in radians)
𝑒 𝑥 −𝑒 −𝑥
sinh(x) Returns the hyperbolic sine of x i.e.
2

sqrt(x) Return the square root of a value


tan(x) Returns the tangent of an angle
𝑒 𝑥 −𝑒 −𝑥
tanh(x) Returns the hyperbolic tangent of x i.e. 𝑒 𝑥 +𝑒 −𝑥

Example:
#include <iostream>
#include <cmath>
using namespace std;

int main(){
int a = 100;
cout<<log10(a);
return 0;
}

You might also like