You are on page 1of 24

IntroductiontoProgramming

(inC++)
Dataandstatements

Jordi Cortadella,Ricard Gavald,FernandoOrejas


Dept.LSI,UPC

Outline
Variables,datatypesandexpressions
Statements:
Assignment
Input/output
Conditionalstatement

IntroductiontoProgramming

Dept.LSI,UPC

Variablesandliterals
Variable: symbolicnametorepresentdatavalues.
Avariableisusuallyassociatedwithamemory
location.
Intuition:thinkofavariableasaboxcontainingvalues
ofacertaintype.

int x

bool z

true

InC++(andmanyotherlanguages),variablesmustbe
declaredbeforetheyareused.
Literal: aconstantofacertaintype.
Examples:4,3.14159,4.1e8,true,"Greenland"
IntroductiontoProgramming

Dept.LSI,UPC

Types
Adatatypespecifies:
Thesetofvaluesthatdataofthattypecanhave
Thetypeofoperationsthatcanbeperformed
withthedata.

Everyprogramminglanguagehasasetof
basicdatatypes.
BasicdatatypesinC++:int,double,bool,char,
string,
IntroductiontoProgramming

Dept.LSI,UPC

Expressions
Expression:acombinationofliterals,
variables,operatorsandfunctionsthatis
evaluatedandreturnsavalue
Examples
a+3(i 1)
sqrt(x)log(4n)
(i 3)<=x
(a!=b)and(s<="abc")

IntroductiontoProgramming

Dept.LSI,UPC

STATEMENTS

IntroductiontoProgramming

Dept.LSI,UPC

Statements
Anyprogramminglanguagehasasetofbasic
statementstomanipulatedata(read,write
andtransform).
Aprogramconsistsofacombinationofdata
andstatementstoperformsometask.
Aprogramcanbecomeanewstatement
(function)thatcanbeusedinotherprograms.
IntroductiontoProgramming

Dept.LSI,UPC

Assignment
Assignment isthefundamentalstatementof
imperative languages:
variable =expression
Semantics:
Thevalueoftheexpressionisevaluated
Theresultisstoredinthevariable
Thepreviousvalueofthevariableislost

IntroductiontoProgramming

Dept.LSI,UPC

Assignment
Examples
int x,i,j;
...

Variableinitialization
Variablescanbeinitialized
withanexpressionintheir
declaration:

//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
IntroductiontoProgramming

double pi=3.14159;
double two_pi =2pi;
string my_name ="Jordi";
Recommendation:declarethe
variableswhenneeded(not
before).Initializethevariable
inthesamedeclaration
wheneverpossible.
Dept.LSI,UPC

Sequenceofstatements
Asequenceofstatements(notnecessarily
assignments)isexecutedsequentially:
statement_1;
statement_2;
...
statement_n;

IntroductiontoProgramming

Dept.LSI,UPC

10

Example:swappingthevalueoftwovariables
Solution1

Solution2

int x,y;

int x,y;

//Precondition:x=X,y=Y
x=y;
y=x;
//Postcondition:x=Y,y=X

//Precondition:x=X,y=Y
int z=x;
x=y;
y=z;
//Postcondition:x=Y,y=X

Whyisthissolution
incorrect?

Atemporaryvariableis
required

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 representtheprogramsdefault
input andoutput devicesrespectively(usually,
thekeyboardandthedisplay).
Simpleoperations:
//Readandstoreinavariable
cin >>variable;
//Writethevalueofanexpression
cout <<expression;
IntroductiontoProgramming

Dept.LSI,UPC

13

ExamplesofI/OinC++
#include<iostream>
usingnamespacestd;
...
int x,y;
double z;
...
cin >>x>>y>>z;
cout <<xy <<z+1<<endl;
...
>in_out
342.75
123.75
IntroductiontoProgramming

Dept.LSI,UPC

14

ExamplesofI/OinC++
#include<iostream>
usingnamespacestd;
...
int x,y;
Someaestheticformatting
isusuallyrequired
double z;
...
cin >>x>>y>>z;
cout <<xy <<","<<z+1<<endl;
...
>in_out
342.75
12,3.75
IntroductiontoProgramming

Dept.LSI,UPC

15

Divisionandremainder
//Input:readstwointegernumbers(a,b)
//Output:writesthedivisionandremainder
//ofa/b
int main(){
int a,b;
cin >>a>>b;
cout <<"Division:"<<a/b
<<",Remainder:"<<a%b<<endl;
}

IntroductiontoProgramming

Dept.LSI,UPC

16

Revisitingtimedecomposition
//Input:readsanintegerN>=0thatrepresents
//acertaintimeinseconds
//Output:writesthedecompositionofNin
//hours(h),minutes(m)andseconds(s)
//suchthat0<=m<60and0<=s<60.
int main(){
int N;
cin >>N;
int s=N%60;
N=N/60;
cout <<N/60<<""<<N%60<<""<<s<<endl;
}

IntroductiontoProgramming

Dept.LSI,UPC

17

Conditionalstatement
if (condition)statement1;
else statement2;
condition isaBooleanexpression
Semantics:iftheconditionevaluatestrue,
statement1 isexecuted,otherwise
statement2 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<0)a=a;
cout <<a<<endl;
}

IntroductiontoProgramming

Dept.LSI,UPC

20

Minandmaxoftwonumbers
int 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

Blocksofstatements
areenclosedinside{}

Dept.LSI,UPC

21

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

You might also like