40
3.7 for 3 3 1. 2. 3. 121002 Introduction to Computer Programming 1

chapter4_2

Embed Size (px)

DESCRIPTION

โครงสร้างควบคุม การวนรอบ

Citation preview

Page 1: chapter4_2

3.7 �������� ���������� for

� ��������������������� ������������� � !���������"#����������� $%"&��#!'(�� #��)��%�# �������* 3 (+��,#���������� $%"&��#!'(�� #��)��%�# �������* 3 (+��,#

� 1. �(���.�/0�"#�0�+����+ �1/����������

� 2. (+�0�+$# ��,�#�3"

� 3. (+���4��!���4��(�"#�0�+����+ �1/����������

121002 Introduction to Computer Programming1

Page 2: chapter4_2

����������� ������������������� Loop

����������� for ���� loop for ����������������������������������� !�������� ���"# !�$%�� ��&#������������������'�����&#� ������! !������&#� �� (initialization) ����!�����/� ��� 0

���������������� ���!��" for

condition"*���+,

-�"

�.-

����������� ������������������� Loop

end

121002 Introduction to Computer Programming2

0�� ��� �*�1�������������������� loop

-�"

���!��"���"3 4� �� Lop for

Page 3: chapter4_2

�'�� "#������������������ for

�'�� :

for (initialization expression; loop repetition condition; update expression)

����� �(���.�/0� ��,�#�3"����������� �F.�/�(�/4 �(�

121002 Introduction to Computer Programming3

����� �(���.�/0� ��,�#�3"����������� �F.�/�(�/4 �(�

Page 4: chapter4_2

0�+#!(��"#������������������ for 0�+#!(��: /*Display N asterisks*/

for (count = 0;

count < 10;

#include <iostream.h>#include <conio.h>int main(){

int count;for(count = 0; count < 10; count+=1)count < 10;

count += 1)

cout << "*";

��,#: for (count = 0; count <10; count += 1)

for(count = 0; count < 10; count+=1){

cout << "*";}getch();return 0;

}

121002 Introduction to Computer Programming4

��,#: for (count = 0; count <10; count += 1)

cout << "*";

R44�FS):

**********

Page 5: chapter4_2

0�+#!(��"#������������������ for 0�+#!(��: /*Display Round No.*/

for (i = 1; i <5; i += 1)for (i = 1; i <5; i += 1)

cout << VRound : V << i << endl;

R44�FS):

Round : 1

Round : 2

#include <iostream.h>#include <conio.h>int main(){

int i;for(i = 1; i < 5; i+=1)

121002 Introduction to Computer Programming5

Round : 2

Round : 3

Round : 4

for(i = 1; i < 5; i+=1) {

cout << "Round : " << i << "\n";}getch();return 0;

}

Page 6: chapter4_2

� XY��� 1. ��������� �!"#!"$%&'�(� 1 )*+ 10 ,*-+�./+0�1�2345 /&+��6

1 2 3 4 5 6 7 8 9 10 �/�>?2@A3.&-+'��50 for

#include <iostream.h>#include <conio.h>#include <conio.h>int main(){

int i;cout << "Print value from 1 to 10 by for-statement" << "\n";

for(i = 1; i <= 10; i+=1){

cout << i << "\t";

121002 Introduction to Computer Programming6

cout << i << "\t";}

getch();return 0;

}

Page 7: chapter4_2

2. ��������� �!13B(�'! 1 )*+ 10 (1 + 2 + 3 + ... + 10 = ?) �/�>?2@A3.&-+ '��50 for

#include <iostream.h>#include <conio.h>int main(){

int i, total=0;int i, total=0;cout << "Compute summation of 1 to 10 by for-statement" << "\n\n";

for(i = 1; i <= 10; ++i){

total = total + i;cout << "i==> " << i << "\n";cout << "Sum of 1 to 10 is ==> " << total << "\n";

}

121002 Introduction to Computer Programming7

}

getch();return 0;

}

Page 8: chapter4_2

