SlideShare a Scribd company logo
Operators
Introduction to Operators
• Definition of operators in programming: Operators in
programming are symbols or special characters that are used to
perform various operations on data. These operations can
range from simple arithmetic calculations to complex logical
comparisons and data manipulations. Operators allow
programmers to work with variables and values, enabling them
to create algorithms and solve problems efficiently.
Why operators are essential in
programming languages
1. Data Manipulation: Operators enable programmers to manipulate data in various ways, such as
performing mathematical calculations, modifying strings, and altering the values of variables. This is
crucial for solving real-world problems and performing tasks in software applications.
2. Control Flow: Operators play a key role in controlling the flow of a program. They allow for
conditional statements, loops, and decision-making, making it possible to create dynamic and
responsive code that can adapt to different situations.
3. Efficiency: Operators provide efficient ways to perform common operations, reducing the amount
of code that needs to be written. This efficiency is vital for optimizing program performance and
minimizing resource usage.
4. Expressiveness: Operators make code more concise and expressive. They allow programmers to
convey complex operations in a clear and readable manner, improving code maintainability and
understandability.
5. Flexibility: Different types of operators cater to various programming needs. From basic arithmetic
operators to advanced bitwise and logical operators, they offer flexibility in implementing a wide
range of functionalities.
6. Compatibility: Operators are a standard feature in most programming languages, making it easier
for developers to switch between languages and apply their knowledge across different platforms
and technologies.
Types of Operators
• Arithmetic operators
• Comparison (relational) operators
• Logical operators
• Assignment operators
• Bitwise operators
• Conditional (ternary) operator
• sizeof() operator
Arithmetic Operators
int main() {
int num1 = 10;
int num2 = 5;
int result;
// Addition Operator (+)
result = num1 + num2;
printf("Addition: %d + %d = %dn", num1, num2, result);
Comparison Operators
#include <stdio.h>
int main() {
int num1 = 10;
int num2 = 5;
// Equal to (==)
printf("Is %d equal to %d? %sn", num1, num2, num1 == num2 ? "Yes" :
"No");
// Not equal to (!=)
printf("Is %d not equal to %d? %sn", num1, num2, num1 != num2 ? "Yes" :
"No");
// Greater than (>)
printf("Is %d greater than %d? %sn", num1, num2, num1 > num2 ? "Yes" :
"No");
// Less than (<)
printf("Is %d less than %d? %sn", num1,
num2, num1 < num2 ? "Yes" : "No");
// Greater than or equal to (>=)
printf("Is %d greater than or equal to %d?
%sn", num1, num2, num1 >= num2 ? "Yes" :
"No");
// Less than or equal to (<=)
printf("Is %d less than or equal to %d?
%sn", num1, num2, num1 <= num2 ? "Yes" :
"No");
return 0;
}
Logical Operators
#include <stdio.h>
int main() {
int num1 = 10;
int num2 = 5;
int num3 = 7;
// Logical AND (&&)
if (num1 > num2 && num1 > num3) {
printf("%d is the largest number.n", num1);
}
// Logical OR (||)
if (num1 > num2 || num1 > num3) {
printf("%d is larger than at least one of
the other numbers.n", num1);
}
// Logical NOT (!)
if (!(num2 == num3)) {
printf("%d is not equal to %d.n", num2,
num3);
}
return 0;
}
Assignment Operators
#include <stdio.h>
int main() {
int num1 = 10;
int num2 = 5;
// Assignment Operator (=)
int result = num1; // Assign the value of num1 to result
printf("Result after assignment: %dn", result);
// Addition Assignment Operator (+=)
result += num2; // Add num2 to result and update result
printf("Result after addition assignment: %dn", result);
// Subtraction Assignment Operator (-=)
result -= num2; // Subtract num2 from result and update result
printf("Result after subtraction assignment: %dn", result);
// Multiplication Assignment Operator (*=)
result *= num2; // Multiply result by num2 and update result
printf("Result after multiplication assignment: %dn", result);
// Division Assignment Operator (/=)
result /= num2; // Divide result by num2 and update result
printf("Result after division assignment: %dn", result);
// Modulus Assignment Operator (%=)
result %= num2; // Calculate result % num2 and update result
printf("Result after modulus assignment: %dn", result);
return 0;
}
Bitwise Operators
#include <stdio.h>
int main() {
int num1 = 12; // Binary: 1100
int num2 = 5; // Binary: 0101
int result;
// Bitwise AND Operator (&)
result = num1 & num2; // Perform bitwise AND operation
printf("Bitwise AND: %d & %d = %dn", num1, num2, result); // Output: 4 (Binary: 0100)
// Bitwise OR Operator (|)
result = num1 | num2; // Perform bitwise OR operation
printf("Bitwise OR: %d | %d = %dn", num1, num2, result); // Output: 13 (Binary: 1101)
// Bitwise XOR Operator (^)
result = num1 ^ num2; // Perform bitwise XOR operation
printf("Bitwise XOR: %d ^ %d = %dn", num1, num2, result); // Output: 9 (Binary: 1001)
// Bitwise NOT Operator (~)
result = ~num1; // Perform bitwise NOT operation on num1
printf("Bitwise NOT: ~%d = %dn", num1, result); // Output: -
13 (Binary: 1111011)
// Left Shift Operator (<<)
result = num1 << 2; // Left shift num1 by 2 bits
printf("Left Shift: %d << 2 = %dn", num1, result); // Output:
48 (Binary: 110000)
// Right Shift Operator (>>)
result = num1 >> 2; // Right shift num1 by 2 bits
printf("Right Shift: %d >> 2 = %dn", num1, result); // Output:
3 (Binary: 11)
return 0;
}
Ternary Operator
• The ternary operator, also known as the conditional operator in C-like programming
languages, is a unique operator that allows you to write concise conditional expressions.
It has the following syntax:
condition ? expression_if_true : expression_if_false
Here's a breakdown of how the ternary operator works:
• condition: This is a boolean expression that evaluates to either true or false.
• expression_if_true: If the condition is true, this expression is evaluated and returned as
the result.
• expression_if_false: If the condition is false, this expression is evaluated and returned as
the result.
The ternary operator is often used as a shorthand for simple if-else statements, providing a
compact and readable way to express conditional logic.
When to Use the Ternary Operator:
• Simple Conditional Assignments:when you need to assign a value to
a variable based on a condition. It's useful for avoiding the verbosity
of an if-else statement in such cases.
int x = 10;
int y = 5;
int result = (x > y) ? x : y; // The value of 'result' will be 10 because x > y is true.
• Conditional Expressions in Output Statements: when you want to
conditionally output different values or messages.
printf("The larger number is: %dn", (x > y) ? x : y);
• Conditional Expressions in Function Arguments:to pass different
arguments to a function based on a condition.
Example:
int maxValue = getMaxValue((x > y) ? x : y);
Example of the ternary operator in code
#include <stdio.h>
int main() {
int num1 = 10;
int num2 = 5;
// Using the ternary operator to find the maximum value
int max = (num1 > num2) ? num1 : num2;
printf("The maximum value is: %dn", max);
return 0;
}
sizeof() Operator
It is used to determine the size in bytes of a data type or a variable. It returns
the size as an unsigned integer. Here's how to use sizeof() to find the size of
data types and variables:
• Size of datatypes:
#include <stdio.h>
int main() {
printf("Size of int: %lu bytesn", sizeof(int));
printf("Size of char: %lu bytesn", sizeof(char));
printf("Size of float: %lu bytesn", sizeof(float));
printf("Size of double: %lu bytesn", sizeof(double));
return 0;}
• Size of variables
#include <stdio.h>
int main() {
int x;
double y;
char z;
printf("Size of x: %lu bytesn", sizeof(x));
printf("Size of y: %lu bytesn", sizeof(y));
printf("Size of z: %lu bytesn", sizeof(z));
return 0;}
• Size of arrays
#include <stdio.h>
int main() {
int x;
double y;
char z;
printf("Size of x: %lu bytesn",
sizeof(x));
printf("Size of y: %lu bytesn",
sizeof(y));
printf("Size of z: %lu bytesn",
sizeof(z));
return 0;
}
Operator Precedence
Precedence Operator Description
1 () Parentheses (grouping)
2 [] Array subscript
-> Member access (via pointer)
. Member access (via object)
++, -- Post-increment, Post-decrement
(type) Type casting
3 ++, -- Pre-increment, Pre-decrement
+, - Unary plus, Unary minus
!, ~ Logical NOT, Bitwise NOT
& Address-of (unary)
* Indirection (unary)
sizeof Size of a type or object
(type) Type casting (compound)
4 *, /, % Multiplication, Division, Modulus
5 +, - Addition, Subtraction
6 <<, >> Bitwise Left Shift, Bitwise Right Shift
7 <, <=, >, >=
Relational operators: Less, Less or
Equal, Greater, Greater or Equal
8 ==, != Relational operators: Equal, Not Equal
9 & Bitwise AND
10 ^ Bitwise XOR
11 | Bitwise OR
12 && Logical AND
Operators with
higher precedence
are evaluated before
operators with lower
precedence. For
example, in an
expression like a *
b + c, the
multiplication (*) is
evaluated before the
addition (+) due to
their respective
precedence levels.
Operator Associativity
Operator associativity defines the order in which operators of the same precedence are evaluated when they appear
consecutively in an expression. In programming languages, there are two common types of operator associativity:
Left-to-Right Associativity (Associative from Left to Right):
In operators with left-to-right associativity, expressions are evaluated from left to right when operators of the same
precedence appear consecutively. This means that the leftmost operator is evaluated first, followed by the one
immediately to its right, and so on.
Example with addition (+):
int result = 5 + 3 + 2; // Evaluates as (5 + 3) + 2, result is 10
Right-to-Left Associativity (Associative from Right to Left):
In operators with right-to-left associativity, expressions are evaluated from right to left when operators of the same
precedence appear consecutively. This means that the rightmost operator is evaluated first, followed by the one
immediately to its left, and so on.
Example with assignment (=):
int a, b, c;
a = b = c = 10; // Evaluates as a = (b = (c = 10)), all variables are assigned the value 10
Examples illustrating operator
associativity

More Related Content

Similar to Operators1.pptx

C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
Chukka Nikhil Chakravarthy
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
sotlsoc
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
psaravanan1985
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
ramanathan2006
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
savitamhaske
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
ManojKhadilkar1
 
C operators
C operatorsC operators
C operators
GPERI
 
Theory3
Theory3Theory3
operators.pptx
operators.pptxoperators.pptx
operators.pptx
ruchisuru20001
 
Operators in c language
Operators in c languageOperators in c language
Operators in c language
Amit Singh
 
2. operator
2. operator2. operator
2. operator
Shankar Gangaju
 
Basics of c++
Basics of c++ Basics of c++
Basics of c++
Gunjan Mathur
 
power point presentation on topic in C++ called "OPERATORS"
power point presentation on topic in C++ called "OPERATORS"power point presentation on topic in C++ called "OPERATORS"
power point presentation on topic in C++ called "OPERATORS"
sj9399037128
 
C program
C programC program
C program
AJAL A J
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
Kathirvel Ayyaswamy
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
programming9
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
LeahRachael
 
ICP - Lecture 5
ICP - Lecture 5ICP - Lecture 5
ICP - Lecture 5
Hassaan Rahman
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
LadallaRajKumar
 

Similar to Operators1.pptx (20)

C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
C operators
C operatorsC operators
C operators
 
Theory3
Theory3Theory3
Theory3
 
operators.pptx
operators.pptxoperators.pptx
operators.pptx
 
Operators in c language
Operators in c languageOperators in c language
Operators in c language
 
2. operator
2. operator2. operator
2. operator
 
Basics of c++
Basics of c++ Basics of c++
Basics of c++
 
power point presentation on topic in C++ called "OPERATORS"
power point presentation on topic in C++ called "OPERATORS"power point presentation on topic in C++ called "OPERATORS"
power point presentation on topic in C++ called "OPERATORS"
 
C program
C programC program
C program
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
 
ICP - Lecture 5
ICP - Lecture 5ICP - Lecture 5
ICP - Lecture 5
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 

Recently uploaded

BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 

Recently uploaded (20)

BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 

Operators1.pptx

  • 2. Introduction to Operators • Definition of operators in programming: Operators in programming are symbols or special characters that are used to perform various operations on data. These operations can range from simple arithmetic calculations to complex logical comparisons and data manipulations. Operators allow programmers to work with variables and values, enabling them to create algorithms and solve problems efficiently.
  • 3. Why operators are essential in programming languages 1. Data Manipulation: Operators enable programmers to manipulate data in various ways, such as performing mathematical calculations, modifying strings, and altering the values of variables. This is crucial for solving real-world problems and performing tasks in software applications. 2. Control Flow: Operators play a key role in controlling the flow of a program. They allow for conditional statements, loops, and decision-making, making it possible to create dynamic and responsive code that can adapt to different situations. 3. Efficiency: Operators provide efficient ways to perform common operations, reducing the amount of code that needs to be written. This efficiency is vital for optimizing program performance and minimizing resource usage. 4. Expressiveness: Operators make code more concise and expressive. They allow programmers to convey complex operations in a clear and readable manner, improving code maintainability and understandability. 5. Flexibility: Different types of operators cater to various programming needs. From basic arithmetic operators to advanced bitwise and logical operators, they offer flexibility in implementing a wide range of functionalities. 6. Compatibility: Operators are a standard feature in most programming languages, making it easier for developers to switch between languages and apply their knowledge across different platforms and technologies.
  • 4. Types of Operators • Arithmetic operators • Comparison (relational) operators • Logical operators • Assignment operators • Bitwise operators • Conditional (ternary) operator • sizeof() operator
  • 5. Arithmetic Operators int main() { int num1 = 10; int num2 = 5; int result; // Addition Operator (+) result = num1 + num2; printf("Addition: %d + %d = %dn", num1, num2, result);
  • 6. Comparison Operators #include <stdio.h> int main() { int num1 = 10; int num2 = 5; // Equal to (==) printf("Is %d equal to %d? %sn", num1, num2, num1 == num2 ? "Yes" : "No"); // Not equal to (!=) printf("Is %d not equal to %d? %sn", num1, num2, num1 != num2 ? "Yes" : "No"); // Greater than (>) printf("Is %d greater than %d? %sn", num1, num2, num1 > num2 ? "Yes" : "No"); // Less than (<) printf("Is %d less than %d? %sn", num1, num2, num1 < num2 ? "Yes" : "No"); // Greater than or equal to (>=) printf("Is %d greater than or equal to %d? %sn", num1, num2, num1 >= num2 ? "Yes" : "No"); // Less than or equal to (<=) printf("Is %d less than or equal to %d? %sn", num1, num2, num1 <= num2 ? "Yes" : "No"); return 0; }
  • 7. Logical Operators #include <stdio.h> int main() { int num1 = 10; int num2 = 5; int num3 = 7; // Logical AND (&&) if (num1 > num2 && num1 > num3) { printf("%d is the largest number.n", num1); } // Logical OR (||) if (num1 > num2 || num1 > num3) { printf("%d is larger than at least one of the other numbers.n", num1); } // Logical NOT (!) if (!(num2 == num3)) { printf("%d is not equal to %d.n", num2, num3); } return 0; }
  • 8. Assignment Operators #include <stdio.h> int main() { int num1 = 10; int num2 = 5; // Assignment Operator (=) int result = num1; // Assign the value of num1 to result printf("Result after assignment: %dn", result); // Addition Assignment Operator (+=) result += num2; // Add num2 to result and update result printf("Result after addition assignment: %dn", result); // Subtraction Assignment Operator (-=) result -= num2; // Subtract num2 from result and update result printf("Result after subtraction assignment: %dn", result); // Multiplication Assignment Operator (*=) result *= num2; // Multiply result by num2 and update result printf("Result after multiplication assignment: %dn", result); // Division Assignment Operator (/=) result /= num2; // Divide result by num2 and update result printf("Result after division assignment: %dn", result); // Modulus Assignment Operator (%=) result %= num2; // Calculate result % num2 and update result printf("Result after modulus assignment: %dn", result); return 0; }
  • 9. Bitwise Operators #include <stdio.h> int main() { int num1 = 12; // Binary: 1100 int num2 = 5; // Binary: 0101 int result; // Bitwise AND Operator (&) result = num1 & num2; // Perform bitwise AND operation printf("Bitwise AND: %d & %d = %dn", num1, num2, result); // Output: 4 (Binary: 0100) // Bitwise OR Operator (|) result = num1 | num2; // Perform bitwise OR operation printf("Bitwise OR: %d | %d = %dn", num1, num2, result); // Output: 13 (Binary: 1101) // Bitwise XOR Operator (^) result = num1 ^ num2; // Perform bitwise XOR operation printf("Bitwise XOR: %d ^ %d = %dn", num1, num2, result); // Output: 9 (Binary: 1001) // Bitwise NOT Operator (~) result = ~num1; // Perform bitwise NOT operation on num1 printf("Bitwise NOT: ~%d = %dn", num1, result); // Output: - 13 (Binary: 1111011) // Left Shift Operator (<<) result = num1 << 2; // Left shift num1 by 2 bits printf("Left Shift: %d << 2 = %dn", num1, result); // Output: 48 (Binary: 110000) // Right Shift Operator (>>) result = num1 >> 2; // Right shift num1 by 2 bits printf("Right Shift: %d >> 2 = %dn", num1, result); // Output: 3 (Binary: 11) return 0; }
  • 10. Ternary Operator • The ternary operator, also known as the conditional operator in C-like programming languages, is a unique operator that allows you to write concise conditional expressions. It has the following syntax: condition ? expression_if_true : expression_if_false Here's a breakdown of how the ternary operator works: • condition: This is a boolean expression that evaluates to either true or false. • expression_if_true: If the condition is true, this expression is evaluated and returned as the result. • expression_if_false: If the condition is false, this expression is evaluated and returned as the result. The ternary operator is often used as a shorthand for simple if-else statements, providing a compact and readable way to express conditional logic.
  • 11. When to Use the Ternary Operator: • Simple Conditional Assignments:when you need to assign a value to a variable based on a condition. It's useful for avoiding the verbosity of an if-else statement in such cases. int x = 10; int y = 5; int result = (x > y) ? x : y; // The value of 'result' will be 10 because x > y is true. • Conditional Expressions in Output Statements: when you want to conditionally output different values or messages. printf("The larger number is: %dn", (x > y) ? x : y); • Conditional Expressions in Function Arguments:to pass different arguments to a function based on a condition. Example: int maxValue = getMaxValue((x > y) ? x : y);
  • 12. Example of the ternary operator in code #include <stdio.h> int main() { int num1 = 10; int num2 = 5; // Using the ternary operator to find the maximum value int max = (num1 > num2) ? num1 : num2; printf("The maximum value is: %dn", max); return 0; }
  • 13. sizeof() Operator It is used to determine the size in bytes of a data type or a variable. It returns the size as an unsigned integer. Here's how to use sizeof() to find the size of data types and variables: • Size of datatypes: #include <stdio.h> int main() { printf("Size of int: %lu bytesn", sizeof(int)); printf("Size of char: %lu bytesn", sizeof(char)); printf("Size of float: %lu bytesn", sizeof(float)); printf("Size of double: %lu bytesn", sizeof(double)); return 0;} • Size of variables #include <stdio.h> int main() { int x; double y; char z; printf("Size of x: %lu bytesn", sizeof(x)); printf("Size of y: %lu bytesn", sizeof(y)); printf("Size of z: %lu bytesn", sizeof(z)); return 0;} • Size of arrays #include <stdio.h> int main() { int x; double y; char z; printf("Size of x: %lu bytesn", sizeof(x)); printf("Size of y: %lu bytesn", sizeof(y)); printf("Size of z: %lu bytesn", sizeof(z)); return 0; }
  • 14. Operator Precedence Precedence Operator Description 1 () Parentheses (grouping) 2 [] Array subscript -> Member access (via pointer) . Member access (via object) ++, -- Post-increment, Post-decrement (type) Type casting 3 ++, -- Pre-increment, Pre-decrement +, - Unary plus, Unary minus !, ~ Logical NOT, Bitwise NOT & Address-of (unary) * Indirection (unary) sizeof Size of a type or object (type) Type casting (compound) 4 *, /, % Multiplication, Division, Modulus 5 +, - Addition, Subtraction 6 <<, >> Bitwise Left Shift, Bitwise Right Shift 7 <, <=, >, >= Relational operators: Less, Less or Equal, Greater, Greater or Equal 8 ==, != Relational operators: Equal, Not Equal 9 & Bitwise AND 10 ^ Bitwise XOR 11 | Bitwise OR 12 && Logical AND Operators with higher precedence are evaluated before operators with lower precedence. For example, in an expression like a * b + c, the multiplication (*) is evaluated before the addition (+) due to their respective precedence levels.
  • 15. Operator Associativity Operator associativity defines the order in which operators of the same precedence are evaluated when they appear consecutively in an expression. In programming languages, there are two common types of operator associativity: Left-to-Right Associativity (Associative from Left to Right): In operators with left-to-right associativity, expressions are evaluated from left to right when operators of the same precedence appear consecutively. This means that the leftmost operator is evaluated first, followed by the one immediately to its right, and so on. Example with addition (+): int result = 5 + 3 + 2; // Evaluates as (5 + 3) + 2, result is 10 Right-to-Left Associativity (Associative from Right to Left): In operators with right-to-left associativity, expressions are evaluated from right to left when operators of the same precedence appear consecutively. This means that the rightmost operator is evaluated first, followed by the one immediately to its left, and so on. Example with assignment (=): int a, b, c; a = b = c = 10; // Evaluates as a = (b = (c = 10)), all variables are assigned the value 10