Professional Documents
Culture Documents
Chuong 4 - Cau Truc Vong Lap - SV
Chuong 4 - Cau Truc Vong Lap - SV
www.hcmute.edu.vn
LOGO
NỘI DUNG
1 Lệnh for
2 Lệnh while
3 Lệnh do…while
4 Các ví dụ
www.hcmute.edu.vn
LOGO
1 Lệnh for
Cú pháp:
www.hcmute.edu.vn
LOGO
1 Lệnh for
Hoạt động:
www.hcmute.edu.vn
LOGO
1 Lệnh for
Ví dụ 1:
for (i = 0; i < 3; i++)
{
printf(“Hello \n”);
}
www.hcmute.edu.vn
LOGO
1 Lệnh for
Ví dụ 2:
int i, a = 3;
for ( i = 0; i<5; i++)
a ++;
www.hcmute.edu.vn
LOGO
1 Lệnh for
Ví dụ 3:
int i, a = 3;
for ( i = 0; ; i ++ )
{
a += 2;
if ( a >= 8 )
break;
}
www.hcmute.edu.vn
LOGO
2 Lệnh while
Cú pháp:
www.hcmute.edu.vn
LOGO
2 Lệnh while
Ví dụ 1:
int i = 0;
while (i < 3)
{
printf(“Hello \n”);
i++;
}
www.hcmute.edu.vn
LOGO
2 Lệnh while
Ví dụ 2:
int a = 4, i = 0;
while ( i <4)
{
a = a + i;
i ++;
}
www.hcmute.edu.vn
LOGO
2 Lệnh while
Ví dụ 3:
int a = 4, i = 0;
while ( i <4)
{
i ++;
a = a + i;
}
www.hcmute.edu.vn
LOGO
2 Lệnh while
Ví dụ 4:
int a = 4, i = 0;
while ( i <=4)
{
i ++;
a = a + i;
}
www.hcmute.edu.vn
LOGO
3 Lệnh do…while
Cú pháp:
do
{
Khối lệnh ;
}
www.hcmute.edu.vn
LOGO
3 Lệnh do…while
Ví dụ 1:
int i = 0;
do
{
printf(“Hello \n”);
i++;
}
while (i < 4);
www.hcmute.edu.vn
LOGO
3 Lệnh do…while
Ví dụ 2:
int i = 0;
do
{
printf(“%d”, i);
}
while ( i ++ < 4);
3 Lệnh do…while
Ví dụ 3:
int i = 0;
do
{
printf(“%d”, i);
}
while ( ++i < 4);
Ví dụ 1:
int a = 0, i = 0;
while (i ++ < 10)
{ if (i % 2 == 0)
{ a = a + i; }
else
a = a + 2 * i;
if (i == 6)
break;
} printf("%d %d", a, i); www.hcmute.edu.vn
LOGO
Ví dụ 2:
int i, j = 0;
for (i = 3; i > 0; i--)
{
while (j++<10)
{
if (j % 2 == 1)
printf("%d", j);
}
} www.hcmute.edu.vn
LOGO
Ví dụ 3: int i, j = 0;
for (i = 3; i > 0; i--)
{ while (j++ < 10)
{ if (j == 7)
continue;
if (j % 2 == 1)
printf("%d", j);
}
}
www.hcmute.edu.vn
LOGO
Ví dụ 4: int i, j = 0;
for (i = 3; i > 0; i--)
{ while (j++ < 10)
{ if (j == 7)
break;
if (j % 2 == 1)
printf("%d", j);
}
}
www.hcmute.edu.vn
LOGO
4 Bài tập
Bài tập 1:
www.hcmute.edu.vn
LOGO
4 Bài tập
Bài tập 2:
www.hcmute.edu.vn
LOGO
4 Bài tập
Bài tập 3:
www.hcmute.edu.vn
LOGO
4 Bài tập
Bài tập 4:
4 Bài tập
Bài tập 5: