Upload
watcharin-puangplia
View
212
Download
0
Tags:
Embed Size (px)
DESCRIPTION
โครงสร้างควบคุม IF ELSE และ switch case
Citation preview
����� ����� 44
Department of Business Computer
Faculty of Business Administrator
Ratchaphreuk College
����� ����� 44���� ���������� ������(Control Structures)(Control Structures)
121002 Introduction to Computer Programming1
(Control Structures)(Control Structures)
��� �!"#$%
� 3.1 $)��*�+,� (Algorithm)
� 3.2 ���./��0� (Pseudocode)� 3.2 ���./��0� (Pseudocode)
� 3.3 ���� ������ (Control structure)
� 3.4 ���� 1 �23 .�� (Sequence structure)
� 3.5 ���� 5��� /26!$ if
121002 Introduction to Computer Programming2
3.5 ���� 5��� /26!$ if
� 3.6 ���� 5���2 0� /26!$ switch
��� �!"#$%
� 3.8 ���� 5��$ �3 =>3 while
� 3.7 ���� 5��$ �3 =>3 for � 3.7 ���� 5��$ �3 =>3 for
� 3.9 ���� 5��$ �3 =>3 do/while
� 3.10 �3 ��� break 52) continue
121002 Introduction to Computer Programming3
��� �!"#$%
� 3.11 $)��*�+,�$ �� (Formulating Algorithm)� $G�"#$% 1, $ �3 =>3 �.0IJ�1��*����� � $G�"#$% 1, $ �3 =>3 �.0IJ�1��*�����
(Case Study 1, Counter-Controlled Repetition)
� 3.12 $)��*�+,�$ �� �.0IJ��+,�$ /$2 K $�*22L (Formulating Algorithm with Top-Down, Stepwise Refinement) � $G�"#$% 2, $ �3 =>3 �.0IJ��L 1�K1 ���
121002 Introduction to Computer Programming4
� $G�"#$% 2, $ �3 =>3 �.0IJ��L 1�K1 ��� (Case Study 2, Sentinel-Controlled Repetition)
� $G�"#$% 3, ���� ������5��=�!* (Case Study 3, Nested Control Structure)
3.1 $)��*�+,� (Algorithm)
� $)��*�+,�I*���*�> �6! $)��*$ I*$ 5$�MNO� �.0K)M)$!�.��0 �>*1!* !M)$!�.��0 �>*1!* !
� $ $)�3
� 23 .�� !$ $)�3
121002 Introduction to Computer Programming5
3.2 ���./��0� (Pseudocode)
� ���./��0� �6! P % !0L Q�L/MR*� $ ���ST�/ �0*�M5$�IJ�I*$ U�V* $)��*�+,� I*$ U�V* $)��*�+,�
� ��2�$%G)�2� 0P % ���IJ�I*J��+1M)K3 ��* =#�IJ�L 052)/ �0*Q.��.0�).�$ Q�LQ.�/MR*P % ���IJ�I*$ / �0*�M5$�K+ W
� Q�L�3 *�*�!�U+�/1!X 51LK)JL�0I��ST�/ �0*�M5$� Y�+.Z $L!*���K)/ �0*�M5$�!!$� $ IJ����./��0��3 I��ST�/ �0*
121002 Introduction to Computer Programming6
$L!*���K)/ �0*�M5$�!!$� $ IJ����./��0��3 I��ST�/ �0*�M5$�Q�L1�!QM$��2$����O2�$%GX0�L0 $���P % �M5$� (/JL* C �6! Java) �����I��IJ� 52)� � ]K.KL!!0TL$��$)��*�+,����IJ�5$�MNO� K+ W
3.3 ���� ������ (Control structure)
� �M5$���$�M5$�� � ]/ �0* #>*Q.��.0IJ����� � �3 $ ������/U�0 3 J*+./�L *�>* �6! � �3 $ ������/U�0 3 J*+./�L *�>* �6!
� ���� 1 �23 .�� (Sequence structure)
� ���� � /26!$ (Selection structure)
� ���� $ �3 =>3 (Repetition structure)
121002 Introduction to Computer Programming7
3.4 ���� 1 �23 .�� (Sequence structure)
� /MR*���� !�3 �������!�U+�/1!XK)�3 *QM1 �23 .�����/ �0*Q�� ]� Q�LQ.�/M2��0* �>*1!*$ �3 *�6!1 �23 .�����/ �0*Q�� ]� Q�LQ.�/M2��0* �>*1!*$ �3 *�6!��/6�!*Q /U+��/1+�
121002 Introduction to Computer Programming8
1��!0L Flowchart !���� 1 �23 .�� !P % =�
121002 Introduction to Computer Programming9
3.5 ���� 5��� /26!$ if
� ���� 5��� /26!$ if �6! ���� �3 ���IJ�/26!$�3 �3 ��� (�6!$2�L��3 ���) �.0! "�0$ 1�K�!�/6�!*Q =#��3 ��� (�6!$2�L��3 ���) �.0! "�0$ 1�K�!�/6�!*Q =#��� 3 TM5���6!� 1. �*#�� /26!$ (One Alternative)
� 2. �!� /26!$ (Two Alternative)
� 3. �2 0� /26!$ (Multiple-Alternative)
121002 Introduction to Computer Programming10
� 3. �2 0� /26!$ (Multiple-Alternative)
3.5.1 ���� 5��� /26!$ if )*+,-� /26!$
� ���� 5��*�> K)/26!$�3 �3 ��� (�6!$2�L��3 ���) $_1L!/�6�! 1�K�!�/6�!*Q 52��/MR*K+1�K�!�/6�!*Q 52��/MR*K+
� 1��!0L Flowchart !���� 5��� /26!$ if )*+,- � /26!$
��������condition
TRUEFALSE
121002 Introduction to Computer Programming11
condition
�����������������������statement T
TM5�� !���� 5��� /26!$ if )*+,-� /26!$
TM5��$ �3 1 �3 ��� :
if (condition) if (condition) Statement T;
TM5��$ �3 $2�L��3 ��� :if (condition){
121002 Introduction to Computer Programming12
if (condition){ True task;}
1��!0L !���� 5��� /26!$ if )*+,- � /26!$
1��!0L 1 : if (x > 0)
x > 0
total = total * x total = total * x;
1��!0L 2 : if (x > y) {
temp = x;x = y;
total = total * x
x > y
temp = x
121002 Introduction to Computer Programming13
temp = x;x = y;y = temp;
}
temp = xx = yy = temp;
1��!0L if )*+,- � /26!$ 1��!0L 1 : if (x > 0)
#include <iostream.h>#include <conio.h>int main(){
float x, total;cout << "input x : ";cin >> x;cout << "\n"; if (x > 0)
total = total * x;cout << "\n";cout << "input total : ";cin >> total;cout << "\n";
if (x > 0){
total = total *x;cout << "x= " << x << " total= " << total;
121002 Introduction to Computer Programming14
cout << "x= " << x << " total= " << total;} getch();return 0;
}
1��!0L if )*+,- � /26!$ 1��!0L 2 :
if (x > y) {
#include <iostream.h>#include <conio.h>int main(){
float x, y, temp;cout << "input x : ";cin >> x;cout << "\n"; {
temp = x;x = y;y = temp;
}
cout << "\n";cout << "input y : ";cin >> y;cout << "\n";
if (x > y){
temp = x;x = y;
121002 Introduction to Computer Programming15
x = y;y = temp;cout << "x= " << x << " y= " << y;
} getch();return 0;
}
3.5.2 ���� 5��� /26!$ if ./- � /26!$
� ���� 5��*�>���!� /26!$ $2L ��6! /26!$�3 �3 ��� (�6!$2�L��3 ���) /�6�!1�K�!�/6�!*Q 52��/MR*K+ �6! �3 �3 ��� (�6!$2�L��3 ���) /�6�! 1�K�!�/6�!*Q 52��/MR*/�_K $2�L��3 ���) /�6�! 1�K�!�/6�!*Q 52��/MR*/�_K
� 1��!0L Flowchart !���� 5��� /26!$ if ./- � /26!$
��������condition
TRUEFALSE
121002 Introduction to Computer Programming16
�����������������������statement T
�����������������������statement F
TM5�� !���� 5��� /26!$ if ./-� /26!$
TM5��$ �3 1 �3 ��� :if (condition){{
statement T ;} else {
statement F ; }
TM5��$ �3 $2�L��3 ��� : if (condition)
121002 Introduction to Computer Programming17
if (condition)
{ true task;
} else {false task;
}
1��!0L !���� 5��� /26!$ if ./- � /26!$ 1��!0L 1 : if (x >= 0)
cout << "positive\nZ;elseelse
cout << Ynegative\nZ;
1��!0L 2 : if (x>y) { temp = x ; x = y ; y = temp ;
121002 Introduction to Computer Programming18
x = y ; y = temp ;} else {
cout << Y x smaller y \nZ; }
#include <iostream.h>#include <conio.h>int main(){
float x;cout << "input x : ";cin >> x;
1��!0L 1 :
if (x >= 0)cout << "positive\nZ;
else cin >> x;cout << "\n";
if (x >= 0){
cout << " positive \n";} else {
cout << " negative \n";}
elsecout << Ynegative\nZ;
x >= 0
cout << "positive\n”
yes no
121002 Introduction to Computer Programming19
}getch();return 0;
}
cout << " negative\n”cout << "positive\n”
#include <iostream.h>#include <conio.h>int main(){
float x, y, temp;cout << "input x : ";cin >> x;cout << "\n";cout << "input y : ";
1��!0L 2 :
if (x>y) { temp = x ;
x = y ; cout << "input y : ";cin >> y;cout << "\n";
if (x > y){
temp = x;x = y;y = temp;
temp = x ;x = y ;y = temp ;
} else {cout << Yx smaller y \nZ;
}
121002 Introduction to Computer Programming20
cout << "x= " << x << " y= " << y;} else {
cout << "x smaller y \n"; }
getch();return 0;
}
3.5.3 ���� 5��� /26!$ if )012 � /26!$� ���� 5��*�>���2 0� /26!$ $2L ��6! I*$G����1�K�!�/6�!*Q
52�� �3 I��/$+.� /26!$ !$ �3 �3 ���� $$�L 2 � /26!$ � � ]IJ����� 5��� /26!$ if �2 0� /26!$�.0�3 $ �M5$�I��!0TLI*TM MultipleqAlternativeMultipleqAlternative
�������������� if
condition��� 1
���� ����
condition��� 2
�������������� else if �� �!
����
condition��� 3
�������������� else if ��#�
����
����
condition
����
���� ����
121002 Introduction to Computer Programming21
else if ��#�condition
��� n�������������� else if ��$%�&�'
�������������� else
���� ����
TM5�� !���� 5��� /26!$ if )012 � /26!$
TM5��: if (condition 1) {
statement 1; if (condition 1) {
statement 1;} else if (condition 2) {
statement 2;::
} else if (condition n) {
121002 Introduction to Computer Programming22
} else if (condition n) {statement n;
} else { statement;}
1��!0L !���� 5��� /26!$ if )012 � /26!$
Ex1. �+,&#�-.���.,/% 0 1�2���� ��1��3����.,����+�,&�����4�#5����6� '!���7�8�9� .&�:%&�., ;-�'< �.,������+�! .8�.,.+ 7�8�9�.8!����
!��������81<7=>1� 1. ������� ������ ������� � ��������2. ����� ����� !��"� ������#$�������%&���'��(� ����� !��"� ������#$�������%&���� ����� !��"� ������#$�������%&�����" 3. ��� %
;-�'< �.,������+�! .8�.,.+ 7�8�9�.8!����
121002 Introduction to Computer Programming23
3. ��� % num � ������� �count_0 � "� ���#$�������%&���'��(�
count_plus � "� ���#$�������%&�����" count_minus � "� ���#$�������%&����
1��!0L !���� 5��� /26!$ if )012 � /26!$ ,?��#�!�������� 1. "$����67�� 8��������"����� % �7�� � count_0 � "� ���#$�������%&���'��(� count_plus � "� ���#$�������%&�����" count_plus � "� ���#$�������%&�����" count_minus � "� ���#$�������%&���� 2. ��������%&������
num � ������� � 3. � �#!������� 9���%&�'��(� �����������'��(���8��67���:������ 9��;�7�<767��%&�'��(� ���� �#!���7��%&���������"� =�;�7
121002 Introduction to Computer Programming24
9��;�7�<767��%&�'��(� ���� �#!���7��%&���������"� =�;�7 9���<7 �������������"��8��67���:������
9��;�7�<7 ��������������8��67���:������4. !��������:� 3 67� 5. #�"� $����
1��!0L !���� 5��� /26!$ if )012 � /26!$
/*increment count_plus , count_minus an$ count_0 depending on x*/if (x > 0) {if (x > 0) {
count_plus = count_plus + 1;} else if (x < 0) {
count_minus = count_minus + 1;} else {
121002 Introduction to Computer Programming25
} else {
count_0 = count_0 + 1; }
1��!0L !���� 5��� /26!$ if )012 � /26!$ start
count_0 = 0, count_plus = 0,count_minus = 0
read num
1 !�
yescount_o=count_o+1 num > 0
yes no
no
count_plus=count_plus+1 count_minus=count_minus+1
read num
num = 0
121002 Introduction to Computer Programming26
print count_0, count_plus, count_minus
end
#include <iostream.h>#include <conio.h>int main(){
int count_0=0, count_plus=0,count_minus=0;float num;cout << "num : ";cin >> num;cout << "\n";cout << "\n";
if (num == 0){
count_0 = count_0+1;} else if (num > 0) {
count_plus = count_plus+1;} else {
count_minus = count_minus+1;}
121002 Introduction to Computer Programming27
cout << "count_0 = " << count_0 << "\n" ;cout << "count_plus = " << count_plus << "\n" ;cout << "count_minus = " << count_minus << "\n" ;
getch();return 0;
}
++LM!1% if �2 0 � /26!$ ���,�'�N���� .8O7� !�� %���P�!���+7�8���'� ��� +3�,��%�+7�8���'����#�'$,#�N-&��� %���? #�'$��!!�3� 10 7Q �+7�8�����?�.8 2 R&#�R� #�'$��!!�3� 3 7Q S2� 10 7Q �+7�8�����?�.8 1 R&#�R� #�'$��!!�3� 1 7Q S2� 3 7Q �+7�8�����?�.8 1/2 R&#�R� #�'$��!!�3� 1 7Q S2� 3 7Q �+7�8�����?�.8 1/2 R&#�R� �!�!�%S2� 1 7Q 1&���+7�8���
!��������81<7=>1� 1. ������� ������ ��(@ (age)2. ����� ���� ��% F��(������(@
��(@��""�7� 10 %G ��% F��6 �:�F 2 <���<�
121002 Introduction to Computer Programming28
��(@��""�7� 10 %G ��% F��6 �:�F 2 <���<� ��(@��""�7� 3 %G 9�� 10 %G ��% F��6 �:�F 1 <���<� ��(@��""�7� 1 %G 9�� 3 %G ��% F��6 �:�F 1/2 <���<� "�"8�9�� 1 %G ���� ��% F��
,?��#�!�������� (Flowchart)
start
yes no
read Age
Age > 10Age > 3 && Age <=10
yesEat = 2 Age > 3
yes no
no
Eat = 1
Age > 10
Age > 1 no
Eat = 0.5 Eat = 0
Age > 1 && Age <=3
121002 Introduction to Computer Programming29
print Eat
end
Source Code
121002 Introduction to Computer Programming30
1��!0L start
x <= 25
x=1, count_0 = 0, count_plus = 0,count_minus = 0
read num
endyes
no
�* Loop
yescount_o=count_o+1 num > 0
yes no
no
count_plus=count_plus+1 count_minus=count_minus+1
read num
num = 0
121002 Introduction to Computer Programming31
print count_0, count_plus, count_minus
x = x + 1
#include <iostream.h>#include <conio.h>int main(){
int x, count_0=0, count_plus=0,count_minus=0;float num;
for (x=1; x<=5; x++){ // start Loop for
cout << "num : ";cin >> num;
�* Loop For
cin >> num;cout << "\n";
if (num == 0){
count_0 = count_0+1;} else if (num > 0) {
count_plus = count_plus+1;} else {
count_minus = count_minus+1;}
121002 Introduction to Computer Programming32
} // end Loop for
cout << "count_0 = " << count_0 << "\n" ;cout << "count_plus = " << count_plus << "\n" ;cout << "count_minus = " << count_minus << "\n" ;
getch();return 0;
}
#include <iostream.h>#include <conio.h>int main(){
int x=1, count_0=0, count_plus=0,count_minus=0; // "$����67�� 8�����float num;
while (x <= 5) // ��=���;���"� �� ��{ // start Loop while
cout << "num : ";cin >> num;
�* Loop While
cin >> num;cout << "\n";
if (num == 0){
count_0 = count_0+1;} else if (num > 0) {
count_plus = count_plus+1;} else {
count_minus = count_minus+1;}
"� ��8��67����"����� %
121002 Introduction to Computer Programming33
x++; // "� ��8��67����"����� % }// end Loop while
cout << "count_0 = " << count_0 << "\n" ;cout << "count_plus = " << count_plus << "\n" ;cout << "count_minus = " << count_minus << "\n" ;
getch();return 0;
}
5��wx$��.1. ��������� !"�#$��%&'#��(%!)*%��+ ,- .�$+/�01234 1 �� 567&*8%&9:�;5��
'!8%&2+/�5<"�� !" *%��+ ,- .�5(=:;�� >"?� �@)234A$8A� �!(+"� !" �B%1. (1 !%D)
�8 �� �!�%
80 ,2?�:7 A
70 c 80 B
60 - 70 C
50 - 60 D
121002 Introduction to Computer Programming34
50 - 60 D
�&#'!�3� 50 F
1 !�start
yesgrade = 'A' score >= 70
yes no
no
read name, score
score >= 80
grade = 'A' score >= 70 no
grade = iB' yesscore >= 60 no
grade = iC' yes score >= 50 no
grade = iD' grade = iF'
121002 Introduction to Computer Programming35
print name, grade
end
#include <iostream.h>#include <conio.h>int main(){
float score; char grade;cout << "enter score : ";cin >> score;cout << "\n";
if (score >= 80){
grade = 'A';} else if (score >= 70 && score < 80) {
grade = 'B';} else if (score >= 60 && score < 70) {
grade = 'C';} else if (score >= 50 && score < 60) {
grade = 'D';
if (score >= 80){
grade = 'A';} else if (score >= 70) {
grade = 'B';} else if (score >= 60) {
grade = 'C';} else if (score >= 50) {
grade = 'D';
121002 Introduction to Computer Programming36
grade = 'D';} else {
grade = 'F';}
cout << "Grade is ==> " << grade << "\n "; getch();return 0;
}
grade = 'D';} else {
grade = 'F';}
read name, score
yes no
yesgrade = 'A' score >= 70 no
score >= 80
yesno score >= 0 && score <=100
yesgrade = 'A' score >= 70 no
grade = iB' yesscore >= 60 no
grade = iC' yes score >= 50 no
grade = iD' grade = iF'
121002 Introduction to Computer Programming37
print name, grade
endprint "** input valid ** G
int main(){
float score; char grade;cout << "enter score : ";cin >> score;cout << "\n";
if (score >= 0 && score <=100){
if (score >= 80) if (score >= 80) {
grade = 'A';} else if (score >= 70) {
grade = 'B';} else if (score >= 60) {
grade = 'C';} else if (score >= 50) {
grade = 'D';} else {
grade = 'F';}
121002 Introduction to Computer Programming38
}cout << "Grade is ==> " << grade << "\n ";
} else { cout << "** input valid ** \n";
}
getch();return 0;
}
5��wx$��.
2. ��������� !"�#$��%&'#��(%!)*%��+ ,- .�$+/�01234 1 ������ 10 �� �� 567&*8%&9:�;5�� '!8%&2+/�5<"�� !" *%��+ ,- .�5(=:;�� >"?� �@)234A$8A� �!(+"� !" �B%
�8 �� �!�%
80 ,2?�:7 A
70 c 80 B
60 - 70 C
50 - 60 D
121002 Introduction to Computer Programming39
�&#'!�3� 50 F
#include <iostream.h>#include <conio.h>int main(){
int x;float score;char grade; cout << " Report \n";cout << " ----------------------\n";
for (x=1; x<= 10; x++){
cout << "Enter score : ";cin >> score;cout << "\n";
if (score >= 0 && score <=100){
if (score >= 80){
grade = 'A';grade = 'A';} else if (score >= 70) {
grade = 'B';} else if (score >= 60) {
grade = 'C';} else if (score >= 50) {
grade = 'D';} else {
grade = 'F';}cout << "Grade is ==> " << grade << "\n\n ";
121002 Introduction to Computer Programming40
cout << "Grade is ==> " << grade << "\n\n "; } else {
cout << "** input valid **\n\n";}
} // loop for
cout << "----------------------\n"; getch();return 0;
}
#include <iostream.h>#include <conio.h>int main(){
int x=1;char grade;float score;
while (x <= 5){
cout << "enter score : ";cin >> score;cout << "\n";
if (score >= 0 && score <=100){
if (score >= 80){
grade = 'A';grade = 'A';} else if (score >= 70) {
grade = 'B';} else if (score >= 60) {
grade = 'C';} else if (score >= 50) {
grade = 'D';} else {
grade = 'F';}cout << "Grade is ==> " << grade << "\n\n ";
} else {
121002 Introduction to Computer Programming41
} else { cout << "** input valid **\n\n";
}x++;
} // loop while
cout << " ---------------------\n";getch();return 0;
}
3.6 ���� 5���2 0� /26!$ switch
� ���� 5���2 0� /26!$ switch K)]T$IJ��L!0I*$G� ! $ /26!$�*#�� /26!$I*�2 0W� /26!$ �.0�L ��� ! $ /26!$�*#�� /26!$I*�2 0W� /26!$ �.0�L ���IJ�1�K�!� K)1�!/MR*K3 *�*/1_� (int) �6! �!�T2J*+.1��!�$%(char) 51LK)1�!Q�L/MR*K3 *�*K+5��0 �(double) �.0! KK)!0TLI*TM !1��5M, *+UK*X �6!yN$XJ�*$_Q.�
121002 Introduction to Computer Programming42
yN$XJ�*$_Q.�
TM5�� !���� 5���2 0� /26!$ switch TM5��: switch (controlling expression)
{ case lable_set1:
statements1 case lable_set1:
statements1break;
case label_set2:statements2break; :
:
121002 Introduction to Computer Programming43
: case lable_setn:
statementsnbreak;
default : }
1��!0L : switch (ch)// $ 1�K�!�/6�!*Q ! switchK)*3 �L !1��5M���$3 �*.Q��I*�/2_��2� switch (int �6! char) QM1�K�!�$���L ����I*51L2)$G� {
#include <iostream.h>#include <conio.h>int main(){char ch;cout << "select ch ==> ";cin >> ch;
switch (ch) {case zB| :case zb| :cout << "Bat\nZ;break ;
case zC| :case zc| :cout << "Cat\nZ;
switch (ch) {
case 'B' :case 'b' :
cout << "Bat\n";break ;
case 'C' :case 'c' :
cout << "Cat\n";break ;
default :
121002 Introduction to Computer Programming44
cout << "Cat\nZ;break ;
default :cout << "Unknown clss \nZ << ch;
}
default :cout << "Unknown clss \n" << ch;
} getch();return 0;
}
5��wx$��.
3. ���*3?�>0!5 !&A�:+ .�;�&�9�:B% �!2����� "8�?���<+4� case "+��3/ ��I�"AI8 " 1 ������'B/�234<34�I:34?&�(J!+< ��I�"AI8 " 1 ������'B/�234<34�I:34?&�(J!+< " 2 ������'B/�234<�&�I:34?& " 3 %% �� !;DD �% �I�B%�� �3/ 5�8�*8%���& �! "I&�?�:*K#"':�"
121002 Introduction to Computer Programming45
�+/� )�X �>*1!*$ �3 *1. �+����1�!$ >0!5 !&�&�9�:B% �!������'B/�234<34�I:34?&KB�K8� 5:;'B/�234
<�&�I:34?&2. �!�T2*3 / � (+��:B% �&�9, ���& �8��, ���&?��, M��, <9�3. 1��5M 3. 1��5M
J6�!1��5M �� ��� 0 J*+. �!�T2
Choise (+��:B% �&�9 char
Ans 'B/�234 float
Width ���& �8�� float
Long ���&?�� float
121002 Introduction to Computer Programming46
4. <& �! 'B/�234<34�I:34?&KB�K8� = �8�� * ?�� 'B/�234<�&�I:34?& = 0.5 * M�� * <9�
Base M�� float
High <9� Float
�>*1!*$ �3 *1. �!#4&(8� �!2�����2. 5<"�<=���&�9�:B% ���3. 07%��=�(+��:B% �&�9 (Choise)4. �:B% �!2�����"8�?���<+4� switch (Choise)
4.1 \8� Choise �0]� ^1_ AI82�����(�& :J=&���<+4� "+��3/ - 07%��=� Width, Long - 07%��=� Width, Long
- �������=� Area = Width * Long%% a02�����*8% 5
4.2 \8� Choise �0]� ^2_ AI82�����(�& :J=&���<+4� "+��3/ - 07%��=� Base, high
- �������=� Area = Base * high%% a02�����*8% 5
4.3 \8� Choise �0]� ^3_ AI82�����(�& :J=&���<+4� "+��3/
121002 Introduction to Computer Programming47
4.3 \8� Choise �0]� ^3_ AI82�����(�& :J=&���<+4� "+��3/ - %% �� <=�� �!2�����%% a02�����*8% 5
4.4 �% �I�B%�� �3/ '#&')*8%���&5�8�*8%K#"':�" %% a02�����*8% 5
5. <#/�<J" �!2�����
switch
start
5��5<���-
7m#��3� Choise
Case 1 Case 2 Case 1 Case 4switch(Choise)
�����6 Area=Width * Long
Case 1 Case 2 Case 1 Case 4
7m#��3� width,long
�����6 Area=Base * high
7m#��3� Base,high
121002 Introduction to Computer Programming48
Width * Long
end
5��5< Area
Base * high
5��5< Area 5��5< Area
#include <iostream.h>#include <conio.h>int main(){float Ans, Width, Long, Base, high;int Choise;
cout << "select Choise ==> ";cin >> Choise; cin >> Choise; cout << "\n";
switch(Choise) {case 1 :
cout << "Input Width : ";cin >> Width;
cout << "\n";
cout << "Input Long : ";
121002 Introduction to Computer Programming49
cout << "Input Long : ";cin >> Long;cout << "\n";
Ans = Width * Long;cout << "Ans ==> " << Ans << "\n"; break ;
case 2 :
cout << "Input Base : ";cin >> Base;cout << "\n";
cout << "Input high : ";cin >> high;cin >> high;cout << "\n";
Ans = 0.5 * Base * high;cout << "Ans ==> " << Ans << "\n"; break ;
case 3 :exit (0);
default :
121002 Introduction to Computer Programming50
default :cout << "Unknown clss \n" << Choise;
}
getch();return 0;
}