Академический Документы
Профессиональный Документы
Культура Документы
Expressions
PROGRAMSUSINGARITHMETIC
EXPRESSIONS
/*
#include <stdio.h>
int main(void) {
int x, y, sum;
x = 20;
y = 30;
sum = x+y;
printf("The result is %d \n, sum );
return 0;
}
Assignment Statements in C
x = 20;
x is assigned the value 20
y = 30;
y is assigned the value 30
Assignment Statements in C
x = 20;
x is assigned the value 20
y = 30;
y is assigned the value 30
sum = x+y;
sum is assigned the value of x+y
x = x + 5;
x is assigned the value of x+5
(25 in this case)
/*
#include <stdio.h>
int main(void) {
int x, y, sum; // declare type of variables
x = 20;
y = 30;
sum = x+y;
printf("The result is %d\n", sum);
return 0;
}
The result is 50
/*
My 4th Program */
#include <stdio.h>
int main(void) {
int x, y, sum;
x = 20;
y = 30;
sum = x+y;
printf("The sum of %d and %d is %d\n", x,
y, sum);
return 0;
}
The sum of 20 and 30 is 50
/*
My 5th Program getting values from
the user */
#include <stdio.h>
int main(void) {
int x, y, sum;
scanf( %d %d ,&x, &y) ;
sum = x+y;
printf("The sum of %d and %d is %d\n, x,
y, sum);
return 0;
}
Yourfirstlabsession
LearnaboutbasicUNIXcommands
Learnaboutbasiceditorcommands
RunasmallCprogramtoprintyournameand
IITexperience.
Runningaprogram
1)Typeinyourprogram
Ifthereareerrors,Edityourprogram
2)Compileyourprogram
Ifcompilationerrors,gotostep1
3)Executeyourprogram
Ifexecutionerrors,gotostep1
4)Ifyouarenotsatisfiedwithyourresults,
Gotostep1
Toconvertyourprogramintomachineunderstandable
,useacompiler,likegcc.
LettheprogrambesavedinMy.c
>gccMy.c
>
Iftherearenoerrors,thenyoucanrunthecompiled
programusing
>a.out
form
AdifferentwaytoCompileandRun
Iftherearemanyprograms,theneachprogramcanbe
compiledseparatelyintoitsownexecutableform
Lettherebetwoprogramstest1.c&test2.c
Compilingfirstprogram
>gccotest1.outtest1.c
Compilingsecondprogram
>gccotest2.outtest2.c
Now,anyprogramcanberunbytheirexecutables.
Torunthefirstprogram
>test1.out
Declaringvariables
inta;
intyear,month,group_number;
inty,m,day;
inta,b,c,d(Notpreferred)
intNum_feet_in_mile;(toolong)
intf_mile
initializingvariables
Agoodpracticeistoinitializethevariablestozeroif
noothervalueshavebeenspecified,
Otherwise,thecomputerwillassignagarbagevalue
tothisvariable.
intfmile;
fmile=0;
0
declareandinitialize
Thesetwothingscanbedoneinonestep.
int fmile=0;
#include <stdio.h>
int main(void) {
int fmile=0, ymile;
int fyard;
ymile = 1760;
fyard = 3;
fmile = ymile * fyard;
printf( 1 Mile = %d Feet.\n", fmile);
return 0;
}
/* Division operation in C */
#include <stdio.h>
int main(void) {
int b=10,
d=4, m;
float c, r;
m =b/d;
c = b/d;
printf(m= %d, c= %f, \n", m,c);
return 0;
}
m= 2, c= 2.0,
/* Division operation in C */
#include <stdio.h>
int main(void) {
int b=10,
d=4, m;
float c, r;
m =b/d;
c = b/d;
r = b*1.0/ d;
printf(m= %d, c= %f, r= %f \n", m,c,r);
return 0;
}
m= 2, c= 2.0, r=2.5
Accuracy of results
(1 /7.0) * 7.0 = ?
Accuracy of results
(1 /7.0) * 7.0 = ?
Do not be surprised if computer does not
print the result of the expression as 1.
Accuracy of results
(1 /7.0) * 7.0 = ?
Do not be surprised if computer does not
print the result of the expression as 1.
1/7.0 = 0.14285714285714285.
(0.14285714285714285.) * 7.0 = 1.0
Accuracy of results
(1 /7.0) * 7.0 = ?
Do not be surprised if computer does not
print the result of the expression as 1.
1/7.0 = 0.14285714285714285.
(0.14285714285714285.) * 7.0 = 1.0
(0.14286) * 7.0 = 1.00002
Computer has finite precision.
floating-point values are rounded to the
number of significant digits permissible
OperatorPrecedence
Indecreasingorderofpriority
1.
2.
3.
4.
Parentheses::()
Unaryminus::5
Multiplication,Division,andModulus
AdditionandSubtraction
Foroperatorsofthesamepriority,evaluationis
fromlefttoright astheyappear
Parenthesismaybeusedtochangetheprecedence
ofoperatorevaluation
26
Examplesof
Arithmeticexpressions
(b/d)*c
27
assigningonevaluetomultiplevariables
Severalvariablescanbeassignedthesamevalue
usingmultipleassignmentoperators
a=b=c=5;
speed=flow=0.0;
Easytounderstandifyourememberthat
theassignmentexpressionhasavalue
Multipleassignmentoperatorsarerighttoleft
associative
28
TypesofvaluesonLHSandRHSof
arithmeticexpressions
Usuallyshouldbethesame
Ifnot,thetypeoftheRHSvaluewillbeinternally
convertedtothetypeoftheLHSvalue,andthen
assignedtoit
Example:
floata;
a=2*3;
RHStypeisintandthevalueis6
LHStypeisfloat,sostoresitas6.0
29
LOGICALEXPRESSIONS
LogicalExpressions
Usesrelationalandlogicaloperatorsin
additiontoarithmeticones
Informally,specifiesaconditionwhichcanbe
trueorfalse
Evaluatestovalue0or1
0impliestheconditionisfalse
1impliestheconditionistrue
31
LogicalStatements
Ifcountislessthan100thenitisTrue
Ifcountexactlyequals100thenitisTrue
Ifcountisnotequalto100thenitisTrue
Ifmarksare80ormorebutlessthan90thenitis
true
ifbalanceismorethan5000ORtransactionsare
morethan25thenitistrue.
32
RelationalOperators
Usedtocomparetwoquantities.
<
islessthan
>
isgreaterthan
<=
islessthanorequalto
>=
isgreaterthanorequalto
==
isequalto
!=
isnotequalto
33
Examples
10>20
isfalse,sovalueis0
25<35.5
istrue,sovalueis1
12>(7+5)
isfalse,sovalueis0
32!=21
istrue,sovalueis1
Whenarithmeticexpressionsareusedoneitherside
ofarelationaloperator,thearithmeticexpressions
willbeevaluatedfirstandthentheresultscompared
a+b>c disthesameas(a+b)>(c+d)
34
LogicalExpressions
(count<100)
Ifcountislessthan100thenitisTrue
(count==100)
Ifcountexactlyequals100thenitisTrue
(count!=100)
Ifcountisnotequalto100thenitisTrue
(count<=100)
Ifcountisupto100thenitisTrue
35
LogicalOperators
LogicalAND(&&)
Evalutesto1ifboththeoperandsarenonzero
LogicalOR(||)
Resultistrueifatleastoneoftheoperandsisnon
zero
X
X && Y
X || Y
non-0
non-0
non-0
non-0
non-0
non-0
non-0
non-0
36
LogicalExpressions
((math+phys+chem)/3>=60)
ifaverageisequaltoorgreaterthan60thenitis
true
((marks>=80)&&(marks<90))
Ifmarksare80ormorebutlessthan90thenitis
true
((balance>5000)||(no_of_trans>25))
ifbalanceismorethan5000ORtransactionsare
morethan25thenitistrue.
37
negationoperator
Unarynegationoperator(!)
Singleoperand
Valueis0ifoperandisnonzero
Valueis1ifoperandis0
example:
a=80,b=70
(a+b>130)evaluatestoTrueifsuma+bexceeds130
!(a+b>130)evaluatestotrueifa+bislessthanorequal
to130
38
Example
(4>3)&&(100!=200)
4>3istrue,sovalue1
100!=200istruesovalue1
Bothoperandsare1,sofinalvalue1(True)
(!10)&&(10+20!=200)
firstoperand10isnon0,sovalue!10is0
secondoperand30!=200istruesovalue1
SincebothoperandsareNOT1sofinalvalue0
(!10)||(10+20!=200)
Sameasabove,butsinceoneoftheoperandhasvalue
non0,sofinalvalue1
39
Example
(4>3)&&(100!=200)
4>3istrue,sovalue1
100!=200istruesovalue1
Bothoperandsare1,sofinalvalue1(True)
(!10)&&(10+20!=200)
firstoperand10isnon0,sovalue!10is0
secondoperand30!=200istruesovalue1
SincebothoperandsareNOT1sofinalvalue0
(!10)||(10+20!=200)
Sameasabove,butsinceoneoftheoperandhasvalue
non0,sofinalvalue1
40
Example
(4>3)&&(100!=200)
4>3istrue,sovalue1
100!=200istruesovalue1
Bothoperandsare1,sofinalvalue1(True)
(!10)&&(10+20!=200)
firstoperand10isnon0,sovalue!10is0
secondoperand30!=200istruesovalue1
SincebothoperandsareNOT1sofinalvalue0
(!10)||(10+20!=200)
Sameasabove,butsinceoneoftheoperandhasvalue
non0,sofinalvalue1
41
Becarefulwithmixedarithmeticand
logicalassignments!
a=3&&b=4
Noparenthesis,soneedtolookatprecedenceand
associativity
=hashigherprecedencethan&&
b=4isanassignmentexpression,evaluatesto4
a=3isanassignmentexpression,evaluatesto3
Bothoperandsof&&arenon0,sofinalvalueofthelogical
expressionis1
Notethatchangingtob=0wouldhavemadethe
finalvalue0
42
Becarefulwithmixedarithmeticand
logicalassignments!
a=3&&b=4
Noparenthesis,soneedtolookatprecedenceand
associativity
=hashigherprecedencethan&&
b=4isanassignmentexpression,evaluatesto4
a=3isanassignmentexpression,evaluatesto3
Bothoperandsof&&arenon0,sofinalvalueofthelogical
expressionis1
Notethatchangingtob=0wouldhavemadethe
finalvalue0
43