SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Computer Programming
in
C++
(PRACTICAL#04)
 Objective : To become familiar with Operators in C++
 Operators : Are the symbols that perform operations / calculations on operands.
 Expression : any arrangement of variables, constants and operators that specifies a
computation.
 Example : sum = num1+num2;
 Three categories of operators :
 Binary Operators : act or operate on two operands.
 Unary Operators act or operate on one operand
 Ternary Operators act or operate on three operands
 Arithmetic Operators
Operators Result
+ Addition
__ Subtraction
* Multiplication
/ Division
% Remainder or Modulus
Arithmetic Assignment Operators
 C++ provides several assignment operators for
abbreviating assignment expressions.
 For example, you can abbreviate the statement
c = c + 3;
 with the addition assignment operator, +=, as
c += 3;
Compound Assignment Operators
Operator Example Expression Result
+= var1+=var2 var1 is assigned the value that is the sum of var1 and var2
_= var1-=var2 var1 is assigned the value that is the value of var2 subtracted
from the value of var1
*= var1*=var2
/=
%=
Arithmetic Operator Example
class ArthemticOperator {
void main() {
int a = 4, b = 2, c;
c = a + b;
cout<<“Addition: “<<+c;
c = a – b;
cout<<“Subtraction: ”<<c;
 c = a * b;
 cout<<“Multiplication: ”<<c;
 c = a / b;
 cout<<“Division: ”<<c;
 c = a % b;
 cout<<“Modulus: ”<<c;
 }
Increment & Decrement Operators
 Increment & Decrement Operators : C++ provides two unary operators for
adding 1 to or subtracting 1 from the value of a numeric variable.
Increment & Decrement Operators
Operator Example Expression Result
++ var1=++var2 var1 is assigned the value of var2+1.
++ var1=var2++ var1 is assigned the value of var2. var2 is incremented by 1.
-- var1= --var2
-- var1=var2--
Preincrementing and postincrementing
Example // main method begins execution of C++ program
void main() {
int c;
c = 5;
cout<<c ; // print 5
cout<<c++ ;
cout<<c <<endl;
c = 5;
cout<< c;
cout<< ++c ;
cout<< c ;
} // end method main
Operator Precedence
Precedence Operators
Highest ++, -- (used as prefixes)
*, / , %
+, -
=,*=, /=,%=,+=,-=
&,^,|
&&,||
Lowest ++, -- (used as suffixes)
Relational Operators
 Java has six relational operators that compare two numbers and
return a Boolean value (i.e. true or false or 1 or 0)
 The outcome of these operations is a Boolean value.
Operator Meaning
 == Equal to
 > Greater than
 >= Greater than or equal to
 < Less than
 <= Less than or equal to
 != not equal to
Relational Operators
Operator Example Expression Result
== var1=var2==var3; var1 is assigned the value true if var2 is equal to var3, or false
otherwise.
!= var1= var2 !=var3; var1 is assigned the value true if var2 is not equal to var3, or false
otherwise.
< Var1= var2 < var3;
<= Var1=var2<=var3;
> Var1=var2 > var3;
>= Var1=var2>=var3;
Relational Operators Example
void main() {
int a = 10; int b = 20;
cout<< "a == b = " << (a == b);
cout<< "a != b = " << (a != b);
cout<< "a > b = " << (a > b);
cout<< "a < b = " << (a < b);
cout<< "b >= a = " << (b >= a);
cout<< "b <= a = " <<(b <= a);
output
The Logical Operators
 C++ provides logical operators to enable programmers
to form or combine two or more Boolean expressions .
The logical operators are
 && (logical AND),
 || (logical OR),
 ! (logical NOT, also called logical negation).
The Logical Operators
 // Demonstrate the Logical operators.
void main() {
int a = 10; int b = 20;
cout<< "a > 9 && a < 20 = " << (a > 9 && a<20 );
cout<< "a > 8 || a <10 = " << (a >8 || a <10);
cout<< “!(a ==b) = " << !(a == b);
}
The Bitwise Operators
 There are also the bitwise operators in C++ which are used to perform bitwise
operations on bit patterns or binary numerals that involve the manipulation of
individual bits.
 Bitwise operators can be applied to the integer types, long, int, short, char, and byte.
 Bitwise operator works on bits and performs bit-by-bit operation.
 Example if a = 5 and b = 3; now in binary format they will be as follows −
 a = 0000 0101
 b = 0000 0011
The Bitwise Operators
 The Bitwise NOT (~) Binary Ones Complement Operator is unary and has the
effect of 'flipping' bits
 Example Number 5 in binary is 00000101
 After Bitwise NOT operation the value is 11111010
 The Bitwise AND (&) 00000101 5
& 00000011 3
00000001 1
The Bitwise Operators
The Bitwise OR (|)
00000101 5
| 00000011 3
00000111 7
The Bitwise Operators
The Bitwise exclusive OR (^)
00000101 5
^ 00000011 3
00000110 6
Tasks for Lab # 4
Task #1 : Write C++ code that takes a number from the user as an input and prints its cube.
(Use cin>> for input)
Task# 2 : Write a C++ program that takes a single character as input and Displays it on the
screen. (Use cin>> for input)
Task #3: Write C++ program to demonstrate the working of the following operators:
increment and decrement operators
Relational operators
Task # 4 : Write a C++ program that Declares 3 floating numbers and prints their total sum
and average.

Weitere ähnliche Inhalte

Was ist angesagt?

Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
bajiajugal
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
ecko_disasterz
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
emailharmeet
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
Deepak Singh
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
eShikshak
 

Was ist angesagt? (20)

Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
Lab 1
Lab 1Lab 1
Lab 1
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++
 
Prefix Postfix
Prefix PostfixPrefix Postfix
Prefix Postfix
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
 
C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
 
Pointers in Programming
Pointers in ProgrammingPointers in Programming
Pointers in Programming
 
C operators
C operatorsC operators
C operators
 
Operators
OperatorsOperators
Operators
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Basic c operators
Basic c operatorsBasic c operators
Basic c operators
 
2. operators in c
2. operators in c2. operators in c
2. operators in c
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
 

Ähnlich wie Few Operator used in c++

Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
CtOlaf
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
Dti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionDti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpression
alish sha
 
PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptx
Nithya K
 

Ähnlich wie Few Operator used in c++ (20)

C++ chapter 2
C++ chapter 2C++ chapter 2
C++ chapter 2
 
Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
 
ICP - Lecture 5
ICP - Lecture 5ICP - Lecture 5
ICP - Lecture 5
 
Theory3
Theory3Theory3
Theory3
 
C++
C++C++
C++
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
ppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operatorsppt on logical/arthimatical/conditional operators
ppt on logical/arthimatical/conditional operators
 
Coper in C
Coper in CCoper in C
Coper in C
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
operators.pptx
operators.pptxoperators.pptx
operators.pptx
 
05 operators
05   operators05   operators
05 operators
 
03. Operators Expressions and statements
03. Operators Expressions and statements03. Operators Expressions and statements
03. Operators Expressions and statements
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
 
3306617
33066173306617
3306617
 
Dti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpressionDti2143 chapter 3 arithmatic relation-logicalexpression
Dti2143 chapter 3 arithmatic relation-logicalexpression
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
 
Operators
OperatorsOperators
Operators
 
Computer programming 2 Lesson 7
Computer programming 2  Lesson 7Computer programming 2  Lesson 7
Computer programming 2 Lesson 7
 
PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptx
 

Kürzlich hochgeladen

Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...
Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...
Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...
drmarathore
 
Vip Mumbai Call Girls Kalyan Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Kalyan Call On 9920725232 With Body to body massage wit...Vip Mumbai Call Girls Kalyan Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Kalyan Call On 9920725232 With Body to body massage wit...
amitlee9823
 
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...
motiram463
 
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Naicy mandal
 
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
CHEAP Call Girls in Mayapuri (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Mayapuri  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Mayapuri  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Mayapuri (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Kothanur Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
Kothanur Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...Kothanur Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
Kothanur Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
amitlee9823
 
Escorts Service Arekere ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Arekere ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Arekere ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Arekere ☎ 7737669865☎ Book Your One night Stand (Bangalore)
amitlee9823
 
Call Girls Banashankari Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Banashankari Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Banashankari Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Banashankari Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
amitlee9823
 

Kürzlich hochgeladen (20)

↑Top celebrity ( Pune ) Nagerbazar Call Girls8250192130 unlimited shot and al...
↑Top celebrity ( Pune ) Nagerbazar Call Girls8250192130 unlimited shot and al...↑Top celebrity ( Pune ) Nagerbazar Call Girls8250192130 unlimited shot and al...
↑Top celebrity ( Pune ) Nagerbazar Call Girls8250192130 unlimited shot and al...
 
9892124323 Pooja Nehwal Call Girls Services Call Girls service in Santacruz A...
9892124323 Pooja Nehwal Call Girls Services Call Girls service in Santacruz A...9892124323 Pooja Nehwal Call Girls Services Call Girls service in Santacruz A...
9892124323 Pooja Nehwal Call Girls Services Call Girls service in Santacruz A...
 
VVIP Pune Call Girls Balaji Nagar (7001035870) Pune Escorts Nearby with Compl...
VVIP Pune Call Girls Balaji Nagar (7001035870) Pune Escorts Nearby with Compl...VVIP Pune Call Girls Balaji Nagar (7001035870) Pune Escorts Nearby with Compl...
VVIP Pune Call Girls Balaji Nagar (7001035870) Pune Escorts Nearby with Compl...
 
Deira Dubai Escorts +0561951007 Escort Service in Dubai by Dubai Escort Girls
Deira Dubai Escorts +0561951007 Escort Service in Dubai by Dubai Escort GirlsDeira Dubai Escorts +0561951007 Escort Service in Dubai by Dubai Escort Girls
Deira Dubai Escorts +0561951007 Escort Service in Dubai by Dubai Escort Girls
 
Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...
Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...
Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...
 
Vip Mumbai Call Girls Kalyan Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Kalyan Call On 9920725232 With Body to body massage wit...Vip Mumbai Call Girls Kalyan Call On 9920725232 With Body to body massage wit...
Vip Mumbai Call Girls Kalyan Call On 9920725232 With Body to body massage wit...
 
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...
(👉Ridhima)👉VIP Model Call Girls Mulund ( Mumbai) Call ON 9967824496 Starting ...
 
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Makarba ( Call Girls ) Ahmedabad ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
 
9004554577, Get Adorable Call Girls service. Book call girls & escort service...
9004554577, Get Adorable Call Girls service. Book call girls & escort service...9004554577, Get Adorable Call Girls service. Book call girls & escort service...
9004554577, Get Adorable Call Girls service. Book call girls & escort service...
 
HLH PPT.ppt very important topic to discuss
HLH PPT.ppt very important topic to discussHLH PPT.ppt very important topic to discuss
HLH PPT.ppt very important topic to discuss
 
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Dharwad 7001035870 Whatsapp Number, 24/07 Booking
 
Top Rated Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Ravet ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
CHEAP Call Girls in Mayapuri (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Mayapuri  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Mayapuri  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Mayapuri (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
SM-N975F esquematico completo - reparación.pdf
SM-N975F esquematico completo - reparación.pdfSM-N975F esquematico completo - reparación.pdf
SM-N975F esquematico completo - reparación.pdf
 
Kothanur Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
Kothanur Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...Kothanur Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
Kothanur Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
 
Escorts Service Arekere ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Arekere ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Arekere ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Arekere ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
Top Rated Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated  Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Top Rated  Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated Pune Call Girls Shirwal ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
 
Call Girls in Vashi Escorts Services - 7738631006
Call Girls in Vashi Escorts Services - 7738631006Call Girls in Vashi Escorts Services - 7738631006
Call Girls in Vashi Escorts Services - 7738631006
 
Book Sex Workers Available Pune Call Girls Yerwada 6297143586 Call Hot India...
Book Sex Workers Available Pune Call Girls Yerwada  6297143586 Call Hot India...Book Sex Workers Available Pune Call Girls Yerwada  6297143586 Call Hot India...
Book Sex Workers Available Pune Call Girls Yerwada 6297143586 Call Hot India...
 
Call Girls Banashankari Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Banashankari Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Banashankari Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Banashankari Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
 

Few Operator used in c++

  • 2.  Objective : To become familiar with Operators in C++  Operators : Are the symbols that perform operations / calculations on operands.  Expression : any arrangement of variables, constants and operators that specifies a computation.  Example : sum = num1+num2;  Three categories of operators :  Binary Operators : act or operate on two operands.  Unary Operators act or operate on one operand  Ternary Operators act or operate on three operands
  • 3.  Arithmetic Operators Operators Result + Addition __ Subtraction * Multiplication / Division % Remainder or Modulus
  • 4. Arithmetic Assignment Operators  C++ provides several assignment operators for abbreviating assignment expressions.  For example, you can abbreviate the statement c = c + 3;  with the addition assignment operator, +=, as c += 3;
  • 5. Compound Assignment Operators Operator Example Expression Result += var1+=var2 var1 is assigned the value that is the sum of var1 and var2 _= var1-=var2 var1 is assigned the value that is the value of var2 subtracted from the value of var1 *= var1*=var2 /= %=
  • 6. Arithmetic Operator Example class ArthemticOperator { void main() { int a = 4, b = 2, c; c = a + b; cout<<“Addition: “<<+c; c = a – b; cout<<“Subtraction: ”<<c;  c = a * b;  cout<<“Multiplication: ”<<c;  c = a / b;  cout<<“Division: ”<<c;  c = a % b;  cout<<“Modulus: ”<<c;  }
  • 7. Increment & Decrement Operators  Increment & Decrement Operators : C++ provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric variable.
  • 8. Increment & Decrement Operators Operator Example Expression Result ++ var1=++var2 var1 is assigned the value of var2+1. ++ var1=var2++ var1 is assigned the value of var2. var2 is incremented by 1. -- var1= --var2 -- var1=var2--
  • 9. Preincrementing and postincrementing Example // main method begins execution of C++ program void main() { int c; c = 5; cout<<c ; // print 5 cout<<c++ ; cout<<c <<endl; c = 5; cout<< c; cout<< ++c ; cout<< c ; } // end method main
  • 10. Operator Precedence Precedence Operators Highest ++, -- (used as prefixes) *, / , % +, - =,*=, /=,%=,+=,-= &,^,| &&,|| Lowest ++, -- (used as suffixes)
  • 11. Relational Operators  Java has six relational operators that compare two numbers and return a Boolean value (i.e. true or false or 1 or 0)  The outcome of these operations is a Boolean value. Operator Meaning  == Equal to  > Greater than  >= Greater than or equal to  < Less than  <= Less than or equal to  != not equal to
  • 12. Relational Operators Operator Example Expression Result == var1=var2==var3; var1 is assigned the value true if var2 is equal to var3, or false otherwise. != var1= var2 !=var3; var1 is assigned the value true if var2 is not equal to var3, or false otherwise. < Var1= var2 < var3; <= Var1=var2<=var3; > Var1=var2 > var3; >= Var1=var2>=var3;
  • 13. Relational Operators Example void main() { int a = 10; int b = 20; cout<< "a == b = " << (a == b); cout<< "a != b = " << (a != b); cout<< "a > b = " << (a > b); cout<< "a < b = " << (a < b); cout<< "b >= a = " << (b >= a); cout<< "b <= a = " <<(b <= a); output
  • 14. The Logical Operators  C++ provides logical operators to enable programmers to form or combine two or more Boolean expressions . The logical operators are  && (logical AND),  || (logical OR),  ! (logical NOT, also called logical negation).
  • 15. The Logical Operators  // Demonstrate the Logical operators. void main() { int a = 10; int b = 20; cout<< "a > 9 && a < 20 = " << (a > 9 && a<20 ); cout<< "a > 8 || a <10 = " << (a >8 || a <10); cout<< “!(a ==b) = " << !(a == b); }
  • 16. The Bitwise Operators  There are also the bitwise operators in C++ which are used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.  Bitwise operators can be applied to the integer types, long, int, short, char, and byte.  Bitwise operator works on bits and performs bit-by-bit operation.  Example if a = 5 and b = 3; now in binary format they will be as follows −  a = 0000 0101  b = 0000 0011
  • 17. The Bitwise Operators  The Bitwise NOT (~) Binary Ones Complement Operator is unary and has the effect of 'flipping' bits  Example Number 5 in binary is 00000101  After Bitwise NOT operation the value is 11111010  The Bitwise AND (&) 00000101 5 & 00000011 3 00000001 1
  • 18. The Bitwise Operators The Bitwise OR (|) 00000101 5 | 00000011 3 00000111 7
  • 19. The Bitwise Operators The Bitwise exclusive OR (^) 00000101 5 ^ 00000011 3 00000110 6
  • 20. Tasks for Lab # 4 Task #1 : Write C++ code that takes a number from the user as an input and prints its cube. (Use cin>> for input) Task# 2 : Write a C++ program that takes a single character as input and Displays it on the screen. (Use cin>> for input) Task #3: Write C++ program to demonstrate the working of the following operators: increment and decrement operators Relational operators Task # 4 : Write a C++ program that Declares 3 floating numbers and prints their total sum and average.