SlideShare a Scribd company logo
Computer Science: A Structured Programming Approach Using C 1
Objectives
❏ To be able to list and describe the six expression categories
❏ To understand the rules of precedence and associativity in
evaluating expressions
❏ To understand the result of side effects in expression evaluation
❏ To be able to predict the results when an expression is evaluated
❏ To understand implicit and explicit type conversion
❏ To understand and use the first four statement types: null,
expression, return, and compound
Chapter 3
Structure of a C Program
Computer Science: A Structured Programming Approach Using C 2
3-1 Expressions
An expression is a sequence of operands and operators
that reduces to a single value. Expressions can be
simple or complex. An operator is a syntactical token
that requires an action be taken. An operand is an
object on which an operation is performed; it receives
an operator’s action.
Primary Expressions
Postfix Expressions
Prefix Expressions
Unary Expressions
Binary Expressions
Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C 3
An expression always reduces to a single value.
Note
Computer Science: A Structured Programming Approach Using C 4
FIGURE 3-2 Postfix Expressions
Computer Science: A Structured Programming Approach Using C 5
(a++) has the same effect as (a = a + 1)
Note
Computer Science: A Structured Programming Approach Using C 6
FIGURE 3-3 Result of Postfix a++
Computer Science: A Structured Programming Approach Using C 7
The operand in a postfix expression must be a variable.
Note
Computer Science: A Structured Programming Approach Using C 8
PROGRAM 3-1 Demonstrate Postfix Increment
Computer Science: A Structured Programming Approach Using C 9
PROGRAM 3-1 Demonstrate Postfix Increment (continued)
Computer Science: A Structured Programming Approach Using C 10
FIGURE 3-4 Prefix Expression
Computer Science: A Structured Programming Approach Using C 11
The operand of a prefix expression must be a variable.
Note
Computer Science: A Structured Programming Approach Using C 12
FIGURE 3-5 Result of Prefix ++a
Computer Science: A Structured Programming Approach Using C 13
(++a) has the same effect as (a = a + 1)
Note
Computer Science: A Structured Programming Approach Using C 14
PROGRAM 3-2 Demonstrate Prefix Increment
Computer Science: A Structured Programming Approach Using C 15
PROGRAM 3-2 Demonstrate Prefix Increment (continued)
Computer Science: A Structured Programming Approach Using C 16
If ++ is after the operand, as in a++, the increment takes
place after the expression is evaluated.
If ++ is before the operand, as in ++a, the increment
takes place before the expression is evaluated.
Note
Computer Science: A Structured Programming Approach Using C 17
FIGURE 3-6 Unary Expressions
Computer Science: A Structured Programming Approach Using C 18
Table 3-1 Examples of Unary Plus And Minus Expressions
Computer Science: A Structured Programming Approach Using C 19
FIGURE 3-7 Binary Expressions
Computer Science: A Structured Programming Approach Using C 20
Both operands of the modulo operator (%) must
be integral types.
Note
Computer Science: A Structured Programming Approach Using C 21
PROGRAM 3-3 Binary Expressions
Computer Science: A Structured Programming Approach Using C 22
PROGRAM 3-3 Binary Expressions (continued)
Computer Science: A Structured Programming Approach Using C 23
PROGRAM 3-3 Binary Expressions (continued)
Computer Science: A Structured Programming Approach Using C 24
The left operand in an assignment expression must be a
single variable.
Note
Computer Science: A Structured Programming Approach Using C 25
Table 3-2 Expansion of Compound Expressions
Computer Science: A Structured Programming Approach Using C 26
PROGRAM 3-4 Demonstration of Compound Assignments
Computer Science: A Structured Programming Approach Using C 27
PROGRAM 3-4 Demonstration of Compound Assignments
Computer Science: A Structured Programming Approach Using C 28
PROGRAM 3-4 Demonstration of Compound Assignments
Computer Science: A Structured Programming Approach Using C 29
3-2 Precedence and Associativity
Precedence is used to determine the order in which
different operators in a complex expression are
evaluated. Associativity is used to determine the order
in which operators with the same precedence are
evaluated in a complex expression.
Precedence
Associativity
Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C 30
PROGRAM 3-5 Precedence
Computer Science: A Structured Programming Approach Using C 31
PROGRAM 3-5 Precedence
Computer Science: A Structured Programming Approach Using C 32
Computer Science: A Structured Programming Approach Using C 33
FIGURE 3-8 Left-to-Right Associativity
Computer Science: A Structured Programming Approach Using C 34
FIGURE 3-9 Right-to-Left Associativity
So you can have things like:
int i =1;
int j =2;
i = j*= 2; // i = j = 4 after
i += i + j+=2; // j=6, i = 10
// after
Computer Science: A Structured Programming Approach Using C 35
You can actually say
int i, sum;
i = sum = 3 +1; // Right to left
// associativity
And i will equal 4, but it is not
good programming practice.
Computer Science: A Structured Programming Approach Using C 36