3. ��������� �!13B(�'! 1 )*+ @E3F�-5#�"G%��23H� �/�>?2@A3.&-+ '��50 for

start

Input num

Sum = 0, i = 1;Sum = 0, i = 1;

i <= num

yes

no

Sum = Sum + i

i++

Print i, sum

end

121002 Introduction to Computer Programming8

i++

Page 9: chapter4_2

3.8 �������� ���������� while

� ����������������������������������0(�� b � !$%/����0�+$# ��,�#�3" �&��0�+$# ��,�#�3" �&��

� ��c����R44�FS)"#���,�#�3"3/(��(��� d'�!) ($�.�) $%�����������,#�41(/���������0(4%�#

� (+���c����R44�FS)"#���,�#�3"��(��� d'�!) (��e$) $%�!1 �����������,#�41(/���������# � !"�/3����������f� 3�$��

121002 Introduction to Computer Programming9

��������,#�41(/���������# � !"�/3����������f� 3�$���# "#�������� while �4!

Page 10: chapter4_2

���������������� ���!��" while while �������/!#$�"#�"������$������ 6 �� Loop �8��"8!�9:�������$��;�$���/!#$�8��<�!���/!#$ for � � ��$�!��"#����=� while %�� ��$���������;�$������$���"#������ � � ��$�"�$�#��%�"#������>��?$�����������/!#$ while %'�

������"*���+,������ Loop

condition"*���+,

-�"

�.-

������"*���+,������ Loop

���-�����!��" while

121002 Introduction to Computer Programming10

�1�� ���1""*���+,�������� loop

-�"

���!��"���"3 4� �� Loop while

Page 11: chapter4_2

�'�� "#������������������ while

�'�� : �'�� :

while (loop repetition condition)

{

statement;

}

121002 Introduction to Computer Programming11

}

Page 12: chapter4_2

0�+#!(��"#������������������ while

0�+#!(��: /*Display N asterisks.*/count = 0;

#include <iostream.h>#include <conio.h>count = 0;

while (count < 10){cout << "*i;count = count + 1;

#include <conio.h>int main(){

int count;count = 0;

while (count < 10){

cout << "*";

121002 Introduction to Computer Programming12

count = count + 1;}

R44�FS): **********

cout << "*";count = count + 1;

}

getch();return 0;

}

Page 13: chapter4_2

0�+#!(��"#������������������ while 0�+#!(�� /*Display Round No.*/

i = 1;while (i < 5){{

cout << VRound : V << i << endl;i = i + 1;

}R44�FS):

Round : 1 Round : 2

int main(){

int i;i = 1;while (i < 5){

121002 Introduction to Computer Programming13

Round : 1 Round : 2 Round : 3 Round : 4

{cout << "Round : " << i << endl;i = i + 1;

}getch();return 0;

}

Page 14: chapter4_2

� XY��� 1. ��������� �!"#!"$%&'�(� 1 )*+ 10 ,*-+�./+0�1�2345 /&+��6

1 2 3 4 5 6 7 8 9 10 �/�>?2@A3.&-+'��50 while

#include <iostream.h>#include <conio.h>#include <conio.h>int main(){

121002 Introduction to Computer Programming14

getch();return 0;

}

Page 15: chapter4_2

2. ��������� �!13B(�'! 1 )*+ 10 (1 + 2 + 3 + ... + 10 = ?) �/�>?2@A3.&-+ '��50 while

#include <iostream.h>#include <conio.h>int main(){

121002 Introduction to Computer Programming15

getch();return 0;

}

Page 16: chapter4_2

3. ��������� �!13B(�'! 1 )*+ @E3F�-5#�"G%��23H� �/�>?2@A3.&-+ '��50 while

start

Input num

Sum = 0, i = 1;Sum = 0, i = 1;

i <= num

yes

no

Sum = Sum + i

i++

Print i, sum

end

121002 Introduction to Computer Programming16

i++

Page 17: chapter4_2

3.9 �������� ���������� do/while

� �������� ���������� while �4% for $%0#�/����0�+$# �(�"#���,�#�3"�(#�+(�����$�.���,#��e$ �(#����$%0�+$# �(�"#���,�#�3"�(#�+(�����$�.���,#��e$ �(#����$%��������� (�41(/������) k�!���# �0(��������� ��4��lc%$%0#����������k�!���# �(#�#!(���#! 1 ������&�������������4��lc%����4(�+����/��f�������� do-while3

121002 Introduction to Computer Programming17

3

Page 18: chapter4_2

���������������� ���!��" do while do while �������/!#$�"#�"������$������ 6 �� Loop �8��<���/!#$ while � � ��$�!��"#���/!#$ do while ���'/��$�#��%;�8!$����"#%'����$�� �����/!#$A�<�� loop %��8�� 1 �� ����!���?$���<<����8!���'/��$�#��%;�"���!�$ C���$�#��%;������&$�����$�� �����/!#$�� loop C���$�#��%;������>������������/!#$ do while �!��"

������"*���+,�������� loop

condition-�"

���!��"���"3 4� �� Loop do while

121002 Introduction to Computer Programming18

condition"*���+,

-�"

�.-���-��

���!��" while

Page 19: chapter4_2

�'�� "#������������������ do/while

�'�� : do�'�� : dostatement;

while (loop repetition condition);

121002 Introduction to Computer Programming19

Page 20: chapter4_2

0�+#!(��"#������������������ do/while 0�+#!(��: /* Display Round No.*/

i=1;do{ cout << VRound : V << i << endl;

{ cout << VRound : V << i << endl;i = i + 1;

}while ( i < 5);

R44�FS):

int main(){

int i;i = 1;do{

cout << "Round : " << i << "\n";

121002 Introduction to Computer Programming20

R44�FS):Round : 1Round : 2Round : 3Round : 4

cout << "Round : " << i << "\n";i = i + 1;

}while (i <5);getch();return 0;

}

Page 21: chapter4_2

�+�/�0�0(��/*Display Round No.*/i = 5; while (i < 5) // ����� ��,�#�3"�(#�{ { cout << Vwhile Round : V << i << endl;i = i + 1;}

i=5;do

121002 Introduction to Computer Programming21

do{ cout << Vdo-while Round : V << i << endl;i = i + 1;

} while ( i < 5); // ����� ��,�#�3"���4��

Page 22: chapter4_2

3.10 ������ break �4% continue

� ������ break ���������������������!1 ������������k�!���# "#��������� ���������� /��'�� ������# "#��������� ���������� /��'�� �����

� break;� ������ continue ���������������!��4.����������!����4,##!'(��

�41(/������ k�!���# "#��������� ���������� /��'�� �����

121002 Introduction to Computer Programming22

�'�� �����

� continue;

Page 23: chapter4_2

/012356 78590:6 break;

#include <iostream.h>#include <conio.h>int main(){

int x;for(x = 1; x < 10; x+=1)for(x = 1; x < 10; x+=1){

if (x == 5){break; // >?22AB5ACDE F5 x == 5

}cout << x ;

}

121002 Introduction to Computer Programming23

}getch();return 0;

}

R44�FS)1234

