IntroductiontoProgramming(inC++)
Dataandstatements
Jordi Cortadella,Ricard Gavald,FernandoOrejasDept.LSI,UPC
Outline
Variables,datatypesandexpressions
Statements: Assignment Input/output Conditionalstatement
2Dept.LSI,UPCIntroductiontoProgramming
Variablesandliterals Variable: symbolicnametorepresentdatavalues. Avariableisusuallyassociatedwithamemorylocation.
Intuition:thinkofavariableasaboxcontainingvaluesofacertaintype.
InC++(andmanyotherlanguages),variablesmustbedeclaredbeforetheyareused.
Literal: aconstantofacertaintype. Examples:4,3.14159,4.1e8,true,"Greenland"
IntroductiontoProgramming Dept.LSI,UPC 3
3 trueint x bool z
Types Adatatypespecifies:
Thesetofvaluesthatdataofthattypecanhave Thetypeofoperationsthatcanbeperformedwiththedata.
Everyprogramminglanguagehasasetofbasicdatatypes.
BasicdatatypesinC++:int,double,bool,char,string,
IntroductiontoProgramming Dept.LSI,UPC 4
Expressions Expression:acombinationofliterals,variables,operatorsandfunctionsthatisevaluatedandreturnsavalue
Examplesa+3(i 1)sqrt(x)log(4n)(i 3)
STATEMENTS
IntroductiontoProgramming Dept.LSI,UPC 6
Statements Anyprogramminglanguagehasasetofbasicstatementstomanipulatedata(read,writeandtransform).
Aprogramconsistsofacombinationofdataandstatementstoperformsometask.
Aprogramcanbecomeanewstatement(function)thatcanbeusedinotherprograms.
IntroductiontoProgramming Dept.LSI,UPC 7
Assignment Assignment isthefundamentalstatementofimperative languages:
variable =expression
Semantics: Thevalueoftheexpressionisevaluated Theresultisstoredinthevariable Thepreviousvalueofthevariableislost
IntroductiontoProgramming Dept.LSI,UPC 8
AssignmentExamples
int x,i,j;...
//x=3,i=8,j=2
x=3i+j;//x=22,i=8,j=2
x=x i;
//x=14,i=8,j=2
j=0;
//x=14,i=8,j=0
Variableinitialization Variablescanbeinitialized
withanexpressionintheirdeclaration:
double pi=3.14159;double two_pi =2pi;stringmy_name ="Jordi";
Recommendation:declarethevariableswhenneeded(notbefore).Initializethevariableinthesamedeclarationwheneverpossible.
IntroductiontoProgramming Dept.LSI,UPC 9
Sequenceofstatements Asequenceofstatements(notnecessarilyassignments)isexecutedsequentially:
statement_1;statement_2;...statement_n;
IntroductiontoProgramming Dept.LSI,UPC 10
Example:swappingthevalueoftwovariables
Solution1
int x,y;
//Precondition:x=X,y=Yx=y;y=x;//Postcondition:x=Y,y=X
Whyisthissolutionincorrect?
Solution2
int x,y;
//Precondition:x=X,y=Yint z=x;x=y;y=z;//Postcondition:x=Y,y=X
Atemporaryvariableisrequired
IntroductiontoProgramming Dept.LSI,UPC 11
?
Swappingtwointegerswithonlytwovariables
//Pre:x=A,y=B
x=x y;
//x=AB,y=B
y=x+y;
//x=AB,y=A
x=y x
//Post:x=B,y=A
IntroductiontoProgramming Dept.LSI,UPC 12
BasicI/OinC++ cin andcout representtheprogramsdefaultinput andoutput devicesrespectively(usually,thekeyboardandthedisplay).
Simpleoperations:
//Readandstoreinavariablecin >>variable;//Writethevalueofanexpressioncout
Divisionandremainder
//Input:readstwointegernumbers(a,b)//Output:writesthedivisionandremainder//ofa/b
int main(){int a,b;cin >>a>>b;cout
Revisitingtimedecomposition
//Input:readsanintegerN>=0thatrepresents//acertaintimeinseconds//Output:writesthedecompositionofNin//hours(h),minutes(m)andseconds(s)//suchthat0
Conditionalstatement
if (condition)statement1;else statement2;
condition isaBooleanexpression Semantics:iftheconditionevaluatestrue,
statement1 isexecuted,otherwisestatement2 isexecuted.
IntroductiontoProgramming Dept.LSI,UPC 18
Conditionalstatement:example
int a,b,m;...//Calculationofthemaximumoftwonumbers//Pre:a=A,b=B
if (a>=b)m=a;else m=b;
//Post:a=A,b=B,m=max(A,B)
IntroductiontoProgramming Dept.LSI,UPC 19
Theelse partisoptional
//Input:readsanintegernumber//Output:writestheabsolutevalue//ofthenumber
intmain(){int a;cin >>a;if (a
Minandmaxoftwonumbersint a,b,minimum,maximum;
//Pre:a=A,b=B//Post:a=A,b=B,//minimum=min(A,B),maximum=max(A,B)
if (a>=b){minimum=b;maximum=a;
}else {
minimum=a;maximum=b;
}
IntroductiontoProgramming Dept.LSI,UPC 21
Blocksofstatementsareenclosedinside{}
Maxofthreenumbers(I)int a,b,c,m;
//Pre:a=A,b=B,c=C//Post:a=A,b=B,c=C,m=max(A,B,C)
if (a>=b){if (a>=c)m=a;else m=c;
}else {
if (b>=c)m=b;else m=c;
}
IntroductiontoProgramming Dept.LSI,UPC 22
Maxofthreenumbers(II)
int a,b,c,m;
//Pre:a=A,b=B,c=C//Post:a=A,b=B,c=C,m=max(A,B,C)
if (a>=band a>=c)m=a;elseif(b>=aand b>=c)m=b;else m=c;
IntroductiontoProgramming Dept.LSI,UPC 23
Maxofthreenumbers(III)
int a,b,c,m;
//Pre:a=A,b=B,c=C//Post:a=A,b=B,c=C,m=max(A,B,C)
if (a>=b)m=a;else m=b;//m=max(a,b)if (c>m)m=c;
IntroductiontoProgramming Dept.LSI,UPC 24