More Related Content

Similar to Chap-03-1.ppt

Chap-02-1.ppt
Chap-02-1.pptChap-02-1.ppt
Chap-02-1.ppt
ssusere6f5a11
 
Chap 02-1
Chap 02-1Chap 02-1
Chap 02-1
Navjot Singh
 
Chap-02-1.ppt
Chap-02-1.pptChap-02-1.ppt
Chap-02-1.ppt
AbdulSacur2
 
Csc1100 lecture01 ch01 pt2-paradigm (1)
Csc1100 lecture01 ch01 pt2-paradigm (1)Csc1100 lecture01 ch01 pt2-paradigm (1)
Csc1100 lecture01 ch01 pt2-paradigm (1)IIUM
 
Csc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigmCsc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigm
IIUM
 
intro to differnt oper.pptx
intro to differnt oper.pptxintro to differnt oper.pptx
intro to differnt oper.pptx
ssuser5ad1571
 
Lecture 3.2.4 C pointer to Structure.pptx
Lecture 3.2.4 C pointer to Structure.pptxLecture 3.2.4 C pointer to Structure.pptx
Lecture 3.2.4 C pointer to Structure.pptx
ravi2692kumar
 
C material
C materialC material
C material
tarique472
 
Chap-14-1 (1).ppt
Chap-14-1 (1).pptChap-14-1 (1).ppt
Chap-14-1 (1).ppt
OmPrakasDas
 
Variable-intro.pptx
Variable-intro.pptxVariable-intro.pptx
Variable-intro.pptx
ssuser5ad1571
 
Chap-08-4.ppt
Chap-08-4.pptChap-08-4.ppt
Chap-08-4.ppt
valerie5142000
 
additional.pptx
additional.pptxadditional.pptx
additional.pptx
Yuvraj994432
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
MLG College of Learning, Inc
 
C basics
C basicsC basics
C basics
sridevi5983
 
C basics
C basicsC basics
C basics
sridevi5983
 
Jeremiah Yancy - Objectives for Software design and testing
Jeremiah Yancy - Objectives for Software design and testingJeremiah Yancy - Objectives for Software design and testing
Jeremiah Yancy - Objectives for Software design and testing
Jeremiah Yancy
 
Computer Programming Past papers for DIT
Computer Programming Past papers for DITComputer Programming Past papers for DIT
Computer Programming Past papers for DIT
ChahatNoor
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdf
madihamaqbool6
 

Similar to Chap-03-1.ppt (20)

Chap-02-1.ppt
Chap-02-1.pptChap-02-1.ppt
Chap-02-1.ppt
 
Chap 02-1
Chap 02-1Chap 02-1
Chap 02-1
 
Chap-02-1.ppt
Chap-02-1.pptChap-02-1.ppt
Chap-02-1.ppt
 
Csc1100 lecture01 ch01 pt2-paradigm (1)
Csc1100 lecture01 ch01 pt2-paradigm (1)Csc1100 lecture01 ch01 pt2-paradigm (1)
Csc1100 lecture01 ch01 pt2-paradigm (1)
 
