SlideShare a Scribd company logo
1 of 24
What are Operators?
• Operators are needed to transform or
manipulate data
• Mathematical computations, comparison
of two values or setting initial values of
variables are made possible by using
arithmetic or relational operators
• Operators are important in that a
miscalculation, missed value or wrong
comparison may compromise the integrity
of a whole module or a whole program
What are Operators?
• The two types of
operators in this section
are:
– Arithmetic operators
– Relational operators
What are Operators?
• An arithmetic or logical expression is
formed by a combination of variables
or literals and an operator
• Format for an arithmetic or logical
expression:
< operand1 > operator < operand2 >
where operand1 or operand2 could be
any literal or variable name
Arithmetic Operators
Arithmetic operators perform mathematical
calculations on two numeric operands
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after Division
Arithmetic Operators
• Examples of arithmetic expressions:
length * width
12.345 + 67.893 + 75.9004
• Expressions with mixed operands are evaluated
using RULES OF PRECEDENCE / ORDER OF PRECEDENCE
• Rules of precedence dictate the order of
evaluation. In this order, certain parts of the
expression gets executed first before others
• For arithmetic operators, the rules of
precedence (or order of evaluation) are:
1. Expressions in parentheses
2. Multiplication and Division, from left to right
3. Addition and subtraction, from left to right
• The assignment operator “=“ has the
lowest precedence
Arithmetic Operators
• In the expression,
answer = 4 + 6 * 2
the result of the expression
would be 16.
Arithmetic Operators
Arithmetic Operators
• However, if it were modified to:
answer = (4 + 6) * 2
The result of the expression
would be 20. This value would
then be assigned to the variable
answer.
Exercise 7.1
Arithmetic Operators
1.Which of the following evaluates to 10?
r=3, o=5, s=2, e=4, g=20, f=25
a. 3 + 5 * 2 c. 25 / 2 + 3
b. 5 + 20 / 4 d. all of the above
2. What is the value of the expression
27 – 7 * 3 / 3?
TO BE DISCUSS
public class RoseArithmetic
{
public static void main (String []args )
{ int a=3, b=5, c=2, d=4, e=20, f=25;
int ans;
ans = a + b * c;
System.out.println("a:" + ans);
ans = b + e / d;
System.out.println("b:" + ans);
ans = f / c + a;
System.out.println("c:" + ans);
ans = 27 - 7 * 3 /3 ;
System.out.println("2:" + ans);
}
}
TO BE DISCUSS
Increment and Decrement Operators
Java also has unary increment and
decrement operators that increase or
decrease the value of a variable by 1
Increment operator < operand1 > ++
Decrement operator < operand1 > --
Where operand1 could be any
literal or variable name
Increment and
Decrement Operators
• Example:
the expression,
count = count + 1;
could be written as
count ++;
• Increment and Decrement operators could
be POSTFIX or PREFIX as shown below
Operator Use Description
++ op++ the value of op was evaluated before it
was incremented
++ ++op the value of op was evaluated after it
was incremented
-- op-- the value of op was evaluated before it
was incremented
-- --op the value of op was evaluated after it
was incremented
Increment and
Decrement Operators
Increment and
Decrement Operators
for(start;end;operand)
Sample
for(int b2=24;b2>=1;b2--)
Increment and
Decrement Operators
for(int b2=1;b2<=24;b2++)
Increment
Decrement
for(int b2=24;b2>=1;b2--)
Exercise 7.2 Increment and
Decrement Operators
public class RoseIncrement {
public static void main(String[] args)
{
String roseName ="ROSEMARIESIBBALUCAGUIRRE",increment;
for(int b2=1;b2<=24;b2++)
{
increment = roseName.substring(0,b2);
System.out.println(increment);
}
}
}
TO BE DISCUSS
Exercise 7.2 Increment and
Decrement Operators
public class RoseDecrement {
public static void main(String[] args)
{
String roseName ="ROSEMARIESIBBALUCAGUIRRE",decrement;
for(int b2=24;b2>=1;b2--)
{
decrement = roseName.substring(0,b2);
System.out.println(decrement);
}
}
}
TO BE DISCUSS
Relational Operators
• Relational or conditional operators
perform comparison of two literals or
two variables, or any combination of
both.
• The evaluation of a conditional
expression results in a boolean value
of either true or false
Relational Operators
• Example: the expression
age > 18
will evaluate to only one value: true or
false
OPERATOR NAME DESCRIPTION
== Equal to Evaluates as true if its operands are
equivalent
> Greater than Evaluates as true when the left
operand is greater than the right
operand
< Less than Evaluates as true when the left
operand is less than the right
operand
Relational Operators
OPERATOR NAME DESCRIPTION
>= Greater than or
equal to
Evaluates as true when the left
operand is greater than or equal
to the right operand
<= Less than or
equal to
Evaluates as true when the left
operand is less than or equal to
the right operand
!= Not equal to Evaluates as true when both
operands are not equal
Relational Operators
Exercise 7.3
Relational Operators
• Determine the result of the following
conditional expressions, given the following
variables and their values:
int roseR =250; int roseO =350;
int roseS =300; int roseE = 250;
1. roseR > roseO 3. roseO <= roseS
2. roseR >= roseE 4. roseS != roseE
TO BE DISCUSS
int roseR = 250; int roseO = 350; int roseS =300; int roseE = 250;
//1. roseR > roseO 2. roseR >= roseE 3. roseO <= roseS 4. roseS != roseE
if (roseR > roseO)
{
System.out.println ("TRUE");
}
else
{
System.out.println("FALSE");
}
TO BE DISCUSS

