SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Operators and Expressions
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
7
1
Outline
 Operands
 Arithmetic Operators
 Unary Operators
 Relational and Logical Operators
 Assignment Operator
 Conditional Operator
 Operator Precedence Groups
Operands
 The data items that operators act upon are called
operands.
 Some operators require two operands(a+b, a-b),
while others act upon only one operand(i++, i--).
Arithmetic Operators
 There are five arithmetic operators in C. They are
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after
integer division
Arithmetic Operators (cont..)
 The % operators is sometimes referred as the
modulus operator.
 The remainder operator (%) requires that both
operands be integers and second operand be
nonzero.
Arithmetic Operators (cont..)
 Division (/) of one integer quantity by another is
referred to as integer division that always results in a
truncated quotient (the decimal portion of the
quotient will be dropped).
 If a division operation is carried out with two
floating-point numbers or with one floating-point
number and one integer, the result will be a floating-
point quotient.
Arithmetic Operators (cont..)
 Suppose that a and b are integer variables and a = 10
and b = 3.
Expression Value
a + b 13
a – b 7
a * b 30
a / b 3
a % b 1
Arithmetic Operators (cont..)
 Suppose that v1 and v2 are floating-point variables
and v1 = 12.5 and v2 = 2.0.
Expression Value
v1 + v2 14.5
v1 – v2 10.5
v1 * v2 25.0
v1 / v2 6.25
Unary Operators
 Unary operator is a class of operator that act upon a
single operand to produce a new value.
 The most common unary operation is unary minus, where
a numerical constant, variable or expression is preceded
my a minus sign.
 Example: -743 -0.2 -3 * (x+y)
 There are two other commonly used unary operators:
1. The increment operator (++)
2. The decrement operator (--)
Unary Operators (cont..)
 Suppose that i is an integer variable that has been
assigned a value of 5.The expression ++i, which is
equivalent to writing i = i + 1, cause the value of i to
be increased to 6. Similarly, the expression --i, which is
equivalent to i = i - 1, cause the (original) value of i to
be decreased to 4.
Unary Operators Example 1
 Lets see an example…
a = 10; b = 20;
x = ++a;
y = b++;
printf(“x = %d a = %dn”,x , a);
printf(“y = %d a = %dn”,y , a);
Unary Operators Example 1 (cont..)
Here, x = ++a statement is equivalent to following two statement
a = a + 1;
x = a;
Thus the value of x will be 11.
On the other hand, y = b++ is equivalent to
y = b;
b = b+1;
So, the value of y will be 20 and b will be 21.
Unary Operators Example 2
Lets do these exercise together:
Here, a = 10; b = 20;
1. x = 50 + ++a;
2. y = 100 + b++;
3. x = a++ - ++a;
Relational and Logical Operators
 There are four relational operators.They are
 Closely associate with the relational operators are
Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
Operator Meaning
== Equal to
!= Not equal to
Relational and Logical Operators
(cont..)
 Relational Operator example 1: suppose that i, j
and k are integer variables whose values are 1, 2 and
3, respectively.
Expression Interpretation Value
i < j true 1
(i + j) >= k true 1
(j +k) > (i + 5) false 0
k !=3 false 0
j == 2 true 1
Relational and Logical Operators
(cont..)
 Relational Operator example 2: Suppose that i is
an integer variable whose value is 7, f is a floating
variable whose value is 5.5, and c is a character
variable that express the character ‘w’
Expression Interpretation Value
f < 5 true 1
(i + f) <= 10 false 0
c == 119 true 1
c != ‘p’ true 1
C > = 10 * ( i + f) false 0
Relational and Logical Operators (cont..)
 C contains two logical operators. They are..
 && is also referred as logical and
 || is also referred as logical or
Operator Meaning
&& and
|| or
Relational and Logical Operators (cont..)
 logical operators examples 1: Suppose that i is an