Csc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigmCsc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigm
 
intro to differnt oper.pptx
intro to differnt oper.pptxintro to differnt oper.pptx
intro to differnt oper.pptx
 
Lecture 3.2.4 C pointer to Structure.pptx
Lecture 3.2.4 C pointer to Structure.pptxLecture 3.2.4 C pointer to Structure.pptx
Lecture 3.2.4 C pointer to Structure.pptx
 
Intro
IntroIntro
Intro
 
C material
C materialC material
C material
 
Chap-14-1 (1).ppt
Chap-14-1 (1).pptChap-14-1 (1).ppt
Chap-14-1 (1).ppt
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
Variable-intro.pptx
Variable-intro.pptxVariable-intro.pptx
Variable-intro.pptx
 
Chap-08-4.ppt
Chap-08-4.pptChap-08-4.ppt
Chap-08-4.ppt
 
additional.pptx
additional.pptxadditional.pptx
additional.pptx
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
C basics
C basicsC basics
C basics
 
C basics
C basicsC basics
C basics
 
Jeremiah Yancy - Objectives for Software design and testing
Jeremiah Yancy - Objectives for Software design and testingJeremiah Yancy - Objectives for Software design and testing
Jeremiah Yancy - Objectives for Software design and testing
 
Computer Programming Past papers for DIT
Computer Programming Past papers for DITComputer Programming Past papers for DIT
Computer Programming Past papers for DIT
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdf
 

Recently uploaded

Chapter 12 - climate change and the energy crisis
Chapter 12 - climate change and the energy crisisChapter 12 - climate change and the energy crisis
Chapter 12 - climate change and the energy crisis
tonzsalvador2222
 
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
Sérgio Sacani
 
erythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptxerythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptx
muralinath2
 
Orion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWSOrion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWS
Columbia Weather Systems
 
Hemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptxHemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptx
muralinath2
 
nodule formation by alisha dewangan.pptx
nodule formation by alisha dewangan.pptxnodule formation by alisha dewangan.pptx
nodule formation by alisha dewangan.pptx
alishadewangan1
 
GBSN - Microbiology (Lab 4) Culture Media
GBSN - Microbiology (Lab 4) Culture MediaGBSN - Microbiology (Lab 4) Culture Media
GBSN - Microbiology (Lab 4) Culture Media
Areesha Ahmad
 
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATIONPRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
ChetanK57
 
Hemoglobin metabolism_pathophysiology.pptx
Hemoglobin metabolism_pathophysiology.pptxHemoglobin metabolism_pathophysiology.pptx
Hemoglobin metabolism_pathophysiology.pptx
muralinath2
 
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptxBody fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
muralinath2
 
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
yqqaatn0
 
Deep Software Variability and Frictionless Reproducibility
Deep Software Variability and Frictionless ReproducibilityDeep Software Variability and Frictionless Reproducibility
Deep Software Variability and Frictionless Reproducibility
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
bordetella pertussis.................................ppt
bordetella pertussis.................................pptbordetella pertussis.................................ppt
bordetella pertussis.................................ppt
kejapriya1
 
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
University of Maribor
 
role of pramana in research.pptx in science
role of pramana in research.pptx in sciencerole of pramana in research.pptx in science
role of pramana in research.pptx in science
sonaliswain16
 
Toxic effects of heavy metals : Lead and Arsenic
Toxic effects of heavy metals : Lead and ArsenicToxic effects of heavy metals : Lead and Arsenic
Toxic effects of heavy metals : Lead and Arsenic
sanjana502982
 
platelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptxplatelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptx
muralinath2
 
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of LipidsGBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
Areesha Ahmad
 
Richard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlandsRichard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlands
Richard Gill
 
extra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdfextra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdf
DiyaBiswas10
 

Recently uploaded (20)

