1 / 8

Computer Programming CS F111

Computer Programming CS F111. Vishal Gupta Department of Computer Science and Information Systems Birla Institute of Technology and Science Pilani Campus, Pilani . Agenda :. Expression Evaluation: Precedence & Associativity. Precedence and Associativity ?.

damara
Download Presentation

Computer Programming CS F111

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer ProgrammingCS F111 Vishal Gupta Department of Computer Science and Information Systems Birla Institute of Technology and Science Pilani Campus, Pilani

  2. Agenda: Expression Evaluation: Precedence & Associativity

  3. Precedence and Associativity? • Operators have rules that are used to determine how expressions are evaluated. • Precedence and associativity deal with the evaluation order within expressions. • Precedencerules specify the order in which operators of different precedence level are evaluated. • Associativityrules decides the order in which multiple occurrences of the same level operator are applied.

  4. Rules of Evaluation of Expression • First, parenthesized sub expression from left to right are evaluated. • If parentheses are nested, the evaluation begins with the innermost sub expression. • The precedence rule is applied in determining the order of application of operators in evaluating sub-expressions. • The associativity rule is applied when two or more operators of the same precedence level appear in a sub expression.

  5. Examples: Evaluate the following expressions 1) z = 2*3/4+4/4+8-2+5/7 z = ?? 2) y = 4/5*6+2/7+4 y = ?? 3) Let inta,b; float c,d; a=5;b=7; c=4.0;d=3.0; Then compute x = a/b*(c+d)/a-b+c/d

  6. Example What is the output of the following piece of code ?? #include <stdio.h> int main (void) { int a, b=21, c=7; a = ++c – b-- % 5; printf (“Value of a = %d, b = %d, c = %d”,a, b, c); } Priority can be overruled by parenthesis. With this expression inside parenthesis are evaluated first so (1+2)*3 gives 9

  7. Thank You

More Related