Subject Name                      Logical Thinking & Problem Solving
Subject Code                      25CSH-107
Semester                          1
LESSON-9 INTRODUCTION EXPRESSIONS
LESSON OBJECTIVES
   • To learn basic symbols of flowcharts
   • To learn how to create flowcharts on different platforms
STRUCTURE OF THE LESSON
   •   Introduction
   •   Expression evaluation
   •   Precedence and Associativity
   •   Summary
   •   FAQs
   •   References
INTRODUCTION
An expression is a combination of operators (such as addition '+', subtraction '-', multiplication
'*', division '/') and operands (variables or literal values), that can be evaluated to yield a single
value of a certain type.
                                 Figure 1 Expressions in C (Image)
EXPRESSION EVALUATION:
Evaluate an expression represented by a String. Expression can contain parentheses, you can
assume parentheses are well-matched. In the C programming language, an expression is evaluated
based on the operator precedence and associativity. When there are multiple operators in an
expression, they are evaluated according to their precedence and associativity. The operator with
higher precedence is evaluated first and the operator with the least precedence is evaluated last.
For example, the expression, 10+15 reduces to the value of 25.
In the C programming language, an expression is evaluated based on the operator precedence and
associativity. When there are multiple operators in an expression, they are evaluated according to
their precedence and associativity. The operator with higher precedence is evaluated first and the
operator with the least precedence is evaluated last.
An expression is evaluated based on the precedence and associativity of the operators in that
expression.
PRIORITY (PRECEDENCE)
This represents the evaluation of expression starts from "what" operator. Precedence determines
which operator is performed first in an expression with more than one operators with different
precedence.
                                       Figure 2 Precedence
For example: Solve
10 + 20 * 30
10 + 20 * 30 is calculated as 10 + (20 * 30) and not as (10 + 20) * 30
Associativity
It represents which operator should be evaluated first if an expression is containing more than one
operator with same priority. Associativity can be either Left to Right or Right to Left.
                                       Figure 3 Associativity
For example: ‘*’ and ‘/’ have same precedence and their associativity is Left to Right, so the
expression “100 / 10 * 10” is treated as “(100 / 10) * 10”.
Operators Precedence and Associativity are two characteristics of operators that determine
the evaluation order of sub-expressions in absence of brackets.
Example 1: Assume x = 2.0 and y= 6.0 . Evaluate the statement
                                       float z = x+3∗x/(y−4);
1. Evaluate expression in parentheses
float z = x+3∗x/(y−4);
float z = x+3∗x/2.0;
2. Evaluate multiplies and divides, from left-to-right
float z = x+3∗x/2.0;
float z = x+6.0/2.0;
float z = x+3.0;
3.Evaluate addition
float z = x+3.0;
float z = 5.0;
4. Perform initialization with assignment
Now, z= 5.0
How do I insert parentheses to get
z= 4.0?
Answer: float z = (x+3∗x)/(y−4);
Example 2: Solve 10 – 3 % 8 + 6 / 4
Example 3: Solve 100 + 200 / 10 - 3 * 10
Answer: Step 1 (solve division): 100+20-3*10
Step 2 (solve multiplication): 100+20-30
Step 3 (solve addition): 120-30
Step 4 (solve subtraction): 90
Example 4:
 #include <stdio.h> // Header file
      int main()    // main function
      int a = 2 + 4 + 3 * 5 / 3 - 5; // Expression evaluation and assignment
      printf("Value of a = %d\n", a);       // printing the value of a
      return 0;           // return statement
Output:
Explanation:
int a = 2 + 4 + 3 * 5 / 3 - 5;
Step 1 (solve multiplication): 2 + 4 + 15 / 3 - 5;
Step 2 (solve Division): 2 + 4 + 5 - 5;
Step 3 (solve addition): 6+5-5
Step 4 (solve addition): 11-5
Step 5 (solve subtraction): 6
Step 6 (Assignment) a=6
TYPES OF EXPRESSION EVALUATION IN C
Based on the operators and operators used in the expression, they are divided into several types.
Types of Expression Evaluation in C are:
    • Integer expressions – expressions which contains integers and operators
    • Real expressions – expressions which contains floating point values and operators
    • Arithmetic expressions – expressions which contain operands and arithmetic operators
    • Mixed mode arithmetic expressions – expressions which contain both integer and real
      operands
    • Relational expressions – expressions which contain relational operators and operands
    • Logical expressions – expressions which contain logical operators and operands
    • Assignment expressions – expressions which contain assignment operators and operands