Chapter 12 - climate change and the energy crisis
Chapter 12 - climate change and the energy crisisChapter 12 - climate change and the energy crisis
Chapter 12 - climate change and the energy crisis
 
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
THE IMPORTANCE OF MARTIAN ATMOSPHERE SAMPLE RETURN.
 
erythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptxerythropoiesis-I_mechanism& clinical significance.pptx
erythropoiesis-I_mechanism& clinical significance.pptx
 
Orion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWSOrion Air Quality Monitoring Systems - CWS
Orion Air Quality Monitoring Systems - CWS
 
Hemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptxHemostasis_importance& clinical significance.pptx
Hemostasis_importance& clinical significance.pptx
 
nodule formation by alisha dewangan.pptx
nodule formation by alisha dewangan.pptxnodule formation by alisha dewangan.pptx
nodule formation by alisha dewangan.pptx
 
GBSN - Microbiology (Lab 4) Culture Media
GBSN - Microbiology (Lab 4) Culture MediaGBSN - Microbiology (Lab 4) Culture Media
GBSN - Microbiology (Lab 4) Culture Media
 
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATIONPRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
 
Hemoglobin metabolism_pathophysiology.pptx
Hemoglobin metabolism_pathophysiology.pptxHemoglobin metabolism_pathophysiology.pptx
Hemoglobin metabolism_pathophysiology.pptx
 
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptxBody fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
Body fluids_tonicity_dehydration_hypovolemia_hypervolemia.pptx
 
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
如何办理(uvic毕业证书)维多利亚大学毕业证本科学位证书原版一模一样
 
Deep Software Variability and Frictionless Reproducibility
Deep Software Variability and Frictionless ReproducibilityDeep Software Variability and Frictionless Reproducibility
Deep Software Variability and Frictionless Reproducibility
 
bordetella pertussis.................................ppt
bordetella pertussis.................................pptbordetella pertussis.................................ppt
bordetella pertussis.................................ppt
 
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
Comparing Evolved Extractive Text Summary Scores of Bidirectional Encoder Rep...
 
role of pramana in research.pptx in science
role of pramana in research.pptx in sciencerole of pramana in research.pptx in science
role of pramana in research.pptx in science
 
Toxic effects of heavy metals : Lead and Arsenic
Toxic effects of heavy metals : Lead and ArsenicToxic effects of heavy metals : Lead and Arsenic
Toxic effects of heavy metals : Lead and Arsenic
 
platelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptxplatelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptx
 
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of LipidsGBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
 
Richard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlandsRichard's aventures in two entangled wonderlands
Richard's aventures in two entangled wonderlands
 
extra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdfextra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdf
 

