You are on page 1of 41

MNHC

G ingvin:N guyncH ong


B m niuK hinTng
K hoaininT
iH cBchK hoaTp.H CM
Em ail:ndhoang@ hcm ut.edu.vn

N idungm nhc(10
chng)
(14tun=28titLT+14titBT)

Chng1:nlicckinthccbnvmytnh
Chng2:Cckiudliuvthaotc
Chng3:GiithiungnnglptrnhC
Chng4:Ccthnhphncbnvkiudliuca
C
Chng5:Cclnhiukhinvvnglp
Chng6:Hm
Chng7:LplutrcabinSchuynkiu
Chng8:Mng
Chng9:Pointer

Tiliutham kho
Tiliu:

TinHc2
ngThnhTn

KThutLpTrnhC
G S.Phm V nt

GioTrnhC

N guynH uTun

GiotrnhBTKthutlptrnh

nhgi
Thigiak:20%
Thicuik:80%

CHNG5

C C L N H iU K H IN
V V N G L P

N idungchng5
5.1Lnhnvlnhphc
5.2LnhIF
5.3LnhSWTICHCASE
5.4LnhWHILE
5.5LnhDOWHILE
5.6LnhFOR
5.7LnhBREAKCONTINUE
5.8LnhRETURN
5.9LnhGOTO
5.10LnhRNG

L nhnvlnhphc

L nhn
Lnh n l mt biu thc thuc loi bt k kt thc
bng;
VD: a+=1;
b=a+1;
c=!a&&b;

L nhphc
Lnhphcbaogmmthaynhiulnhnnmtrong
du{}
VD: {
a+=1;
b=++a;
c=a>=b;
}

L nhIF
Lnhifccccutrcsau:

C ulnhiukinif
if(<dieukien>)
{
/*caclenhthuchien True
neudieukiendung*/
}
statement(s)

expression

False

Next statement

LptrnhCCN TT2.20022005

11

Vd
1. #include<stdio.h>
1. intm ain(){
2. intb;
1. printf("Enteravalue:");
2. scanf("% d",& b);
3. if(b<0)
4.

printf("Thevalue\
isnegative\n");
5. return0;
6. }
LptrnhCCN TT2.20022005

12

if else
if(<dieukien>)
{
/*caclenhthuchienneudieukiendung*/
}
else
True
{
expression
/*caclenhthuchien
neudieukiensai*/ statement1
}

Next statement
LptrnhCCN TT2.20022005

False

statement2

13

Vd

printf(1/X is:);
if(X )
printf(% f\n,1/X );
else
printf(undefined
\n);

LptrnhCCN TT2.20022005

14

Linginnhngdphm
1. #include<stdio.h>
1. intm ain(){
2. intb;
1.
2.
3.
4.
5.
6. }