An expression may also use combinations of the above expressions. Such expressions are known as
compound expressions.
Summary
   •    An expression is a sequence of operands and operators that reduces to a single value. An
        expression can be variable or constant or combination of them.
   •    Operator: An operator is a symbol or sign that specifies the certain operation to be
        carried out with given data or operand. For example c= a + b, where '=' and '+' are
        operators. ...For example, the expression, 10+5 reduces to the value of 15.
   •    In the C programming language, an expression is evaluated based on the operator
        precedence and associativity. When there are multiple operators in an expression, they are
        evaluated according to their precedence and associativity.
   •    There are different levels of operator precedence and an operator may belong to one of these
        levels. The operators at the higher level of precedence are evaluated first. The operators in
        the same level of precedence are evaluated from left to right or from right to left, based on
        the associativity property of an operator.
FAQs:
1) What is the difference between precedence and associativity?
 Operator precedence determines which operator is performed first in an expression with more
than one operators with different precedence. Operators Associativity is used when two operators
of same precedence appear in an expression. Associativity can be either Left to Right or Right to
Left.
2) All operators with the same precedence have same associativity?
Yes, this is necessary, otherwise, there won’t be any way for the compiler to decide evaluation order
of expressions which have two operators of same precedence and different associativity. For
example + and – have the same associativity.
3) Precedence and associativity of postfix ++ and prefix ++ are different?
 Precedence of postfix ++ is more than prefix ++, their associativity is also different. Associativity
of postfix ++ is left to right and associativity of prefix ++ is right to left.
4) Which operator has lowest priority?
 , (comma) operator has lowest priority. Operator precedence determines the grouping of terms
in an expression and decides how an expression is evaluated.
Discussion Forum:
1. Why c is created?
    What are your views on the need of c?
Assessment Questions
1. What will be the output of the following C code?
    #include <stdio.h>
         int main()
                double b = 5 % 3 & 4 + 5 * 6;
                printf("%lf", b);
                return 0;
A. 2
B. 2.000000
C. 30
D. Run time error
2. () (Round Brackets) has the [Blank 1] precedence.
Answer:Highest
3. , (Comma operator) has the [Blank 1] precedence.
Answer: Lowest
4. There are two types of associativity : [Blank 1] and [Blank 2]
Answer: Left to Right, Right to Left
5. The ++ operator increments the operand by 1, whereas, the -- operator decrements it by 1.
A. True
B. False
6.       #include <stdio.h>
         int main()
     {
         double b = 5 & 3 && 4 || 5 | 6;
         printf("%lf", b);
         return 0;
     }
A) 1.000000
B) 0.000000
C) 7.000000
D) 2.000000
7. S++ or S = S+1, which can be recommended to increment the value by 1 and why?
8. What is reminder for 5.0 % 2?
9. Explain the use of comma operator (,).
10. What is the difference between the = symbol and == symbol?
11. Which of the following operators is incorrect and why?
( >=, <=, <>, ==)
References
                   1. Programming in ANSI C by E. Balaguruswamy, Tata McGraw Hill.
                   2. Programming in C Ansi standard, by Yashwant Kanetkar, BPB
                      Publications.
      Books        3. Programming with C (Schaum's Outline Series) by Byron Gottfried
                      Jitender Chhabra, Tata McGraw Hill.
                   4. C Programming Language by Brian W. Kernighan, Dennis Ritchie,
                      Pearson education.
                   1. https://ocw.mit.edu/courses/electrical-engineering-and-computer-
                      science/6-087-practical-programming-in-c-january-iap-2010/lecture-
                      notes/MIT6_087IAP10_lec01.pdf
                   2. https://www.tutorialcup.com/cprogramming/operator-precedence-
                      associativity.htm
   Website Links   3. https://www.geeksforgeeks.org/operator-precedence-and-
                      associativity-in-c/
                   4. https://www.sitesbay.com/cprogramming/c-expression-evaluation
                   5. https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.
                      html#zz-3.5
                   1. https://www.youtube.com/watch?v=qSGZzPZDs_M
    Video links    2. https://www.youtube.com/watch?v=I86J1UjEWtM
                   3. https://www.youtube.com/watch?v=IagOJjF641A