integer whose value is 7, f is a floating-point variable
whose value is 5.5, and c is a character variable that
represents the character ‘w’.
Expression Interpretation Value
(i >=6) && (c == ‘w’) true 1
(I >=6) || (c == 119) true 1
(f < 11) && (I >100) false 0
(c != ‘p’) || ((I + f) <=
10)
true 1
Relational and Logical Operators (cont..)
 logical operators examples 1: Suppose that i is an
integer whose value is 7, f is a floating-point variable
whose value is 5.5, and c is a character variable that
represents the character ‘w’.
Expression Interpretation Value
(I + f) <= 10 false 0
(i >=6) && (c == ‘w’) true 1
(c = ‘p’) || ((I + f) <=
10)
false 0
Relational and Logical Operators (cont..)
 logical operators examples 1: Suppose that i is an
integer whose value is 7, f is a floating-point variable
whose value is 5.5, and c is a character variable that
represents the character ‘w’.
Expression Interpretation Value
(I + f) <= 10 false 0
(i >=6) && (c == ‘w’) true 1
(c = ‘p’) || ((I + f) <=
10)
false 0
Assignment Operator
 The most commonly used assignment operator is ‘=’
 identifier = expression
 Example:
 a = 3;
 x = y;
 sum = a + b;
 And so on….
Conditional Operator
 Simple conditional operations can be carried out with
the conditional operator(? :).
 A conditional expression is written in the form
expression 1 ? expression 2 : expression 3
 When evaluating a conditional expression, expression 1
is evaluated first.
 If expression 1 is true, than expression 2 is evaluated and this
become the value of the conditional expression.
 If the expression 1 is false, then expression 3 is evaluated and
this become the value of the conditional expression.
Conditional Operator Example
 Let i = 5
So, flag = (i < 0) ? 0 : 100
Output will be 100 as expression 1 (i < 0) is false.
 Let, f = 8 and g = 9
So, min = (f < g) ? f : g
Output will be 8 as expression 1(f<g) is true.
Operator Precedence Groups
Operator Category Operators Associativity
unary operators - ++ -- ! sizeof(type) R -> L
arithmetic multiply,
divide, remainder
* / % L -> R
arithmetic add and
subtract
+ - L -> R
relational operators < <= > >= L -> R
equality operators == != L -> R
logical and && L -> R
logical or || L -> R
conditional operator ? : R -> L
assignment operators = += -+ *= /= %= R -> L
Lecture 7- Operators and Expressions

Weitere ähnliche Inhalte

Was ist angesagt?

C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshareGagan Deep
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)SURBHI SAROHA
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9Rumman Ansari
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsDhivyaSubramaniyam
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or javaSamsil Arefin
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)SURBHI SAROHA
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)SURBHI SAROHA
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Vishvesh Jasani
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
Conditional statements in vb script
Conditional statements in vb scriptConditional statements in vb script
Conditional statements in vb scriptNilanjan Saha
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++Neeru Mittal
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 

Was ist angesagt? (20)

C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshare
 
C if else
C if elseC if else
C if else
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Conditional statements in vb script
Conditional statements in vb scriptConditional statements in vb script
Conditional statements in vb script
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Input And Output
 Input And Output Input And Output
Input And Output
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
7 functions
7  functions7  functions
7 functions
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 

Ähnlich wie Lecture 7- Operators and Expressions

Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
C++ Expressions Notes
C++ Expressions NotesC++ Expressions Notes
C++ Expressions NotesProf Ansari
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c languageTanmay Modi
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzationKaushal Patel
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)jahanullah
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3rohassanie
 
Conditional and special operators
Conditional and special operatorsConditional and special operators
Conditional and special operatorsMegha Sharma
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programmingsavitamhaske
 
Types of Operators in C programming .pdf
Types of Operators in C programming  .pdfTypes of Operators in C programming  .pdf
Types of Operators in C programming .pdfRichardMathengeSPASP
 

Ähnlich wie Lecture 7- Operators and Expressions (20)

Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
C++ Expressions Notes
C++ Expressions NotesC++ Expressions Notes
C++ Expressions Notes
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
 