printf("Enteravalue:");
scanf("% d",& b);
if(b==5)
printf(bis");printf(5\n);
return0;

LptrnhCCN TT2.20022005

15

Linginnhngdphm
1. printf(1/X is:);
2. if(X <0);
3.

printf(X isnegative\n);

4.

LptrnhCCN TT2.20022005

16

Vd:Kim tranhiuiu
kin
1. #include<stdio.h>
2. intm ain(){

3.
4.
5.
6.
7.
8.
9.
10.
11.
12.}

intb;

printf("Enteravalue:");
scanf("% d",& b);
if(b<0)

printf("Thevalueisnegative\n");
elseif(b==0)

printf("Thevalueiszero\n");
else
printf("Thevalueispositive\n");
return0;

LptrnhCCN TT2.20022005

17

iukinlngnhau
Culnhifcthclngvonhau.
1. if(X >=0){
2.
if(Y <0)
3.
Y =Y +sqrt(X );
4. }
5. else
6.
Y =Y +sqrt(X );

Tuynhin,cnchnthtcccplnhif else khilngcc

lnhif.N ukhngsphtsinhli.

1. if(X >=0)
2.
if(Y <0)
3.
Y =Y +sqrt(X );
4. else
5. Y =Y +sqrt(X );
LptrnhCCN TT2.20022005

18

V dlnhIF
1. Vitchngtrnhnhp2svinrasnhnhtv
lnnht?(PhamNguyenDuy,ChungHuuHien)
2. Vitchngtrnhgiiphngtrnhbc2(DuongHungManh,
NguyenTanVinh,NguyenVanDuc,DOHuuTan,TranTrungHieu,DinhDucNguyen,BuiHuuSon)

3. Nhpvosnguyndngnvi0<n<8.Tytheo
n hy in ra cc ch Sunday, Monday,,Saturday
(TranMinhKhoa,NguyenVinhSon,LuongKhoiDu,DangQuangNhat)

4. Nhp vo 3 cnh ca tam gic. In ra tam gic


loi g ? (thng, vung, u, cn, vung cn) (Pham
NguyenDuy,HoVanPhap,VuDinhNgoc)

L nhSW ITC H C A SE
Cphp:

switch(bieuthuc)
{
casehang_1:
lenh_1;
break;

casehang_n:
lenh_n;
break;
default:
lenh;
break;
}

L nhSW ITC H C A SE

Vd:i1snguynsangchuiktltnccm nhc

#include<stdio.h>
#include<conio.h>
m ain()
{intm a;
printf("\nchom cnchuyn");scanf("% d",& m a);
sw itch(m a)
{
case0:printf("\nlptinhca");
break;
case1:printf("\nlptinhcb");
break;
case2:printf("\nlptrungcp");
break;
case3:printf("\nlpchuynvin");
break;
default:printf("\nlpthitinhcph");
}

L nhW H ILE
Cphp:

w hile(biuthc1)lnh1;

N guyntcthchin:
+B1.Tnhgitr
cabiuthc1
+B2.N ugitr
cabiuthc1sai(=0)thchngtrnhra
khivngw hile
+B3.N ugitr
cabiuthcngththchinlnh1v
quaylibc1(b1)
Ch:Biuthc1cthgm nhiubiuthcnhngtnh
ngsaiphthucvobiuthccuicng

L nhW H ILE

Vd:TnhtngS=1+2+3++N

#include<stdio.h>
m ain()
{
intN ,S=0,i=0;
printf(N hapsonguyenduong:);scanf(% d,& N );
w hile(i<=N )
{
S=S+i;
i++;
}
printf(Tong=% d,S);
}

L nhW H ILE

Vd:TnhtngS=1+2+3++N

#include<stdio.h>
m ain()
{
intN ,S=0,i=0;
printf(N hapsonguyenduong:);scanf(% d,& N );
w hile(i<=N )S+=i++;
printf(Tong=% d,S);
}

L nhW H ILE

Vd:TnhtngS=1+2+3++N

#include<stdio.h>
m ain()
{
intN ,S=0,i=0;
printf(N hapsonguyenduong:);scanf(% d,& N );
w hile(S+=i++;i<=N );
printf(Tong=% d,S);
}

L nhD O W H ILE
Cphp:

dolnh1;w hile(biuthc1);

N guyntcthchin:
+B1.M ythchinlnh1
+B2.Sautnhgitr
cabiuthc1,nugitr
cabiu
thc1saithchngtrnhthotrakhivnglp.N ugitr

cabiuthc1ngthquaylibc1.
Ch:w hile: iukinckim tratrc,nung
m ithchin.dow hile:culnhcthchintrckhi
kim tra.Culnhthchinbaogitnhtl1ln.
Biuthc1cthgm nhiubiuthcnhngtnhngsai
phthucvobiuthccuicng

L nhD O W H ILE

V d:tnhpivisaiseps=1E4,pi=44/3+4/54/7+...eps
#include<stdio.h>
m ain()
{floatpi,dau,i,eps,saiso;
i=1.0;dau=1;saiso=1e4;
pi=4.0;
printf("\nangxlvuilngi!");
do
{
eps=4.0/(2.0*i+1.0);
pi+=dau*eps;dau=dau*1.0;i+=1.0;
}
w hile(eps>saiso);
printf("\nspil:"% f",pi);
}

L nhFO R
Cphp:

for(bt1;bt2;bt3)lnh;

N guyntcthchin:
+B1.Tnhgitr
cabt1
+B2.Tnhgitr
cabt2
+B3.N ugitr
cabt2sai(==0)ththotkhivngfor;nu
bt2ng(!=0)ththchinlnh

+B4.Tnhgitr
cabt3,quaylikim trabt2
Ch:bt2vngm tthnccoilunng
bt1,bt2,bt3cthlccbiuthcphy

L nhFO R
Vd1:TnhtngS=1+2+3++N
for(i=0,S=0;i<=N;i++)S+=i;
for(i=0,S=0;i<=N;)S+=i++;
for(i=0,S=0;i<=N;S+=i,i++);
for(i=0,S=0;i<=N;S+=i++);
Vd2:Tnhtng
S=1!+(1+2)!+....+(1+2+....i)!.....(1+2+..n)!

L nhFO R

Vd2:Tnhtng
S=1!+(1+2)!+....+(1+2+....i)!.....(1+2+..n)!
#include<stdio.h>
m ain()
{
inti,j,t,n;doublegt,s;
printf("nhpn=");scanf("% d",&n);
for(s=0,t=0,i=1;i<=n;++i)
{
t=t+i;
for(gt=1,j=1;j<=t;++j)
gt=gt*j;s=s+gt;
}
printf("tongs=% 15.0f",s);
}

L nhFO R

Vd2:Tnhtng
S=1!+(1+2)!+....+(1+2+....i)!.....(1+2+..n)!
#include<stdio.h>
m ain()
{
inti,j,t,n;doublegt,s;
printf("nhpn=");scanf("% d",&n);
for(s=0,t=1,i=1;i<=1;++i,t=t+
1)
{
for(gt=1,j=1;j<=t;++j)
gt*=j;s+=gt;
}
pr
intf("tongs=% 15.0f",s);
}

L nhFO R

Vd2:Tnhtng
S=1!+(1+2)!+....+(1+2+....i)!.....(1+2+..n)!
#include<stdio.h>
m ain()
{
inti,j,t,n;doublegt,s;
printf("nhpn=");scanf("% d",&n);
for(s=0,t=1,i=1;i<=n;++i,
t=t+i,s+=gt)
for(gt=1,j=1;j<=t;gt*=j,++j);
printf("tongs=% 15.0f",s);
}

L nhFO R

Vd2:Tnhtng
S=1!+(1+2)!+....+(1+2+....i)!.....(1+2+..n)!
#include<stdio.h>
m ain()
{
Inti=1,j=1,t=1,n;doublegt=1,s=
0;
printf("nhpn=");scanf("% d",&n);
for(;i<=n;++i,t=t+i,s+=gt)
{
for(;j<=t;
gt*=j++/*gt=j;++j*/
}
printf("tongs=% 15.0f",s);
}

Bitp
1. Tm ccsnm trongkhongt150n1400tho
tnhchtsbngtnglpphngccchsca
chng
:
V d:153=13+53+33hoc370=33+73+03
2. S1=1*3*5*7*9*...*(2n1)V oM inhTien,V oD inhM inh,H oangN gocBao
S2=2*4*6*8* *(2n)
3. S1=12+34+56+78+ (N guyenD inhPhuong,N guyenH ongD uc,Tran
D inhK hai,N guyenM inhThuan,D inhD ucN guyen,D uongV u)

L nhBR EA K & C O N TIN U E


Cphp:

break;

D ngthotkhivnglp.K higpculnhnytrongvng
lp,m yrakhivchnculnhsaucclnhtrn.N u
nhiuvnglp>breaksthotrakhivnglpgnnht.

Cphp:continue;
K higplnhnytrongccvnglp,m ysbquaphncn
litrongvnglpvtiptcthchinvnglptiptheo.
ivilnhform ystnhlibiuthc3(bt3)vquayli
bc2.
ivilnhw hile,dow hilem ystnhligitr
cabiu
thc1vquaylibc1.

L nhBR EA K & C O N TIN U E


Vd:N hp1ktkckttrngvbquacc
ktkhnghplvktthckhinESC .
charkytu;
w hile(1)/*lunlunngvnglpvnhcu*/
{
kytu=getch();
if(kytu==27)break;
if(kytu>122||kytu<65)continue;
printf(c,kytu);
}

L nhR ETU R N
Cphp:

return;
returnbieuthuc;
return(bieuthuc);

Lnhnydngthotrakhihm hinthitrvhm
gin,cthtrvtr.
Lnhnysktthchm dnnm
utrongthnhm .K higplnhnyCskhngthchinlnh
nosaulnhreturn
VD:
intso_sanh(inta,intb)
{
return(a>b)?1:(a==b)?0:1;
}

L nhG O TO & R N G
Cphp:

gotonhan;
nhan:lenh;

C H :PH M V IN H N TR O N G C N G 1H M

L nhG O TO & R N G
Vd2:Tnhtng
S=1!+(1+2)!+....+(1+2+....i)!.....(1+2+..n)!
#include<stdio.h>
m ain()
{
inti=1,j=1,t=1,n;floatgt=1,s=0;
scanf("% d",&n);
for(;;)
{for(;;)
{gt*=j++;
if(j>t)gototong;}
tong:s+=gt;++i,t=t+i;
if(i>n)gotoKT;}
KT:printf("Tong=% 6.0f\n",s);
}

N uyenX uanTruong(2),TranM inhK hoa,D oanV iK im

K hanh,N guyenD inhH uy,D uongH ungM anh,PhanG ia


H uy
H oV anPhap(2),H uynhTriN han,H uynhD ucTri,
N guyenD uyN goc(2),LeTruongPhu(3),BuiA nhD uc,Le
V inhTinh(2),N guyenV inhSon(2),N guyenPhuA n,
N guyenH oangD uy
N guyenH ongD uc(3),V oD ucThuan(3),Pham Canh
H ung(4),N guyenV anD uc(3),V uD inhN goc,D oH uu
Tan(2),N guyenX uanThich(3),TranQ uocH ieu,N guyen
N gocThach.

You might also like