Chap-03-1.ppt

  • 1. Computer Science: A Structured Programming Approach Using C 1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions ❏ To understand the result of side effects in expression evaluation ❏ To be able to predict the results when an expression is evaluated ❏ To understand implicit and explicit type conversion ❏ To understand and use the first four statement types: null, expression, return, and compound Chapter 3 Structure of a C Program
  • 2. Computer Science: A Structured Programming Approach Using C 2 3-1 Expressions An expression is a sequence of operands and operators that reduces to a single value. Expressions can be simple or complex. An operator is a syntactical token that requires an action be taken. An operand is an object on which an operation is performed; it receives an operator’s action. Primary Expressions Postfix Expressions Prefix Expressions Unary Expressions Binary Expressions Topics discussed in this section:
  • 3. Computer Science: A Structured Programming Approach Using C 3 An expression always reduces to a single value. Note
  • 4. Computer Science: A Structured Programming Approach Using C 4 FIGURE 3-2 Postfix Expressions
  • 5. Computer Science: A Structured Programming Approach Using C 5 (a++) has the same effect as (a = a + 1) Note
  • 6. Computer Science: A Structured Programming Approach Using C 6 FIGURE 3-3 Result of Postfix a++
  • 7. Computer Science: A Structured Programming Approach Using C 7 The operand in a postfix expression must be a variable. Note
  • 8. Computer Science: A Structured Programming Approach Using C 8 PROGRAM 3-1 Demonstrate Postfix Increment
  • 9. Computer Science: A Structured Programming Approach Using C 9 PROGRAM 3-1 Demonstrate Postfix Increment (continued)
  • 10. Computer Science: A Structured Programming Approach Using C 10 FIGURE 3-4 Prefix Expression
  • 11. Computer Science: A Structured Programming Approach Using C 11 The operand of a prefix expression must be a variable. Note
  • 12. Computer Science: A Structured Programming Approach Using C 12 FIGURE 3-5 Result of Prefix ++a
  • 13. Computer Science: A Structured Programming Approach Using C 13 (++a) has the same effect as (a = a + 1) Note
  • 14. Computer Science: A Structured Programming Approach Using C 14 PROGRAM 3-2 Demonstrate Prefix Increment
  • 15. Computer Science: A Structured Programming Approach Using C 15 PROGRAM 3-2 Demonstrate Prefix Increment (continued)
  • 16. Computer Science: A Structured Programming Approach Using C 16 If ++ is after the operand, as in a++, the increment takes place after the expression is evaluated. If ++ is before the operand, as in ++a, the increment takes place before the expression is evaluated. Note
  • 17. Computer Science: A Structured Programming Approach Using C 17 FIGURE 3-6 Unary Expressions
  • 18. Computer Science: A Structured Programming Approach Using C 18 Table 3-1 Examples of Unary Plus And Minus Expressions
  • 19. Computer Science: A Structured Programming Approach Using C 19 FIGURE 3-7 Binary Expressions
  • 20. Computer Science: A Structured Programming Approach Using C 20 Both operands of the modulo operator (%) must be integral types. Note
  • 21. Computer Science: A Structured Programming Approach Using C 21 PROGRAM 3-3 Binary Expressions
  • 22. Computer Science: A Structured Programming Approach Using C 22 PROGRAM 3-3 Binary Expressions (continued)
  • 23. Computer Science: A Structured Programming Approach Using C 23 PROGRAM 3-3 Binary Expressions (continued)
  • 24. Computer Science: A Structured Programming Approach Using C 24 The left operand in an assignment expression must be a single variable. Note
  • 25. Computer Science: A Structured Programming Approach Using C 25 Table 3-2 Expansion of Compound Expressions
  • 26. Computer Science: A Structured Programming Approach Using C 26 PROGRAM 3-4 Demonstration of Compound Assignments
  • 27. Computer Science: A Structured Programming Approach Using C 27 PROGRAM 3-4 Demonstration of Compound Assignments
  • 28. Computer Science: A Structured Programming Approach Using C 28 PROGRAM 3-4 Demonstration of Compound Assignments
  • 29. Computer Science: A Structured Programming Approach Using C 29 3-2 Precedence and Associativity Precedence is used to determine the order in which different operators in a complex expression are evaluated. Associativity is used to determine the order in which operators with the same precedence are evaluated in a complex expression. Precedence Associativity Topics discussed in this section:
  • 30. Computer Science: A Structured Programming Approach Using C 30 PROGRAM 3-5 Precedence
  • 31. Computer Science: A Structured Programming Approach Using C 31 PROGRAM 3-5 Precedence
  • 32. Computer Science: A Structured Programming Approach Using C 32
  • 33. Computer Science: A Structured Programming Approach Using C 33 FIGURE 3-8 Left-to-Right Associativity
  • 34. Computer Science: A Structured Programming Approach Using C 34 FIGURE 3-9 Right-to-Left Associativity
  • 35. So you can have things like: int i =1; int j =2; i = j*= 2; // i = j = 4 after i += i + j+=2; // j=6, i = 10 // after Computer Science: A Structured Programming Approach Using C 35
  • 36. You can actually say int i, sum; i = sum = 3 +1; // Right to left // associativity And i will equal 4, but it is not good programming practice. Computer Science: A Structured Programming Approach Using C 36

Editor's Notes

  1. Associativity used when operators have same precedence: the += type operatorrs use R->L associativity, basic math operators like * use L->R