Basics of c++
Basics of c++ Basics of c++
Basics of c++
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
Conditional and special operators
Conditional and special operatorsConditional and special operators
Conditional and special operators
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
C language basics
C language basicsC language basics
C language basics
 
C PRESENTATION.pptx
C PRESENTATION.pptxC PRESENTATION.pptx
C PRESENTATION.pptx
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Types of Operators in C programming .pdf
Types of Operators in C programming  .pdfTypes of Operators in C programming  .pdf
Types of Operators in C programming .pdf
 

Mehr von Md. Imran Hossain Showrov (15)

Lecture 22 - Error Handling
Lecture 22 - Error HandlingLecture 22 - Error Handling
Lecture 22 - Error Handling
 
Lecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header FileLecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header File
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Lecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional ArrayLecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional Array
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
 
Lecture 4- Computer Software and Languages
Lecture 4- Computer Software and LanguagesLecture 4- Computer Software and Languages
Lecture 4- Computer Software and Languages
 
Lecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devicesLecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devices
 
Lecture 2 - Introductory Concepts
Lecture 2 - Introductory ConceptsLecture 2 - Introductory Concepts
Lecture 2 - Introductory Concepts
 
Lecture 1- History of C Programming
Lecture 1- History of C Programming Lecture 1- History of C Programming
Lecture 1- History of C Programming
 

Kürzlich hochgeladen

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 

