0% found this document useful (0 votes)
7 views40 pages

Lec 06

Uploaded by

sabajitboro0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views40 pages

Lec 06

Uploaded by

sabajitboro0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Introduction to Computing

(CS 1109/1110)
http://jatinga.iitg.ac.in/~asahu/cs1109/

C Statement and Expression


Evaluation

A. Sahu

Dept of Comp. Sc. & Engg.


Indian Institute of Technology Guwahati 1
Outline
• C Statements
• Expression Evaluation
• Bedmas Rule
• Relational Expression and Evaluation

2
C Statements

• Statements are terminated with a


semicolon and that is ';'
• e.g:
char acharacter;
int i, j = 18, k = -20;
printf("Initially, given
j = 18 and k = -20\n");
C Programming : Sum of A and B
#include <stdio.h>

int main(){
int A,B, S; Statement 1
printf(“Enter two
numbers ”); Statement 2
scanf(“%d %d”,&A,&B);
Statement 3
S=A+B;
Statement 4
printf(“Res=%d”, S);
Statement 5
return 0;
} Statement 6 4
C: Block of Statements
Group of statements (compound
statement) are enclosed by curly braces: {
and }.
Mark the start and the end of code block.
C Programming : Sum of A and B
#include <stdio.h>
Start of the BLOCK
int main(){
int A,B, S; Statement 1
printf(“Enter two
numbers ”); Statement 2
scanf(“%d %d”,&A,&B);
Statement 3
S=A+B;
Statement 4
printf(“Res=%d”, S);
Statement 5
return 0;
} Statement 6
End of the BLOCK 6
Comments in C
Single line of comment: // comment here

More than single line of comment or


expanded: /* comment(s) here */
#include <stdio.h> // for printf()
/* main() function, where program
execution starts */
int main(){
/* declares variable and
initializes it*/
int i = 8;
printf(“value of i=%d\n”,i);
return 0;
}
Declaring Variables
• Before using a variable, you must give the
compiler some information about the
variable; i.e., you must declare it.
• The declaration statement includes the data
type of the variable.
• Examples of variable declarations:
int length ;
float area ;
Declaring Variables
• When we declare a variable
– Space is set aside in memory to hold a value of the
specified data type
– That space is associated with the variable name
– That space is associated with a unique address
• Visualization of the declaration
int length ; length

Garbage value

FE07
Using Variables: Initialization
• Variables may be be given initial values, or
initialized, when declared. Examples:
length
int length=7; 7
diameter
float diameter=5.9; 5.9
initial
char initial =‘A’; ‘A’
Using Variables: Initialization
• Do not “hide” the initialization
– Put initialized variables on a separate line
– A comment is always a good idea
– Example:
int height; /* rectangle height */
int width=6; /* rectangle width */
int area; /* rectangle area */
NOT int height, width = 6, area ;
Using Variables: Assignment
• Variables may have values assigned to them through the use
of an assignment statement.
– Uses the assignment operator =
• This operator (=) does not denote equality.
• It assigns the value of the righthand side of the statement (the
expression) to the variable on the lefthand side.
• Only single variables may appear on the lefthand side of the
assignment operator.
• Examples:
diameter = 5.9 ;
area = length * width ;
Using Variables: Assignment
• variable= <const|Expression>
• <Expresion> can be simple or complex expression

area = length * width ;


Arithmetic Operators in C
Name Operator Example

Addition + num1 + num2


Subtraction - initial - spent
Multiplication * fathoms * 6
Division / sum / count
Modulus % m%n
Division
• Integer division
– If both operands of a division expression are
integers,
– you will get an integer answer.
• The fractional portion is thrown away.
• Examples : 17 / 5 = 3
4 / 3 = 1
35 / 9 = 3
Division : float
• Division where at least one operand is a floating
point number will produce a floating point
answer.
• Examples: 17.0 / 5 = 3.4
4 / 3.2 = 1.25
35.2 / 9.1 = 3.86813
• What happens? The integer operand is
temporarily converted to a floating point, then
the division is performed. == > FP domain
Division By Zero
• Division by zero is mathematically undefined.
• If you allow division by zero in a program, it
will cause a fatal error.
• Your program will terminate execution and
give an error message.
• Non-fatal errors do not cause program
termination, just produce incorrect results.
Modulus
• The expression m % n yields the integer
remainder after m is divided by n.
• Modulus is an integer operation -- both
operands MUST be integers.
• Examples : 17 % 5 = 2
6%3 = 0
9%2 = 1
5%8 = 5
Uses for Modulus
• Used to determine if an integer value is even
or odd
5 % 2 = 1 odd 4 % 2 = 0 even
If you take the modulus by 2 of an integer, a
result of 1 means the number is odd and a
result of 0 means the number is even
Expression Evaluation

20
Algebra: BEDMAS/PEDMAS Rule
• B-E-DM-AS or P-E-DM-AS or B-O-DM-AS
• B/P : Bracket or Parenthesis ( )
– In C, only ( ) used for expression
– Curly braces {}, and square bracket [] used for
some other purpose.
– Again [] may involves in expression as in the form
of array access
• E : Exponentiation or Order (O)
• DM: Division and Multiplication
• AS : Addition and Subtraction
21
BEDMAS Example
• Evaluate 8+3*4/2
– DM have higher priority as compared to AS
– All DM get evaluated left to right
8+3*4/2 = 8+ 12/2 = 8+6 = 14
• Evaluate 15-(6+1)+30/(3*2)
15-(6+1)+30/(3*2)= 15-(7)+30/(6)
15-7+5=8+5=13
• Evaluate (95/19)2+3
– (95/19)2+3 = (5)2+3 = 25+3 =28
22
BEDMAS equivalent in C
Arithmetic Operators Precedence Rule
Operator(s) Precedence & Associativity
() Evaluated first. If nested
(embedded), innermost first.
* / % Evaluated second. If there are
several, evaluated left to right.
+ - Evaluated third. If there are
several, evaluated left to right.
= Evaluated last, right to left.
Using Parentheses
• Use parentheses to change the order in which
an expression is evaluated.
a + b * c Would multiply b * c first,
then add a to the result.
If you really want the sum of a and b to be
multiplied by c, use parentheses to force the
evaluation to be done in the order you want.
(a + b) * c
• Also use parentheses to clarify a complex
expression.
Practice With Evaluating Expressions
Given integer variables a, b, c, d, and e,
where a = 1, b = 2, c = 3, d = 4,
evaluate the following expressions:
a + b - c + d
a * b / c
1 + a * b % c
a + d % b - c
e = b = d + c / b - a
Practice With Evaluating Expressions
Given integer variables a, b, c, d, and e,
where a = 1, b = 2, c = 3, d = 4,
evaluate the following expressions:
a + b - c + d =3-3+4=0+4=4
a * b / c = 2/3+4=0+4=4
1 + a * b % c =1+2%3=1+2=3
a + d % b - c =1+0-3=1-3=-2
e = b = d + c / b – a
Increment and Decrement Operators
• The increment operator ++
• The decrement operator --
• Precedence: lower than (), but higher than *
/ and %
• Associativity: right to left
• Increment and decrement operators can only
be applied to variables, not to constants or
expressions
Increment Operator
• If we want to add one to a variable, we can say:
count = count + 1 ;
• Programs often contain statements that
increment variables, so to save on typing, C
provides these shortcuts:
count++ ; OR ++count ;
Both do the same thing. They change the value
of count by adding one to it.
Postincrement Operator
• The position of the ++ determines when the value is
incremented. If the ++ is after the variable, then the
incrementing is done last (a postincrement).
int amount, count ;
count = 3 ;
amount = 2 * count++ ;
• amount gets the value of 2 * 3, which is 6, and then 1
gets added to count.
• So, after executing the last line, amount is 6 and count
is 4.
Preincrement Operator
• If the ++ is before the variable, then the incrementing
is done first (a preincrement).
int amount, count ;
count = 3 ;
amount = 2 * ++count ;

• 1 gets added to count first, then amount gets the value


of 2 * 4, which is 8.
• So, after executing the last line, amount is 8 and count
is 4.
A Hand Trace Example
int ans, val= 4 ;
Code Val Ans
4 garbage
val = val + 1 ;
val++ ;
++val ;
ans = 2 * val++ ;
ans = ++val / 2 ;
val-- ;
--val ;
ans = --val * 2 ;
ans = val-- / 3 ;
A Hand Trace Example
int ans, val= 4 ;
Code Val Ans
4 garbage
val = val + 1 ; 5
val++ ; 6
++val ; 7
ans = 2 * val++ ; 8 14
ans = ++val / 2 ; 9 4
val-- ; 8
--val ; 7
ans = --val * 2 ; 6 12
ans = val-- / 3 ; 5 2
C Code : Previous Example
int main(){
int ans, val=4;
val = val + 1 ;
printf(“ans=%d val=%d\n”,ans,val);
val++ ; ++val ;
printf(“ans=%d val=%d\n”,ans,val);
ans = 2 * val++ ;
printf(“ans=%d val=%d\n”,ans,val);
ans=++val/2; val--;--val;
printf(“ans=%d val=%d\n”,ans,val);
ans=--val*2;
printf(“ans=%d val=%d\n”,ans,val);
ans = val-- / 3 ;
printf(“ans=%d val=%d\n”,ans,val);
return 0;
}
Practice
Given
int a = 1, b = 2, c = 3 ;

What is the value of this expression?

++a * b - c--

What are the new values of a, b, and c?


Practice
Given
int a = 1, b = 2, c = 3 ;

What is the value of this expression?

++a * b - c--
=1
What are the new values of a, b, and c?
a =2 c=2
More Practice
Given
int a =1, b =2, c =3, d =4 ;

What is the value of this expression?

++b / c + a * d++

What are the new values of a, b, c, and d?


More Practice
Given
int a =1, b =2, c =3, d =4 ;

What is the value of this expression?

++b / c + a * d++
= 1+4 =5
What are the new values of a, b, c, and d?
a=1, b=3,c=3,d=5
Assignment Operators
= += -= *= /= %=
Statement Equivalent Statement
a = a+2 ; a += 2 ;
a = a-3 ; a -= 3 ;
a = a*2 ; a *= 2 ;
a = a/4 ; a /= 4 ;
a = a%2 ; a %= 2 ;
b = b+(c+2); b += c + 2 ;
d =d*(e-5); d *= e - 5 ;
Practice with Assignment Operators
int i = 1, j = 2, k = 3, m = 4 ;

Expression Value
i += j + k

j *= k = m + 5

k -= m /= j * 2
Practice with Assignment Operators
int i = 1, j = 2, k = 3, m = 4 ;

Expression Value
i += j + k i=6

j *= k = m + 5 k=9, j=18

k -= m /= j * 2 m=1, k=2

You might also like