More Related Content

What's hot

CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++Pranav Ghildiyal
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
C OPERATOR
C OPERATORC OPERATOR
C OPERATORrricky98
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++Online
 
Operators in c language
Operators in c languageOperators in c language
Operators in c languageAmit Singh
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programmingsavitamhaske
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Deepak Singh
 
Lecture#2 Computer languages computer system and Programming EC-105
Lecture#2 Computer languages computer system and Programming EC-105Lecture#2 Computer languages computer system and Programming EC-105
Lecture#2 Computer languages computer system and Programming EC-105NUST Stuff
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressionsvishaljot_kaur
 
Unit 1-problem solving with algorithm
Unit 1-problem solving with algorithmUnit 1-problem solving with algorithm
Unit 1-problem solving with algorithmrajkumar1631010038
 
Class 2 variables, classes methods...
Class 2   variables, classes methods...Class 2   variables, classes methods...
Class 2 variables, classes methods...Fernando Loizides
 

What's hot (18)

Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
COM1407: C Operators
COM1407: C OperatorsCOM1407: C Operators
COM1407: C Operators
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
C OPERATOR
C OPERATORC OPERATOR
C OPERATOR
 
Operators
OperatorsOperators
Operators
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++
 
Operators in c language
Operators in c languageOperators in c language
Operators in c language
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
 
Lecture#2 Computer languages computer system and Programming EC-105
Lecture#2 Computer languages computer system and Programming EC-105Lecture#2 Computer languages computer system and Programming EC-105
Lecture#2 Computer languages computer system and Programming EC-105
 
Relational operators
Relational operatorsRelational operators
Relational operators
 
What are operators?
What are operators? What are operators?
What are operators?
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
 
Unit 1-problem solving with algorithm
Unit 1-problem solving with algorithmUnit 1-problem solving with algorithm
Unit 1-problem solving with algorithm
 
Operators in c++
Operators in c++Operators in c++
Operators in c++
 
Class 2 variables, classes methods...
Class 2   variables, classes methods...Class 2   variables, classes methods...
Class 2 variables, classes methods...
 
C# Fundamentals - Basics of OOPS - Part 2
C# Fundamentals - Basics of OOPS - Part 2C# Fundamentals - Basics of OOPS - Part 2
C# Fundamentals - Basics of OOPS - Part 2
 

Similar to Chapter 7: Arithmetic and Relational Operators

Similar to Chapter 7: Arithmetic and Relational Operators (20)

Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
 
operators.pptx
operators.pptxoperators.pptx
operators.pptx
 
C++
C++ C++
C++
 
operator
operatoroperator
operator
 
Class_IX_Operators.pptx
Class_IX_Operators.pptxClass_IX_Operators.pptx
Class_IX_Operators.pptx
 
lecture4 pgdca.pptx
lecture4 pgdca.pptxlecture4 pgdca.pptx
lecture4 pgdca.pptx
 
Oop using JAVA
Oop using JAVAOop using JAVA
Oop using JAVA
 
Operator precedence and associativity
Operator precedence and associativityOperator precedence and associativity
Operator precedence and associativity
 
Operator precedence and associativity
Operator precedence and associativityOperator precedence and associativity
Operator precedence and associativity
 
Report Group 4 Constants and Variables
Report Group 4 Constants and VariablesReport Group 4 Constants and Variables
Report Group 4 Constants and Variables
 
Report Group 4 Constants and Variables(TLE)
Report Group 4 Constants and Variables(TLE)Report Group 4 Constants and Variables(TLE)
Report Group 4 Constants and Variables(TLE)
 
Project in TLE
Project in TLEProject in TLE
Project in TLE
 
3306617
33066173306617
3306617
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
C++ Expressions Notes
C++ Expressions NotesC++ Expressions Notes
C++ Expressions Notes
 
Coper in C
Coper in CCoper in C
Coper in C
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
Operators and Expressions
Operators and ExpressionsOperators and Expressions
Operators and Expressions
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 