Kürzlich hochgeladen (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 

Lecture 7- Operators and Expressions

  • 1. Operators and Expressions Md. Imran Hossain Showrov (showrovsworld@gmail.com) 7 1
  • 2. Outline  Operands  Arithmetic Operators  Unary Operators  Relational and Logical Operators  Assignment Operator  Conditional Operator  Operator Precedence Groups
  • 3. Operands  The data items that operators act upon are called operands.  Some operators require two operands(a+b, a-b), while others act upon only one operand(i++, i--).
  • 4. Arithmetic Operators  There are five arithmetic operators in C. They are Operator Purpose + Addition - Subtraction * Multiplication / Division % Remainder after integer division
  • 5. Arithmetic Operators (cont..)  The % operators is sometimes referred as the modulus operator.  The remainder operator (%) requires that both operands be integers and second operand be nonzero.
  • 6. Arithmetic Operators (cont..)  Division (/) of one integer quantity by another is referred to as integer division that always results in a truncated quotient (the decimal portion of the quotient will be dropped).  If a division operation is carried out with two floating-point numbers or with one floating-point number and one integer, the result will be a floating- point quotient.
  • 7. Arithmetic Operators (cont..)  Suppose that a and b are integer variables and a = 10 and b = 3. Expression Value a + b 13 a – b 7 a * b 30 a / b 3 a % b 1
  • 8. Arithmetic Operators (cont..)  Suppose that v1 and v2 are floating-point variables and v1 = 12.5 and v2 = 2.0. Expression Value v1 + v2 14.5 v1 – v2 10.5 v1 * v2 25.0 v1 / v2 6.25
  • 9. Unary Operators  Unary operator is a class of operator that act upon a single operand to produce a new value.  The most common unary operation is unary minus, where a numerical constant, variable or expression is preceded my a minus sign.  Example: -743 -0.2 -3 * (x+y)  There are two other commonly used unary operators: 1. The increment operator (++) 2. The decrement operator (--)
  • 10. Unary Operators (cont..)  Suppose that i is an integer variable that has been assigned a value of 5.The expression ++i, which is equivalent to writing i = i + 1, cause the value of i to be increased to 6. Similarly, the expression --i, which is equivalent to i = i - 1, cause the (original) value of i to be decreased to 4.
  • 11. Unary Operators Example 1  Lets see an example… a = 10; b = 20; x = ++a; y = b++; printf(“x = %d a = %dn”,x , a); printf(“y = %d a = %dn”,y , a);
  • 12. Unary Operators Example 1 (cont..) Here, x = ++a statement is equivalent to following two statement a = a + 1; x = a; Thus the value of x will be 11. On the other hand, y = b++ is equivalent to y = b; b = b+1; So, the value of y will be 20 and b will be 21.
  • 13. Unary Operators Example 2 Lets do these exercise together: Here, a = 10; b = 20; 1. x = 50 + ++a; 2. y = 100 + b++; 3. x = a++ - ++a;
  • 14. Relational and Logical Operators  There are four relational operators.They are  Closely associate with the relational operators are Operator Meaning < Less than <= Less than or equal to > Greater than >= Greater than or equal to Operator Meaning == Equal to != Not equal to
  • 15. Relational and Logical Operators (cont..)  Relational Operator example 1: suppose that i, j and k are integer variables whose values are 1, 2 and 3, respectively. Expression Interpretation Value i < j true 1 (i + j) >= k true 1 (j +k) > (i + 5) false 0 k !=3 false 0 j == 2 true 1
  • 16. Relational and Logical Operators (cont..)  Relational Operator example 2: Suppose that i is an integer variable whose value is 7, f is a floating variable whose value is 5.5, and c is a character variable that express the character ‘w’ Expression Interpretation Value f < 5 true 1 (i + f) <= 10 false 0 c == 119 true 1 c != ‘p’ true 1 C > = 10 * ( i + f) false 0
  • 17. Relational and Logical Operators (cont..)  C contains two logical operators. They are..  && is also referred as logical and  || is also referred as logical or Operator Meaning && and || or
  • 18. Relational and Logical Operators (cont..)  logical operators examples 1: Suppose that i is an integer whose value is 7, f is a floating-point variable whose value is 5.5, and c is a character variable that represents the character ‘w’. Expression Interpretation Value (i >=6) && (c == ‘w’) true 1 (I >=6) || (c == 119) true 1 (f < 11) && (I >100) false 0 (c != ‘p’) || ((I + f) <= 10) true 1
  • 19. Relational and Logical Operators (cont..)  logical operators examples 1: Suppose that i is an integer whose value is 7, f is a floating-point variable whose value is 5.5, and c is a character variable that represents the character ‘w’. Expression Interpretation Value (I + f) <= 10 false 0 (i >=6) && (c == ‘w’) true 1 (c = ‘p’) || ((I + f) <= 10) false 0
  • 20. Relational and Logical Operators (cont..)  logical operators examples 1: Suppose that i is an integer whose value is 7, f is a floating-point variable whose value is 5.5, and c is a character variable that represents the character ‘w’. Expression Interpretation Value (I + f) <= 10 false 0 (i >=6) && (c == ‘w’) true 1 (c = ‘p’) || ((I + f) <= 10) false 0
  • 21. Assignment Operator  The most commonly used assignment operator is ‘=’  identifier = expression  Example:  a = 3;  x = y;  sum = a + b;  And so on….
  • 22. Conditional Operator  Simple conditional operations can be carried out with the conditional operator(? :).  A conditional expression is written in the form expression 1 ? expression 2 : expression 3  When evaluating a conditional expression, expression 1 is evaluated first.  If expression 1 is true, than expression 2 is evaluated and this become the value of the conditional expression.  If the expression 1 is false, then expression 3 is evaluated and this become the value of the conditional expression.
  • 23. Conditional Operator Example  Let i = 5 So, flag = (i < 0) ? 0 : 100 Output will be 100 as expression 1 (i < 0) is false.  Let, f = 8 and g = 9 So, min = (f < g) ? f : g Output will be 8 as expression 1(f<g) is true.
  • 24. Operator Precedence Groups Operator Category Operators Associativity unary operators - ++ -- ! sizeof(type) R -> L arithmetic multiply, divide, remainder * / % L -> R arithmetic add and subtract + - L -> R relational operators < <= > >= L -> R equality operators == != L -> R logical and && L -> R logical or || L -> R conditional operator ? : R -> L assignment operators = += -+ *= /= %= R -> L