Page 24: chapter4_2

/012356 78590:6 break;

#include <iostream.h>#include <conio.h>int main(){

int x;for(x = 1; x < 10; x+=1)for(x = 1; x < 10; x+=1){

if (x == 5){continue; // GHI:2J0KLEJMAJH x==5 BNO8565K/2O0KOP LQ3RHO859M//GHK/

?C06 continue}cout << x ;

}

121002 Introduction to Computer Programming24

}getch();return 0;

}

R44�FS)12346789

Page 25: chapter4_2

3.11 Formulating Algorithm

� ��c�d&�l� 1, ����������� !��0�+�� �1/

� ������% +�+.S������������������ while ��,# do/while � ������% +�+.S������������������ while ��,# do/while �0($%��0�+���0�+��&��������!�+(� ������ (Counter) �F,�#�% 1$���+���������$%����������1 ��&�� b ���$%��+.S������������ !��0�+�� �1/ �/,�#��� $���+������������(#�������+��# �������$%��.�/0�

121002 Introduction to Computer Programming25

�������$%��.�/0�

Page 26: chapter4_2

0�+#!(����c�d&�l� 1, ����������� !��0�+�� �1/�$�!)

0#�������%����r4��!"#����d&�l� 10 �����������!�0#�������%����r4��!"#����d&�l� 10 �����������!��&��3 ���� XY��� �4%3 R4�%��������4"$���+��0e/0����0( 0 f&� 100

��% +�+.S���������s*��

$%0#��� �%����"�3�, ����+c���(��r4��! �4%F./F)

121002 Introduction to Computer Programming26

$%0#��� �%����"�3�, ����+c���(��r4��! �4%F./F)R44�FS)##�/� ������"�!�������/$%��������������������������� �F,�#�� �%����"�3���4%��&��

Page 27: chapter4_2

0�+#!(����c�d&�l� 1, ����������� !��0�+�� �1//* Class average program with counter-controlled repetition */#include <iostream.h>int main(){

