Upload
manuel-sosaeta
View
237
Download
0
Embed Size (px)
Citation preview
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 1/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#1
CS 31:
Introduction toComput!r Sci!nc! I
Topic 1
Introduction to
Programming
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 2/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#2
Introduction to Programming
PR$%RA&&I'%
PR$C(SS()A&P*(
+ri"ing a Car
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 3/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#3
Introduction to Programming
Programming Proc! (,ampl!
T-p! o Contol S!u!ntial: St!p#-#t!p
S!l!ction: $n! o choic! R!p!tition: R!p!at!d t!p
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 4/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#
Program Control lo
S!u!ntial
$n! St!p at a Tim!
4!gin (ndComput! Comput! Comput! Comput!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 5/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#5
Program Control lo
S!l!ction
$n! o Choic!
Comput!
Comput!Condition 4!gin (nd
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 6/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#6
Program Control lo
R!p!tition
R!p!at!d St!p
4!gin (ndConditionInitiali7! Comput! 8pdat!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 7/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#
Program Control lo
Compoition
R!plac! an- S!u!ntial St!p
ith
To or mor! S!u!ntial St!p
a S!l!ction or a R!p!tition
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 8/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#;
Program Control lo
Compoition
(,ampl!
%u! a 'um!r 4!t!!n 0 and 100
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 10/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#10
Introduction to Programming
Programming St!p
1. Sp!ciication: +!cription o th! Ta<
2. Anal-i: 8nd!rtanding th! Ta<
3. T!t +ata: Compl!t! S!t o T!t
. +!ign: +ata and Algorithm
5. Impl!m!nt: =rit! th! Program
6. T!t and +!ug: &a<! It =or<
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 11/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#11
Algorithm +!ign
Tool
lo Chart
P!udocod!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 12/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#12
Algorithm +!ign
lo Chart
%raphical S-mol
(a- to >iuali7! lo
+iicult to +ra in Comm!nt
(,ampl!: lo o Control +iagram
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 13/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#13
Algorithm +!ign
lo Chart (,ampl!
%u! a 'um!r 4!t!!n 0 and 100
Set
G to 50G = N?
N
Y
Done:
G is N
G < N?N
Y
Add
C to G
Set
C to 25
Subtract
C from G
Divide
C by 2
Start
G is te current !uess
C is te current can!e
N is te cosen number
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 15/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#15
Algorithm +!ign
P!udocod! (,ampl!
%u! a 'um!r 4!t!!n 0 and 100
1. S!t th! gu! to 50. 2. S!t th! chang! to 25.
3. I th! gu! i th! num!r +$'(?
. I th! gu! i l! than th! num!radd th! chang! to th! gu!.
5. I th! gu! i gr!at!r than th! num!rutract th! chang! rom th! gu!
6. +i"id! th! chang! - 2.
. %o to St!p 3.
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 16/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#16
Algorithm +!ign
P!udocod! (,ampl! // %!t th! input "alu!
// I th! input "alu! ar! not "alid// +ipla- an !rror m!ag!
// (l!
// or !ach input "alu!
// Calculat! th! r!ult
// +ipla- th! r!ult
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 17/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#1
Programming 4aic
Programming Practic!
@ +!ign !or! impl!m!nting
@ 8! comm!nt li!rall-
@ +!"!lop and t!t incr!m!ntall-
@ R!u! unction
@ Start !arl-
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 18/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#1;
Programming 4aic
*!arning to Program
Program
Program
Program
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 19/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#19
Programming *anguag! *!"!l
&achin! ". uman
&achin! *anguag!
A!ml- *anguag!
igh#*!"!l *anguag!
'atural *anguag!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 20/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#20
Programming *anguag! *!"!l
&achin! *anguag!
@ *anguag! o th! Comput!r
@ Conit o 0B and 1B
@ &achin! +!p!nd!nt
@ *o!t *!"!l@ 'o *imit
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 21/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#21
Programming *anguag! *!"!l
A!ml- *anguag!
@ Simplii!d Comput!r *anguag!
@ Conit o S-mol and $p!ration
@ &achin! +!p!nd!nt
@ S!cond *o!t *!"!l@ 'o *imit: Sam! a &achin! *anguag!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 22/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#22
Programming *anguag! *!"!l
igh#*!"!l *anguag!
@ &ath!matical *anguag!
@ Conit o &ath!matical Stat!m!nt
@ &achin! Ind!p!nd!nt &otl-D
@ igh!t *!"!l (,c!pt 'aturalD@ *imit!d Capailit-: 'ot All $p!ration
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 23/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming©9/2010 John A. Rohr All Right R!!r"!d
JAR 1#23
Programming *anguag! *!"!l
8!
@ &achin! *anguag!: +!ugging $nl-
@ A!ml- *anguag!: Critical Ta<
@ igh#*!"!l *anguag!: &ot Common
@ 'atural *anguag!: 'ot E!t
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 24/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!dJAR 1#2
Programming *anguag! *!"!l
(,ampl!@ igh#*!"!l: Sum F irt G S!condH
@ A!ml-: *$A+ irt A++ S!cond
ST$R( Sum
@ &achin!: 210A F 0010000100001010104 F 0010000100001011
310C F 0011000100001100
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 25/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!dJAR 1#25
Compilation Proc!
igh#*!"!l &achin!
1. (nt!r high#l!"!l program
2. Tranlat! to A!ml- *anguag!
3. Tranlat! to &achin! *anguag!
. Comin! ith *irar- unction
5. *oad into Comput!r
6. Run Program
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 26/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d JAR 1#26
Compilation Proc!
A!ml!r
Compil!r
*in<!r
*oad!r
Sourc!
il!
CGGil!
*irar-il!
$!ct
il!
$/Amil!
(,!cut!il!
&ain&!mor-
Am
$
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 27/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d JAR 1#2
Compilation Proc!
8!r St!p
1. (nt!r high#l!"!l program ith !ditor
2. Compil! th! program
3. i, th! -ntactic !rror
. Run th! program
5. i, th! !mantic !rror
6. Compl!t! th! docum!ntation
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 28/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d JAR 1#2;
igh#*!"!l *anguag!
&an- T-p!
@ $RTRA': irt &ath!matical
@ C$4$*: (arl- (nglih#*i<!
@ *ISPS'$4$*: Sp!cial#Purpo!
@ A+A: +$+ Standard
@ C: $p!rating S-t!m Impl!m!ntation
@ CGG: $!ct#$ri!nt!d Programming
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 29/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d JAR 1#29
igh#*!"!l *anguag!
C/CGG
@ C: In"!nt!d to Program 8'I) $. S.
%!n!ral Purpo!
@ CGG: (nhanc!m!nt o C
$!ct#$ri!nt!d Programming
*anguag! or thi Cour!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 30/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d JAR 1#30
unction and Program
unction
@ A ingl! unit o computation
Accomplih! on! thing
&a- u! oth!r unction
&a- produc! a r!ult
S!u!nc! o tat!m!nt
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 31/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d JAR 1#31
unction and Program
Program
@ Impl!m!ntation o a compl!t! olution
Sol"! a p!cii!d prol!m
Includ! input and output
&a- u! unction
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 32/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d JAR 1#32
CGG 4aic
K!-ord/Id!ntii!r
@ *!tt!r or L ollo!d - l!tt!r L M digit@ Ca! !niti"!@ 8nlimit!d l!ngth: Compil!r ma- u! 32
@ +onBt !gin ith L: 8!d - compil!r @ &ultipl! ord: Capitali7! or u! L @ Contant: Capitali7!
R!uir!m!nt
Con"!ntion
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 33/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d JAR 1#33
CGG 4aic
K!-ord/Id!ntii!r@ Id!ntii!r: 'am! o data
@ K!-ord: Id!ntii!r r!!r"!d or p!ciic languag! u!
@ (,ampl!: , data1 m-'am! a123c
igL"arial! "123
@ In"alid: 37 )E 3 Na3
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 34/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d JAR 1#3
CGG 4aic
+ata T-p!
@ Int!g!r: int
@ R!al: float *imit!d pr!ciion
double (,t!nd!d pr!ciion
@ Charact!r: char $n! charact!r onl-
@ *ogical: bool Tru! or al!
ardar!
Sotar!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 35/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d JAR 1#35
CGG 4aic
Int!g!r +ata
@ +icr!t! "alu!
@ Counting num!r
@ Poiti"! n!gati"! or 7!ro
@ >alu! p!cii!d - d!cimal digit
@ 'o d!cimal point
@ (,ampl!: 2 #;0 0 16
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 36/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©9/2010 John A. Rohr All Right R!!r"!d JAR 1#36
CGG 4aic
R!al +ata
@ Continuou "alu!
@ Poiti"! n!gati"! or 7!ro
@ >alu! p!cii!d - d!cimal digit
@ $ptional d!cimal point
@ $ptional ci!ntiic notation
@ (,ampl!: 3.1 #25.; 0 123 #3.5!;
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 37/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©
9/2010 John A. Rohr All Right R!!r"!d JAR 1#3
CGG 4aic
4ool!an +ata
@ Tru! or al! onl-
@ true and false ar! <!-ord in CGG
@ 'o 4ool!an data t-p! in C
al! r!pr!!nt!d - 7!ro "alu!
Tru! r!pr!!nt!d - an- non7!ro "alu!
Tru! "alu! o on! g!n!rat!d - compil!r
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 38/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©
9/2010 John A. Rohr All Right R!!r"!d JAR 1#3;
CGG 4aic
Charact!r +ata
@ Singl! it!m o a charact!r !t
@ &achin!#d!p!nd!nt: 4a!d on hardar! ASCII/A'SI (4C+IC
8nicod!@ Charact!r ar! !nclo!d in ingl! uot!
@ (,ampl!: OAO O7O O3O OO O O OQO
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 39/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©
9/2010 John A. Rohr All Right R!!r"!d JAR 1#39
CGG 4aic
String +ata
@ !ro or mor! it!m o a charact!r !t
@ Can ! !mpt- 'o charact!rD
@ Can ! on! charact!r
@ Can ! multipl! charact!r
@ String ar! !nclo!d in doul! uot!
@ (,ampl!: A String AC?N21
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 40/81
CS 31: Introduction To CS ITopic 1: Introduction to Programming
©
9/2010 John A. Rohr All Right R!!r"!d JAR 1#0
CGG 4aic
Charact!r ". String
@ Charact!r: 4uilt#in to CGG
char data t-p! Singl! charact!r onl- (nclo!d in ingl! uot!
@String: A"ailal! uing #include string string data t-p!
!ro or mor! charact!r (nclo!d in doul! uot!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 41/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©
9/2010 John A. Rohr All Right R!!r"!d JAR 1#1
CGG 4aic
Contant and >arial!@ (l!m!ntar- unit o inormation
@ Singl! data it!m@ 'am! i an id!ntii!r
@ T-p! i p!cii!d in d!claration
@ Contant i not chang!d onc! d!in!d
@ >arial! i !t - computation
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 42/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©
9/2010 John A. Rohr All Right R!!r"!d JAR 1#2
CGG 4aic
*it!ral@ Contant unit o inormation
@ Singl! data it!m@ Sp!ciic "alu!
@ Int!g!r: +!cimal num!rH 'o d!cimal point
@ loat: +!cimal num!rH =ith d!cimal point@ Charact!r: $n! charact!r o !t
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 43/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©
9/2010 John A. Rohr All Right R!!r"!d JAR 1#3
CGG 4aic
*it!ral (,ampl!@ Int!g!r: ;0
@ loat: ;.2 ;.2 ;.2(2 ;.2!2 @ +oul!: 16.2 1.62(1 1.62!1
@ *ong +l: ;.2* ;.2l ;.2(2* ;.2!2l
@ Charact!r: OO
@ String: String *it!ral
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 44/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#
CGG 4aic
(cap! S!u!nc!@ Charact!r ith p!cial m!aning
@Sp!cii!d in CGG ith pr!c!ding ac<lah@ *imit!d !t
n '!lin!
O Singl! Uuot!
+oul! Uuot!
4ac<lah
0 Charact!r String 'ullD T!rminator
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 45/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#5
CGG 4aic
+ata +!claration
@ &ut ! d!clar!d !or! u!
@ %i"! compil!r inormation aout th! data
'am!
T-p! Si7! 'ot or !l!m!ntar- data t-p!D
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 46/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#6
CGG 4aic
+ata +!claration (,ampl!
@ int iH
@ int ,1 ,2 acH@ loat 1 "olt po!rH@ doul! !ight "3 inpH
@ ool tat!H@ char ch l!tt!rH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 47/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#
CGG 4aic
+ata Initiali7ation
@ &ut ! initiali7!d !or! irt u!
@ To a- to initiali7!
+!claration
Aignm!nt
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 48/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#;
CGG 4aic
+ata Initiali7ation (,ampl!
@ int i F 0H
@ int ,1 F 3 ,2 F #5 ac F 3 5 G 9H@ loat 1 F 0 "olt F 120.5 po!r F 0.1H@ doul! !ight F 100 "3 F 0.03 inp F #2H
@ ool tat! F al!H@ char ch F OO l!tt!r F O)OH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 49/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#9
CGG 4aic
Arithm!tic $p!rator
@ 8nar- -nta,
op!rator $p!rand: #"alu!
@ 4inar- -nta,
$p!rand op!rator $p!rand: a G 5
@ Par!nth!!
num!rD
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 50/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#50
CGG 4aic
Arithm!tic $p!rator
@ G Addition
@ # Sutraction
@ &ultiplication
@ / +i"iion
@ V &odulu R!maind!r
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 51/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#51
CGG 4aic
+i"iion
@ To int!g!r op!rand Int!g!r r!ult
(,ampl!: 12 / 5 F 2@ To r!al op!rand R!al r!ult
(,ampl!: 12.0 / 5.0 F 2.
@ &i,!d op!rand R!al r!ult
(,ampl!: 12.0 / 5 F 2.
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 52/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#52
CGG 4aic
+ata i!rarch-
int/char loat
doul!
long doul!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 53/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#53
CGG 4aic
Incr!m!nt and +!cr!m!nt
@ GG Pr!incr!m!nt/Potincr!m!nt
Pr!incr!m!nt: Incr!m!nt !or! u!
Potincr!m!nt: Incr!m!nt at!r u!
@ ## Pr!d!cr!m!nt/Potd!cr!m!nt
Potd!cr!m!nt: +!cr!m!nt !or! u!Potd!cr!m!nt: +!cr!m!nt at!r u!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 54/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#5
CGG 4aic
(,pr!ion
@ $p!rand and op!rator
@ Arithm!tic calculation
@ T-p! con"!rion h!r! allo!d
Implicit: Automatic
(,plicit: Cat
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 55/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#55
CGG 4aic
(,pr!ion (,ampl!
@ "ar1
@ it!m1 5
@ a G cD
@ 0.5 a! h!ight
@ , p / m nD G tD
@ "ar F num / di"D di"
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 56/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#56
CGG 4aic
Automatic T-p! Con"!rion
@ Promotion: 'o lo o accurac-
(,ampl!: Int!g!r loat10 loat "arial! !com! 10.0
@ +!motion: &a- lo! accurac-
(,ampl!: loat Int!g!r 10.1 Int!g!r "arial! !com! 10
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 57/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#5
CGG 4aic
CGG Stat!m!nt
@ T!rminat!d - !micolon
@ $n! action Aignm!nt
Input/$utput
Control
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 58/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#5;
CGG 4aic
Aignm!nt Stat!m!nt
@ S!t th! "alu! o a "arial!
@ >arial! nam! id!ntii!rD on l!t id!
@ F op!rator
@ >alu! !,pr!ion on right
@ (,ampl!: "alu! F 1.23H
data F a! G 5H
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 59/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#59
CGG 4aic
Aignm!nt Stat!m!nt
@ $rd!r o !"aluation not guarant!!d
a G i th! am! a G a Prol!m: n G GGnD: A"oid thi u!
@ Shorthand notation or om! tat!m!nt
>arial! "alu! chang!d - G # / or V orm i "arial! GF !,pr!ionH
Sam! a "arial! F "arial! G !,pr!ionH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 60/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#60
CGG 4aic
Aignm!nt (,ampl!
@ an F actor1 actor2H
@ F "1 G "2 "3 G " / "5DH
@ d2 GF 16H
@ 7 F i7!1 a!H
@ a F PI rH
@ r VF dH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 61/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#61
CGG 4aic
4aic String $p!ration
@ +!claration: tring tring1H
tring tring2 tring3H
@ Initiali7ation: tring tring F A StringH
@ Aignm!nt: tring1 F H
tring2 F Anoth!r tringH
tring3 F tringH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 62/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#62
CGG 4aic
Conol! Input/$utput
@ 8! iotr!am lirar-
Ninclud! Wiotr!amX
uing nam!pac! tdH
@ Input: cin tr!am !,traction op!rator
@ $utput: cout tr!am in!rtion op!rator
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 63/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#63
CGG 4aic
Conol! Input: cin
@ R!ad "alu! rom <!-oard tdinD
@ >alu! !parat!d - hit! pac! Spac! ta or n!lin!
@ *in! !nd!d ith n!lin! Wr!turnXD
@ (,ampl! cin XX m-+ataH
cin XX irt>alu! XX !cond>alu!H
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 64/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#6
CGG 4aic
Conol! Input: cin
@ Input "alu! can ! on multipl! lin!
@ (ach input "arial! r!c!i"! on! it!m
@ (,ampl!
Singl! lin!: 29 3.15 #12.5Wr!turnX
&ultipl! lin!: 29 3.15Wr!turnX
#12.5Wr!turnX
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 65/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#65
CGG 4aic
Conol! Input: cin.ignor!D
@ &a- n!!d to ad"anc! to n!,t lin!
@ &ut <ip o"!r n!lin! at !nd o lin!
@ cin.ignor! pro"id! th! capailit-
@ S<ip up to 10000 charact!r to n!lin!
@ Stop on irt charact!r o n!,t lin!
cin.ignor!10000 OnODH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 66/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#66
CGG 4aic
Conol! $utput: cout
@ +ipla- "alu! on th! cr!!n tdoutD
@ String "alu! and !,pr!ion
@ &ultipl! it!m not !parat!d
@ (nd lin! ith \n or endl pr!!rr!dD
@ (,ampl!: cout WW (,ampl! WW !ndlH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 67/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#6
CGG 4aic
Conol! $utput: cout
@ 'um!r ormatting i automatic
@ Compil!r d!cid! hat ormat to u!
@ Can ! !,plicitl- controll!d: !.g. 123.5
cout.!tio::i,!dDH
cout.!tio::hopointDH
cout.pr!ciion2DH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 68/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#6;
CGG 4aic
Conol! $utput: cout
@ $utput lin! !nd!d !,plicitl-
@ 'o n! lin! unl! p!cii!d
@ $n! tat!m!nt: &ultipl! output lin!
cout WW a WW !ndl WW WW !ndlH
@ &ultipl! tat!m!nt: $n! output lin!
cout WW aH cout WW H cout WW WW !ndlH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 69/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#69
CGG 4aic
Conol! $utput: cout
@ 8! cout or t!,t prompt
cout WW (nt!r a "alu!: H cin XX m->alu!H
@ (nd inal output ith !nd o lin!
cout WW !ndlH
Put u!u!nt output on n!,t lin!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 70/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#0
CGG 4aic
Conol! $utput: c!rr
@ Alt!rnat! output to cr!!n td!rrD
@ =or< li<! cout
@ Can ! r!dir!ct!d to di!r!nt il!
@ (,ampl!
c!rr WW (rror m!ag!? WW !ndlH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 71/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#1
CGG 4aic
Conol! I/$ (,ampl!
cout WW (nt!r data "alu!:H
cin XX "alu!H
cout WW Th! "alu! i
WW "alu!
WW !ndlH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 72/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#2
CGG 4aic
&or! String $p!ration
@ String l!ngth: tring.l!ngthD or tring.i7!D
@ Singl! charact!r !,traction: tringY0Z@ Sutring: tring.utrtartD@ Sutring: tring.utrtart countD
@ String output: cout WW tringH@ String input: cin XX tringH 1 =ordD@ String input: g!tlin!cin tringDH 1 *in!D
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 73/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#3
CGG 4aic
String $p!ration (,ampl!
@ tring t F +ata tringH
@ t.l!ngthD or t.i7!D i 11@ tY0Z i O+O@ tY6Z i Ot[@ tY11Z do! not !,it
@ t.utr3D i a tring\@ t.utr;2D i in\@ t.utr93D i ill!gal
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 74/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#
CGG 4aic
String Input
@ cin XX tringH
R!ad on! ord: 8p to hit! pac! Stop on hit! pac! charact!r
@ g!tlin!cin tringDH
R!ad on! compl!t! lin! Stop on irt charact!r o n!,t lin!
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 75/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#5
CGG 4aic
String Input (,ampl!
@ Input lin!: Thi i om! t!,t
@ tring t1 t2 t3H@ g!tlin!cin t1D put
Thi i om! t!,t in t1 and top at th! tart o th! n!,t lin!
@ cin XX t2H cin XX t3H put Thi in t2 and i in t3 and top at th! pac! at!r i
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 76/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#6
CGG 4aic
*irari!
@ Pro"id! uilt#in capailiti!
@ Includ!d - pr!proc!or @ &ad! a"ailal! ith #include tat!m!nt
@ (,ampl!
Ninclud! Wiotr!amX Str!am input/output Ninclud! WtringX String
Ninclud! WctliX!,it rand and oth!r
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 77/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#
CGG 4aic
'am!pac!
@ Pro"id! lat nam! or id!ntii!r
@ R!ol"! nam! conlict ith lirari!
@ Standard lirari! u! std
@ 'am!pac! nam! p!cii!d ith using
(,ampl!: uing nam!pac! tdH
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 78/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#;
CGG 4aic
Comm!nt
@ To t-p! can ! int!rmi,!d
@ Singl! lin! comm!nt pr!!rr!dD 4!gin ith //
(nd at !nd o lin!
@ &ultipl! lin! comm!nt alt!rnat!D 4!gin ith /
(nd ith /
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 79/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#9
CGG 4aic
Comm!nt (,ampl!
@ // Thi i a ingl!#lin! comm!nt
@ , F 1H // Thi i a partial#lin! comm!nt@ /
Thi i a mulitpl!#lin! comm!nt
Th!r! can ! man- lin! Thi i th! lat lin! o th! comm!nt
/
C 4 i
8/9/2019 CS31-1 ucla
http://slidepdf.com/reader/full/cs31-1-ucla 80/81
CS 31: Introduction To CS I
Topic 1: Introduction to Programming
©9/2010 John A. Rohr
All Right R!!r"!d
JAR 1#;0
CGG 4aic
Programming St-l!
@ =rit! und!rtandal! cod!
@ 8! m!aningul data nam!
@ 8! nam! or contant
@ 8! comm!nt li!rall-
@ Prot!ct againt u!r !rror
8P+AT(S