Introduction To Computing (CS 101) : Statements, Operators and Expressions
Introduction To Computing (CS 101) : Statements, Operators and Expressions
Integral Fractional
printf(“Area=%d”,Area); STOP
return 0;
}
C Example 2: Area of Circle
#include <stdio.h>
#define PI 3.142 START
int main(){
Input W, L
float r, Area;
printf(“Enter radius”);
scanf(“%f”,&r); Area PI x r x
r
Area=PI*r*r; Print Area
printf(“Area=%f”,Area);
return 0; STOP
}
The literal PI value get replaced by 3.142
C Example 3: Temp Conversion
#include <stdio.h>
START
int main(){
Input Tc
float Tc,Tf;
printf(“Enter Tc”);
scanf(“%f”,&Tc); Tf 1.8 x Tc + 32
Tf=(1.8*Tc)+32; Print Tf
printf(“Tf=%f”,Tf);
return 0; STOP
}
C Example 4: Force Between Two bodies
#include <stdio.h>
int main(){
float F, m1,m2,r;
START
float G=6.673e-11;
printf(“Enter m1 m2”); Input m1, m2, r
scanf(“%f %f”,&m1,&m2);
printf(“Enter r”);
F G*m1*m1 /r2
scanf(“%f”,&r);
F=(G*m1*m2)/(r*r); Print F
printf(“Tf=%f”,F);
STOP
return 0;
}
Expression Evaluation
• B-E-DM-AS Rule
• B : Bracket or Parenthesis ( )
– Only ( ) used for expression
– Curly braces {}, and square bracket [] used for some
other purpose.
• E : Exponentiation or Order (O)
• DM: Division and Multiplication
• AS : Addition and Subtraction
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.
Expression Evaluation
• 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
Using Parentheses
a+b*c
• Would multiply b * c first, then add a to the result.
• Use parentheses to change the order in which an
expression is evaluated.
• 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
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 + d
1 + a * b % c
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 + d = 2/3+4=0+4=4
1 + a * b % c =1+2%3=1+2=3
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
• If we want to add one to a variable, we can say:
count = count + 1 ;
count++ ; OR ++count ;
• Both do the same thing. They change the value of count by
adding one to it.
Post-Increment Operator
• The position of the ++ determines when the value is
incremented. If the ++ is after the variable, then the
incrementing is done last (a post-increment).