Chapter 2 - Programming Elements
Chapter 2 - Programming Elements
Chapter 2
Arithmetic expressions and precedence; Conditional Branching and
Loops; Writing and evaluation of conditionals and consequent
branching; Iteration and loops.
Required Installations
1. Visual Studio Code (https://code.visualstudio.com/)
2. MinGW (http://mingw.org/)
3. Online C Program Compiler
(https://www.tutorialspoint.com/compile_c_online.php)
4. Android Apps
(https://play.google.com/store/apps/details?id=name.antonsmirno
v.android.cppdroid&hl=en_IN&gl=US)
Operators Precedence & Associativity
Associativity
Order Category Operators
Order
1 Parenthesis/Postfix (), [], ->, ., ++, – – Left to Right
2 Unary +, –, !, ~, ++, – –, (type)*, &, sizeof Right to Left
3 Multiplicative *, /, % Left to Right
4 Additive +, – Left to Right
5 Shift <<, >> Left to Right
6 Relational <, <=, >, >= Left to Right
7 Equality ==, != Left to Right
Operators Precedence & Associativity
Associativity
Order Category Operators
Order
8 Bitwise AND & Left to Right
9 Bitwise XOR ^ Left to Right
10 Bitwise OR | Left to Right
11 Logical AND && Left to Right
12 Logical OR || Left to Right
13 Conditional ?: Right to Left
14 Assignment =, +=, -=, *=, /=, %, =>, >=, <<=, &=, ^=, |= Right to Left
15 Comma , Left to Right
Operator Precedence
1. Operator Precedence governs which operator will be evaluated first
in an expression
2. Example:
1. Ans = 3 + 2 * 5
2. Possibility 1: Ans = (3 + 2) * 5 = 25
3. Possibility 2: Ans = 3 + (2 * 5) = 13
3. Which one resultant is correct?
4. Operator Precedence says multiplication has higher priority.
5. Therefore, Ans = 13 is the correct answer
Associativity of Operators
• If Operator Precedence is same, then which operator will be evaluated first
in an expression
• Associativity of operator decided which operator will be evaluated first.
Operator Associativity can be
• Left to Right
• Right to Left
• Example:
Ans = 20 / 2 * 5
Possibility 1: Ans = (20 / 2) * 5 = 50 Right to Left
Possibility 2: Ans = 20 / (2 * 5) = 2 Left to Right
• Which one resultant is correct?
• Since operator associativity for Multiplicative Operators is Left to Right
• Therefore, Ans = 2 is the correct answer
Operators Precedence
1. Parenthesis has highest precedence and also used in function calls
2. Consider an Example:
• int myVar = testFun();
3. How complier will know that testFun (→ Operand) is
• a function
• not a variable
4. Expression contains:
• Assignment Operator = and
• Parenthesis Operator ()
5. Assignment Operator has less precedence than Parenthesis Operator
• int myVar = ( testFun() );
• Therefore, testFun belongs to () then assigned to myVar
Examples
• int a = 10; b = 20; c = 30; d = 40;
• if(a <= b == d >= c) // if(true == true)
• printf(“Something”)
• else
• printf(“Elsething”)
• Ans = Something
b, B, KB, MB, GB, TB
• b – Bit 1
• B – Byte 8
• KB – KiloBytes 1024 Bytes 103
• MB – MegaBytes 1024 KiloBytes 106
• GB – GigaBytes 1024 MegaBytes 109
• TB – TeraBytes 1024 GigaByte 1012