More from Dr. Rosemarie Sibbaluca-Guirre

More from Dr. Rosemarie Sibbaluca-Guirre (20)

Korean Language: Culture 한국어 개요
Korean Language: Culture 한국어 개요Korean Language: Culture 한국어 개요
Korean Language: Culture 한국어 개요
 
Korean Language Overview 한국어 개요
Korean Language Overview 한국어 개요Korean Language Overview 한국어 개요
Korean Language Overview 한국어 개요
 
Conjunction 접속사
Conjunction   접속사Conjunction   접속사
Conjunction 접속사
 
Pronoun 대명사
Pronoun  대명사Pronoun  대명사
Pronoun 대명사
 
Usage of Particles 입자의 사용
Usage of Particles 입자의 사용Usage of Particles 입자의 사용
Usage of Particles 입자의 사용
 
Usage of Particles 입자의 사용
Usage of Particles 입자의 사용Usage of Particles 입자의 사용
Usage of Particles 입자의 사용
 
Korean Word Order 한국어 단어 순서
Korean Word Order 한국어 단어 순서Korean Word Order 한국어 단어 순서
Korean Word Order 한국어 단어 순서
 
Korean Number 한국 번호
Korean Number 한국 번호Korean Number 한국 번호
Korean Number 한국 번호
 
ISAD 313-3_ TOOLS OF THE SYSTEM ANALYSIS.pptx
ISAD 313-3_ TOOLS OF THE SYSTEM ANALYSIS.pptxISAD 313-3_ TOOLS OF THE SYSTEM ANALYSIS.pptx
ISAD 313-3_ TOOLS OF THE SYSTEM ANALYSIS.pptx
 
ISAD 313-1_INTRODUCTION TO SYSTEMS.pptx
ISAD 313-1_INTRODUCTION TO SYSTEMS.pptxISAD 313-1_INTRODUCTION TO SYSTEMS.pptx
ISAD 313-1_INTRODUCTION TO SYSTEMS.pptx
 
ISAD 313-2_ SYSTEM ANALYSIS.pptx
ISAD 313-2_ SYSTEM ANALYSIS.pptxISAD 313-2_ SYSTEM ANALYSIS.pptx
ISAD 313-2_ SYSTEM ANALYSIS.pptx
 
ISAD 313-4_ RESEARCH PROJECT.pptx
ISAD 313-4_ RESEARCH PROJECT.pptxISAD 313-4_ RESEARCH PROJECT.pptx
ISAD 313-4_ RESEARCH PROJECT.pptx
 
ISAD 313-3_ SYSTEM FLOW.pptx
ISAD 313-3_ SYSTEM FLOW.pptxISAD 313-3_ SYSTEM FLOW.pptx
ISAD 313-3_ SYSTEM FLOW.pptx
 
ISAD 313-3_ MODELS.pptx
ISAD 313-3_ MODELS.pptxISAD 313-3_ MODELS.pptx
ISAD 313-3_ MODELS.pptx
 
ACCT11_9_Financial Position.pptx
ACCT11_9_Financial Position.pptxACCT11_9_Financial Position.pptx
ACCT11_9_Financial Position.pptx
 
ACCT11_8_Equity.pptx
ACCT11_8_Equity.pptxACCT11_8_Equity.pptx
ACCT11_8_Equity.pptx
 
ACCT11_7_Performance.pptx
ACCT11_7_Performance.pptxACCT11_7_Performance.pptx
ACCT11_7_Performance.pptx
 
ACCT11_6_Worksheet.pptx
ACCT11_6_Worksheet.pptxACCT11_6_Worksheet.pptx
ACCT11_6_Worksheet.pptx
 
ACCT11_5_Adjusting Entries.pptx
ACCT11_5_Adjusting Entries.pptxACCT11_5_Adjusting Entries.pptx
ACCT11_5_Adjusting Entries.pptx
 
ACCT11_4_Trial Balance.pptx
ACCT11_4_Trial Balance.pptxACCT11_4_Trial Balance.pptx
ACCT11_4_Trial Balance.pptx
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

