Department of Business Computer Faculty of Business Administrator Ratchaphreuk College
4 (Control Structures) 1
121002 Introduction to Computer Programming
!"#$%
3.1 $ ) * +, (Algorithm) 3.2 ./ 0 (Pseudocode) 3.3 (Control structure) 3.4 1 23 . (Sequence structure) 3.5 5 /26!$ if 3.6 5 2 0 /26!$ switch
2
121002 Introduction to Computer Programming
!"#$%
3.8 3.7 3.9 3.10
5 $ 3 => 3 while 5 $ 3 => 3 for 5 $ 3 => 3 do/while 3 break 52) continue
3
121002 Introduction to Computer Programming
!"#$%
3.11 $ ) * +, $ (Formulating Algorithm)
$ G "#$% 1, $ 3 =>3 .0IJ 1 * (Case Study 1, Counter-Controlled Repetition)
3.12 $ ) * +, $ .0IJ +, $ /$2 K $ *2 2L
(Formulating Algorithm with Top-Down, Stepwise Refinement)
$ G "#$% 2, $ 3 =>3 .0IJ L 1 K1 (Case Study 2, Sentinel-Controlled Repetition) $ G "#$% 3, 5 = !* (Case Study 3, Nested Control Structure) 4
121002 Introduction to Computer Programming
3.1 $ ) * +, (Algorithm) $ ) * +, I* * > 6! $ ) *$ I*$ 5$ MNO .0K) M )$! . 0 >*1!* !
$ $ ) 3 23 . ! $ $ ) 3
5
121002 Introduction to Computer Programming
3.2 ./ 0 (Pseudocode) ./ 0 6! P % !0L Q L/MR* $ S T / 0* M 5$ IJ
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* M 5$ K + W Q L 3 * * ! U+ /1! X 51LK)JL 0I ST / 0* M 5$ Y +.Z $L!* K)/ 0* M 5$ !!$ $ IJ ./ 0 3 I ST / 0* M 5$ Q L1 ! QM$ 2$ O2 $%GX0 L 0 $ P % M 5$ (/JL* C 6! Java) I IJ 52) ]K.KL!!0TL$ $ ) * +, IJ 5$ MNO K + W 6
121002 Introduction to Computer Programming
3.3 (Control structure) M 5$ $ M 5$ ]/ 0* #>*Q. .0IJ
3 $ /U 0 3 J*+./ L * >* 6!
1 23 . (Sequence structure) /26!$ (Selection structure) $ 3 =>3 (Repetition structure)
7
121002 Introduction to Computer Programming
3.4 1 23 . (Sequence structure)
/MR* ! 3 ! U+ /1! XK) 3 *QM 1 23 . / 0*Q ] Q LQ. /M2 0* >*1!*$ 3 * 6! / 6 !*Q /U+ /1+
8
121002 Introduction to Computer Programming
1 !0L Flowchart ! 1 23 . ! P % =
9
121002 Introduction to Computer Programming
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 TM5 6!
1. *# /26!$ (One Alternative) 2. ! /26!$ (Two Alternative) 3. 2 0 /26!$ (Multiple-Alternative)
10
121002 Introduction to Computer Programming
3.5.1 5 /26!$ if )*+-, /26!$
5 * > K)/26!$ 3 3 ( 6!$2 L 3 ) $_1!L / 6! 1 K ! / 6 !*Q 52 /MR*K +
1 !0L Flowchart ! 5 /26!$ if )*+,- /26!$
FALSE
condition
TRUE
statement T
11
121002 Introduction to Computer Programming
TM5 ! 5 /26!$ if )*+,- /26!$ TM5 $ 3 1 3 :
if (condition) Statement T; TM5 $ 3 $2 L 3 :
if (condition) { True task; } 12
121002 Introduction to Computer Programming
1 !0L ! 5 /26!$ if )*+,- /26!$ 1 !0L 1 : if (x > 0) total = total * x; 1 !0L 2 : if (x > y) { temp = x; x = y; y = temp; }
x>0 total = total * x
x>y temp = x x=y y = temp; 13
121002 Introduction to Computer Programming
1 !0L
if )*+,- /26!$ 1 !0L 1 : if (x > 0) total = total * x;
#include <iostream.h> #include <conio.h> int main() { float x, total; cout << "input x : "; cin >> x; cout << "\n"; cout << "input total : "; cin >> total; cout << "\n"; if (x > 0) { total = total *x; cout << "x= " << x << " total= " << total; } getch(); return 0; }
14
121002 Introduction to Computer Programming
1 !0L
if )*+,- /26!$ 1 !0L 2 : if (x > y) { temp = x; x = y; y = temp; }
#include <iostream.h> #include <conio.h> int main() { float x, y, temp; cout << "input x : "; cin >> x; cout << "\n"; cout << "input y : "; cin >> y; cout << "\n"; if (x > y) { temp = x; x = y; y = temp; cout << "x= " << x << " } getch(); return 0;
y= " << y;
} 15
121002 Introduction to Computer Programming
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 1 !0L Flowchart ! 5 /26!$ if ./- /26!$
FALSE
statement F
condition
TRUE
statement T
16
121002 Introduction to Computer Programming
TM5 ! 5 /26!$ if ./- /26!$ TM5 $ 3 1 3 : if (condition) { statement T ; } else { statement F ; } TM5 $ 3 $2 L 3 : if (condition) { true task; } else { false task; } 17
121002 Introduction to Computer Programming
1 !0L ! 5 /26!$ if ./- /26!$ 1 !0L 1 :
if (x >= 0) cout << "positive\nZ; else cout << Ynegative\nZ;
1 !0L 2 :
if (x>y) {
} else { }
temp = x ; x=y; y = temp ; cout << Y x smaller y \nZ; 18
121002 Introduction to Computer Programming
1 !0L 1 : if (x >= 0) cout << "positive\nZ; else cout << Ynegative\nZ; yes
x >= 0
cout << "positive\nâ&#x20AC;?
#include <iostream.h> #include <conio.h> int main() { float x; cout << "input x : "; cin >> x; cout << "\n"; if (x >= 0) { cout << " positive \n"; } else { cout << " negative \n"; } getch(); return 0;
no
cout << " negative\nâ&#x20AC;?
} 19
121002 Introduction to Computer Programming
1 !0L 2 : if (x>y) { temp = x ; x=y; y = temp ; } else { cout << Yx smaller y \nZ; }
#include <iostream.h> #include <conio.h> int main() { float x, y, temp; cout << "input x : "; cin >> x; cout << "\n"; cout << "input y : "; cin >> y; cout << "\n"; if (x > y) { temp = x; x = y; y = temp; cout << "x= " << x << " y= " << y; } else { cout << "x smaller y \n"; } getch(); return 0; }
121002 Introduction to Computer Programming
20
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 $ M 5$ I !0TLI* TM MultipleqAlternative
condition 1
if
else if !
condition 2 else if #
condition 3 else if $% & '
condition n
else
21
121002 Introduction to Computer Programming
TM5 ! 5 /26!$ if )012 /26!$ TM5 : if (condition 1) { statement 1; } else if (condition 2) { statement 2; : : } else if (condition n) { statement n; } else { statement; } 22
121002 Introduction to Computer Programming
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. % num count_0 " #$ %& ' ( count_plus " #$ %& " count_minus " #$ %& 121002 Introduction to Computer Programming
23
1 !0L ! 5 /26!$ if )012 /26!$ , ? # ! 1. "$ 67 8 " % 7 count_0 " #$ %& ' ( count_plus " #$ %& " count_minus " #$ %& 2. %& num 3. #! 9 %& ' ( ' ( 8 67 : 9 ; 7 <767 %& ' ( #! 7 %& " = ; 7 9 <7 " 8 67 : 9 ; 7 <7 8 67 : 4. ! : 3 67 5. # " $ 121002 Introduction to Computer Programming
24
1 !0L ! 5 /26!$ if )012 /26!$ /*increment count_plus , count_minus an$ count_0 depending on x*/ if (x > 0) { count_plus = count_plus + 1; } else if (x < 0) { count_minus = count_minus + 1; } else { count_0 = count_0 + 1; } 25
121002 Introduction to Computer Programming
1 !0L ! 5 /26!$ if )012 /26!$ start
1 !
count_0 = 0, count_plus = 0,count_minus = 0 read num yes
num = 0 yes count_o=count_o+1
no num > 0
count_plus=count_plus+1
no
count_minus=count_minus+1
print count_0, count_plus, count_minus end 26
121002 Introduction to Computer Programming
#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"; if (num == 0) { count_0 = count_0+1; } else if (num > 0) { count_plus = count_plus+1; } else { count_minus = count_minus+1; } cout << "count_0 = " << count_0 << "\n" ; cout << "count_plus = " << count_plus << "\n" ; cout << "count_minus = " << count_minus << "\n" ; getch(); return 0; } 27
121002 Introduction to Computer Programming
++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 ! ! %S2 1 7Q 1& +7 8 ! 81<7=>1 1. (@ (age) 2. % F ( (@ (@ "" 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 28
121002 Introduction to Computer Programming
, ? # ! (Flowchart) start read Age yes Eat = 2
Age > 3 && Age <=10
no
Age > 10 yes
Age > 3
Eat = 1
no Age > 1 && Age <=3 Age > 1
Eat = 0.5
no Eat = 0
print Eat end 29
121002 Introduction to Computer Programming
Source Code
30
121002 Introduction to Computer Programming
1 !0L
start
* Loop
x=1, count_0 = 0, count_plus = 0,count_minus = 0 x <= 25
no
end
yes
read num yes
num = 0 yes count_o=count_o+1
no num > 0
count_plus=count_plus+1
no
count_minus=count_minus+1
print count_0, count_plus, count_minus x=x+1 31
121002 Introduction to Computer Programming
#include <iostream.h> #include <conio.h> int main() { int x, count_0=0, count_plus=0,count_minus=0; float num;
* Loop For
for (x=1; x<=5; x++) { // start Loop for cout << "num : "; 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; } } // end Loop for cout << "count_0 = " << count_0 << "\n" ; cout << "count_plus = " << count_plus << "\n" ; cout << "count_minus = " << count_minus << "\n" ; getch(); return 0; }
121002 Introduction to Computer Programming
32
#include <iostream.h> #include <conio.h> int main() { int x=1, count_0=0, count_plus=0,count_minus=0; // "$ 67 8 float num;
* Loop While
while (x <= 5) // = ; " { // start Loop while cout << "num : "; 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; } 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; }
121002 Introduction to Computer Programming
33
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
&#'! 3 50
F
34
121002 Introduction to Computer Programming
start
1 !
read name, score yes grade = 'A'
score >= 80 yes grade = iB'
no score >= 70 yes grade = iC'
no score >= 60 yes grade = iD'
no score >= 50
no grade = iF'
print name, grade end 35
121002 Introduction to Computer Programming
#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'; } else { grade = 'F'; }
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'; }
cout << "Grade is ==> " << grade << "\n "; getch(); return 0; } 121002 Introduction to Computer Programming
36
read name, score no
score >= 0 && score <=100
yes grade = 'A'
yes score >= 80 yes grade = iB'
no score >= 70 yes grade = iC'
no score >= 60 yes grade = iD'
no score >= 50
no grade = iF'
print name, grade print "** input valid ** G 121002 Introduction to Computer Programming
end
37
int main() { float score; char grade; cout << "enter score : "; cin >> score; cout << "\n"; if (score >= 0 && score <=100) { 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'; } cout << "Grade is ==> " << grade << "\n "; } else { cout << "** input valid ** \n"; } getch(); return 0; }
121002 Introduction to Computer Programming
38
5 wx$ . 2. !" #$ %&'# (%!)*% + ,- . $+/ 01234 1 10 567&*8%&9: ;5 '!8%&2+/ 5<" !" *% + ,- . 5(=:; >"? @)234A$8 A !(+" !" B% 8
! %
80 ,2? :7
A
70 c 80
B
60 - 70
C
50 - 60
D
&#'! 3 50
F
39
121002 Introduction to Computer Programming
#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'; } 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 { cout << "** input valid **\n\n"; } } // loop for cout << "----------------------\n"; getch(); return 0; }
121002 Introduction to Computer Programming
40
#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'; } 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 { cout << "** input valid **\n\n"; } x++; } // loop while cout << " ---------------------\n"; getch(); return 0;
121002 Introduction to Computer Programming
}
41
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 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.
42
121002 Introduction to Computer Programming
TM5 ! 5 2 0 /26!$ switch TM5 : switch (controlling expression) { case lable_set1: statements1 break; case label_set2: statements2 break; : : case lable_setn: statementsn break; default : } 121002 Introduction to Computer Programming
43
1 !0L : switch (ch) // $ 1 K ! / 6 !*Q ! switch K)*3 L ! 1 5M $3 *.Q I* /2_ 2 switch (int 6! char) QM1 K ! $ L I*51L2)$ G { case zB| : case zb| : cout << "Bat\nZ; break ; case zC| : case zc| : cout << "Cat\nZ; break ; default : cout << "Unknown clss \nZ << ch; }
#include <iostream.h> #include <conio.h> int main() { char ch; cout << "select ch ==> "; cin >> ch; switch (ch) { case 'B' : case 'b' : cout << "Bat\n"; break ; case 'C' : case 'c' : cout << "Cat\n"; break ; default : cout << "Unknown clss \n" << ch; } getch(); return 0; } 44
121002 Introduction to Computer Programming
5 wx$ . 3. *3? >0!5 !&A :+ . ; & 9 :B% !2 "8 ? <+4 case "+ 3/ I "AI8 " 1 'B/ 234<34 I:34?& (J!+< " 2 'B/ 234< & I:34?& " 3 %% !;DD % I B% 3/ 5 8 *8% & ! "I& ? :*K#"': "
45
121002 Introduction to Computer Programming
+/ ) 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 J6 !1 5M Choise
0 (+ :B% & 9
J*+. ! T2 char
Ans Width
'B/ 234 & 8
float float
Long Base
&? M
float float
High
<9
Float
4. <& ! 'B/ 234<34 I:34?&KB K8 = 8 * ? 'B/ 234< & I:34?& = 0.5 * M * <9 121002 Introduction to Computer Programming
46
>* 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 - = 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/ - %% <= !2 %% a02 *8% 5 4.4 % I B% 3/ '#&')*%8 &5 8 *8%K#"': " %% a02 *8% 5 5. <#/ <J" !2 121002 Introduction to Computer Programming
47
start 5 5< 7m# 3 Choise Case 1
Case 2
switch (Choise)
7m# 3 width,long
7m# 3 Base,high
6 Area= Width * Long
6 Area= Base * high
5 5< Area
5 5< Area end
121002 Introduction to Computer Programming
Case 1
Case 4
5 5< Area
48
#include <iostream.h> #include <conio.h> int main() { float Ans, Width, Long, Base, high; int Choise; cout << "select Choise ==> "; cin >> Choise; cout << "\n"; switch(Choise) { case 1 : cout << "Input Width : "; cin >> Width; cout << "\n"; cout << "Input Long : "; cin >> Long; cout << "\n"; Ans = Width * Long; cout << "Ans ==> " << Ans << "\n"; break ; 49
121002 Introduction to Computer Programming
case 2 : cout << "Input Base : "; cin >> Base; cout << "\n"; cout << "Input high : "; cin >> high; cout << "\n"; Ans = 0.5 * Base * high; cout << "Ans ==> " << Ans << "\n"; break ; case 3 : exit (0); default : cout << "Unknown clss \n" << Choise; } getch(); return 0; } 50
121002 Introduction to Computer Programming