Enter grade: 98Enter grade: 76{

int counter, grade, total, average;/* initialization phase */total = 0;counter = 1;/* processing phase */while (counter <= 10) {

Enter grade: 76Enter grade: 71Enter grade: 87Enter grade: 83Enter grade: 90Enter grade: 57Enter grade: 79

121002 Introduction to Computer Programming27

{cout << "Enter grade: ";cin >> grade;total = total + grade;counter = counter + 1;

}

Enter grade: 57Enter grade: 79Enter grade: 82Enter grade: 94Class average is 81

Page 28: chapter4_2

0�+#!(����c�d&�l� 1, ����������� !��0�+�� �1/(0(#)

/* termination phase */average = total / 10;

Enter grade: 98Enter grade: 76Enter grade: 71average = total / 10;

cout<<"Class average is " << average << endl;return 0; /* indicate program ended successfully */

}

Enter grade: 71Enter grade: 87Enter grade: 83Enter grade: 90Enter grade: 57Enter grade: 79

121002 Introduction to Computer Programming28

Enter grade: 79Enter grade: 82Enter grade: 94Class average is 81

Page 29: chapter4_2

��c�d&�l� 2 3�FA3,6A3�/�>?2@E3%�'4%�3@G!

������% +�+.S������������������ while ������% +�+.S������������������ while

��,# do/while �0($%��0�+���0�+��&��������!�+(�

� ����!��� (sentinel value) �F,�# #�+(����

�� "#/'4�"�/���e$.���4+

121002 Introduction to Computer Programming29

�� "#/'4�"�/���e$.���4+

Page 30: chapter4_2

0�+#!(����c�d&�l� 2, ����������� !���(�0�+$0���1/�$�!)

0#�������%����r4��!"#����d&�l����������!�0#�������%����r4��!"#����d&�l����������!���(�� �!+�� ��c�d&�l� 1 �0(3/(��� $���+����d&�l������(�#�4(+����

��% +�+.S���������s*��

���� ����!��� �F,�# #�+(������ "#/'4�"�/���e$.��

121002 Introduction to Computer Programming30

���� ����!��� �F,�# #�+(������ "#/'4�"�/���e$.���4+ � !R'��$%F./F)�%����"�3�$��/ �4+$&�F./F)�(�0�+$0��4�3� �F,�#��������/�'+(�3 F./F)�%�����e$.���4+

Page 31: chapter4_2

��z ���!/0�+#!(����c�d&�l� 2

���'� �(�)*����+,' total �-�(./� 0���'� �(�)*����+,' counter �-�(./� 0�� �%����(�����"�3��� �%����(�����"�3�While R'��!��3/(3 �|#��(�0�+$0���"�3�

+��%����"��� total�F.�/�(�"#� counter "&����&���� �%����(�0(#3� (�&��#�$$%�����(�0�+$0���e3 )

If counter 3/(��(��� 0 {

121002 Introduction to Computer Programming31

If counter 3/(��(��� 0 {����� �� average ��(��� total ��� +! counterF./F) average

} ElseF./F) VNo grades were enteredi (!��3/(/�����|#��%����"�/�)

Page 32: chapter4_2

0�+#!(����c�d&�l� 2, ����������� !���(�0�+$0���1//* Class average program with sentinel-controlled repetition */#include <iostream.h>int main(){

float average; /*new data type : ����� 0�+���*/{

float average; /*new data type : ����� 0�+���*/int counter, grade, total;/* initialization phase : �������� �(���.�/0� */total = 0;counter = 0;/* processing phase : �����%/+4R4 */cout << "Enter grade, -1 to end: ";cin >> grade;while (grade != -1){

121002 Introduction to Computer Programming32

while (grade != -1){

total = total + grade;counter = counter + 1;cout << "Enter grade, -1 to end: ";cin >> grade;

}

Page 33: chapter4_2

0�+#!(����c�d&�l� 2, ����������� !���(�0�+$0���1/(0(#)/* termination phase */if (counter != 0){{

average = (float) total / counter;cout << VClass average isi << average;

}else

cout << "No grades were entered\n";getch();return 0; /* indicate program ended successfully */

121002 Introduction to Computer Programming33

getch();return 0; /* indicate program ended successfully */

}

Page 34: chapter4_2

R44�FS)��c�d&�l� 2, ����������� !���(�0�+$0���1/Enter grade: 75Enter grade: 94Enter grade: 97Enter grade: 97Enter grade: 88Enter grade: 70Enter grade: 64Enter grade: 83Enter grade: 89

121002 Introduction to Computer Programming34

Enter grade: 89Enter grade: -1Class average is 8250

Page 35: chapter4_2

��c�d&�l� 3 ��������+ �1/� �#�

��������+ �1/���R(��/��������������������+ �1/���R(��/������������

�+ �1/����/��f���/�0(#���3���,�#! b 3

�0(��������+ �1/� �#������������

�����. $�������%�# ��������+ �1/�#�

121002 Introduction to Computer Programming35

�����. $�������%�# ��������+ �1/�#�

�"� +!���

Page 36: chapter4_2

0�+#!(����c�d&�l� 3, �@�+.�23+@'0@G!�00,25��$�!) 0#�����"�!�������/�F,�#�1�R44�FS)R4���# "#�� � #�1*�0����0�+���$����(�!��� .� � !3 �� ��!�,�#"#����d&�l� 10 �� � !3 �� ��!�,�#"#����d&�l� 10 �� - ������d&�l�������# R(�� f� $����!�,�#$%/�0�+�4" 1 �"�!�#!'( - ������d&�l�������# 0� f� $����!�,�#$%/�0�+�4" 2 �"�!�#!'(

- �4%f����/����d&�l�/���+(� 8 ��# R(�� ��F./F)"#�+�/ VGood Testi tuitioni

��% +�+.S���������s*��- �� R4���# (�����,# �4" 1 ��,# 2) �"�3� � �"#�+�/ VEnter resulti (�(R4���# ) ����$#�1���������������/0#������ R4���# �(�0(#3�

121002 Introduction to Computer Programming36

���$#�1���������������/0#������ R4���# �(�0(#3�- �� $���+�R4���# �0(4%��. (R(���4%0�)- � ��1�R4���# �F,�# #�$���+�"#����d&�l����# R(���4%���d&�l����# 0�- f����d&�l�/���+(� 8 ��# R(�� ��F./F)"#�+�/ VGood Testi

Page 37: chapter4_2

��z ���!/0�+#!(����c�d&�l� 3���'� �(�)*����+,' passes (!��������3,�4 ��) �-�(./� 0���'� �(�)*����+,' failures (!��������3,���) �-�(./� 0���'� �(�)*����+,' student counter �-�(./� 1While student counter �#!�+(���,#��(��� 10 While student counter �#!�+(���,#��(��� 10

�� �(�R4���# �(�0(#3�If ���d&�l�# R(���F.�/��&������( passes

Else�F.�/��&������( failures

�F.�/��&������ student counter

121002 Introduction to Computer Programming37

�F.�/��&������ student counterF./F)$���+�"#� passesF./F)$���+�"#� failuresIf ���d&�l�/���+(� 8 ��# R(��

F./F) VGood Testi

Page 38: chapter4_2

0�+#!(����c�d&�l� 3, �@�+.�23+@'0@G!�00,25�/* Analysis of examination results */#include <iostream.h>int main(){

/* initializing variables in declarations */{

/* initializing variables in declarations */int passes = 0, failures = 0, student = 1, result;/* process 10 students; counter-controlled loop */while (student <= 10){

cout << "Enter result (1=pass, 2=fail): ";cin >> result;if (result == 1) /* if/else nested in while */

121002 Introduction to Computer Programming38

cin >> result;if (result == 1) /* if/else nested in while */

passes = passes + 1;else

failures = failures + 1;student = student + 1;

}

Page 39: chapter4_2

0�+#!(����c�d&�l� 3, �@�+.�23+@'0@G!�00,25�

cout << VPassed V << passes;cout << "Failed " << failures);cout << "Failed " << failures);if (passes > 8)

cout << "Raise tuition\n";return 0; /* successfully termination */

}

121002 Introduction to Computer Programming39

Page 40: chapter4_2

R44�FS)��c�d&�l� 3, �@�+.�23+@'0@G!�00,25�Enter Result (1=pass, 2=fail): 1Enter Result (1=pass, 2=fail): 1Enter Result (1=pass, 2=fail): 1Enter Result (1=pass, 2=fail): 2Enter Result (1=pass, 2=fail): 2Enter Result (1=pass, 2=fail): 1Enter Result (1=pass, 2=fail): 1Enter Result (1=pass, 2=fail): 1Enter Result (1=pass, 2=fail): 1Enter Result (1=pass, 2=fail): 1

121002 Introduction to Computer Programming40

Enter Result (1=pass, 2=fail): 1Enter Result (1=pass, 2=fail): 1Passed 9Failed 1Raise tuition