Chapter 7: Arithmetic and Relational Operators

  • 1.
  • 2. What are Operators? • Operators are needed to transform or manipulate data • Mathematical computations, comparison of two values or setting initial values of variables are made possible by using arithmetic or relational operators • Operators are important in that a miscalculation, missed value or wrong comparison may compromise the integrity of a whole module or a whole program
  • 3. What are Operators? • The two types of operators in this section are: – Arithmetic operators – Relational operators
  • 4. What are Operators? • An arithmetic or logical expression is formed by a combination of variables or literals and an operator • Format for an arithmetic or logical expression: < operand1 > operator < operand2 > where operand1 or operand2 could be any literal or variable name
  • 5. Arithmetic Operators Arithmetic operators perform mathematical calculations on two numeric operands Operator Description + Addition - Subtraction * Multiplication / Division % Remainder after Division
  • 6. Arithmetic Operators • Examples of arithmetic expressions: length * width 12.345 + 67.893 + 75.9004 • Expressions with mixed operands are evaluated using RULES OF PRECEDENCE / ORDER OF PRECEDENCE • Rules of precedence dictate the order of evaluation. In this order, certain parts of the expression gets executed first before others
  • 7. • For arithmetic operators, the rules of precedence (or order of evaluation) are: 1. Expressions in parentheses 2. Multiplication and Division, from left to right 3. Addition and subtraction, from left to right • The assignment operator “=“ has the lowest precedence Arithmetic Operators
  • 8. • In the expression, answer = 4 + 6 * 2 the result of the expression would be 16. Arithmetic Operators
  • 9. Arithmetic Operators • However, if it were modified to: answer = (4 + 6) * 2 The result of the expression would be 20. This value would then be assigned to the variable answer.
  • 10. Exercise 7.1 Arithmetic Operators 1.Which of the following evaluates to 10? r=3, o=5, s=2, e=4, g=20, f=25 a. 3 + 5 * 2 c. 25 / 2 + 3 b. 5 + 20 / 4 d. all of the above 2. What is the value of the expression 27 – 7 * 3 / 3? TO BE DISCUSS
  • 11. public class RoseArithmetic { public static void main (String []args ) { int a=3, b=5, c=2, d=4, e=20, f=25; int ans; ans = a + b * c; System.out.println("a:" + ans); ans = b + e / d; System.out.println("b:" + ans); ans = f / c + a; System.out.println("c:" + ans); ans = 27 - 7 * 3 /3 ; System.out.println("2:" + ans); } } TO BE DISCUSS
  • 12. Increment and Decrement Operators Java also has unary increment and decrement operators that increase or decrease the value of a variable by 1 Increment operator < operand1 > ++ Decrement operator < operand1 > -- Where operand1 could be any literal or variable name
  • 13. Increment and Decrement Operators • Example: the expression, count = count + 1; could be written as count ++; • Increment and Decrement operators could be POSTFIX or PREFIX as shown below
  • 14. Operator Use Description ++ op++ the value of op was evaluated before it was incremented ++ ++op the value of op was evaluated after it was incremented -- op-- the value of op was evaluated before it was incremented -- --op the value of op was evaluated after it was incremented Increment and Decrement Operators
  • 16. Increment and Decrement Operators for(int b2=1;b2<=24;b2++) Increment Decrement for(int b2=24;b2>=1;b2--)
  • 17. Exercise 7.2 Increment and Decrement Operators public class RoseIncrement { public static void main(String[] args) { String roseName ="ROSEMARIESIBBALUCAGUIRRE",increment; for(int b2=1;b2<=24;b2++) { increment = roseName.substring(0,b2); System.out.println(increment); } } } TO BE DISCUSS
  • 18. Exercise 7.2 Increment and Decrement Operators public class RoseDecrement { public static void main(String[] args) { String roseName ="ROSEMARIESIBBALUCAGUIRRE",decrement; for(int b2=24;b2>=1;b2--) { decrement = roseName.substring(0,b2); System.out.println(decrement); } } } TO BE DISCUSS
  • 19. Relational Operators • Relational or conditional operators perform comparison of two literals or two variables, or any combination of both. • The evaluation of a conditional expression results in a boolean value of either true or false
  • 20. Relational Operators • Example: the expression age > 18 will evaluate to only one value: true or false
  • 21. OPERATOR NAME DESCRIPTION == Equal to Evaluates as true if its operands are equivalent > Greater than Evaluates as true when the left operand is greater than the right operand < Less than Evaluates as true when the left operand is less than the right operand Relational Operators
  • 22. OPERATOR NAME DESCRIPTION >= Greater than or equal to Evaluates as true when the left operand is greater than or equal to the right operand <= Less than or equal to Evaluates as true when the left operand is less than or equal to the right operand != Not equal to Evaluates as true when both operands are not equal Relational Operators
  • 23. Exercise 7.3 Relational Operators • Determine the result of the following conditional expressions, given the following variables and their values: int roseR =250; int roseO =350; int roseS =300; int roseE = 250; 1. roseR > roseO 3. roseO <= roseS 2. roseR >= roseE 4. roseS != roseE TO BE DISCUSS
  • 24. int roseR = 250; int roseO = 350; int roseS =300; int roseE = 250; //1. roseR > roseO 2. roseR >= roseE 3. roseO <= roseS 4. roseS != roseE if (roseR > roseO) { System.out.println ("TRUE"); } else { System.out.println("FALSE"); } TO BE DISCUSS