chapter4_2

Page 1

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

1. ( . /0 "# 0 + + 1/ 2. (+ 0 +$ # ,# 3" 3. (+ 4 ! 4 ( "# 0 + + 1/

1

121002 Introduction to Computer Programming


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

condition "* +, - "

.-

end

! " "3 4 Lop for 0 * 1 loop 2

121002 Introduction to Computer Programming


' "# for ' : for (initialization expression; loop repetition condition; update expression) ( . /0

,# 3"

F. / ( /4 (

3

121002 Introduction to Computer Programming


0 +#!( "# for 0 +#!( : /*Display N asterisks*/ for (count = 0; count < 10; count += 1) cout << "*"; ,#:

#include <iostream.h> #include <conio.h> int main() { int count; for(count = 0; count < 10; count+=1) { cout << "*"; } getch(); return 0; }

for (count = 0; count <10; count += 1) cout << "*";

R44 FS): ********** 121002 Introduction to Computer Programming

4


0 +#!( "# for 0 +#!( : /*Display Round No.*/ for (i = 1; i <5; i += 1) cout << VRound : V << i << endl; #include <iostream.h> R44 FS): #include <conio.h> int main() Round : 1 { int i; Round : 2 for(i = 1; i < 5; i+=1) { Round : 3 cout << "Round : " << i << "\n"; } Round : 4 getch(); return 0; } 5

121002 Introduction to Computer Programming


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> 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"; } getch(); return 0; } 6

121002 Introduction to Computer Programming


2. !13B( '! 1 )*+ 10 (1 + 2 + 3 + ... + 10 = ?) / >?2@A3.&-+ ' 50 for #include <iostream.h> #include <conio.h> int main() { 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"; }

}

getch(); return 0;

7

121002 Introduction to Computer Programming


3. !13B( '! 1 )*+ @E3F -5# "G% 23H / >?2@A3.&-+ ' 50 for start Input num Sum = 0, i = 1;

i <= num yes Sum = Sum + i

no

Print i, sum end

i++

8

121002 Introduction to Computer Programming


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

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

9

121002 Introduction to Computer Programming


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

condition "* +, - "

.-

- ! " while

! " "3 4 Loop while 1 1" "* +, loop 10

121002 Introduction to Computer Programming


' "# while ' : while (loop repetition condition) { statement; }

11

121002 Introduction to Computer Programming


0 +#!( "# while 0 +#!( : /*Display N asterisks.*/ count = 0; while (count < 10) { cout << "*i; count = count + 1; } R44 FS): **********

#include <iostream.h> #include <conio.h> int main() { int count; count = 0; while (count < 10) { cout << "*"; count = count + 1; }

}

getch(); return 0; 12

121002 Introduction to Computer Programming


0 +#!( "# while 0 +#!( /*Display Round No.*/ i = 1; while (i < 5) { cout << VRound : V << i << endl; i = i + 1; int main() } { int i; R44 FS): i = 1; while (i < 5) Round : 1 { Round : 2 cout << "Round : " << i << endl; i = i + 1; Round : 3 } Round : 4 getch(); return 0;

} 13

121002 Introduction to Computer Programming


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> int main() {

}

getch(); return 0; 14

121002 Introduction to Computer Programming


2. !13B( '! 1 )*+ 10 (1 + 2 + 3 + ... + 10 = ?) / >?2@A3.&-+ ' 50 while #include <iostream.h> #include <conio.h> int main() {

getch(); return 0; } 15

121002 Introduction to Computer Programming


3. !13B( '! 1 )*+ @E3F -5# "G% 23H / >?2@A3.&-+ ' 50 while start Input num Sum = 0, i = 1;

i <= num yes Sum = Sum + i

no

Print i, sum end

i++

16

121002 Introduction to Computer Programming


3.9 do/while

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

17

121002 Introduction to Computer Programming


! " do while do while /!#$ "# " $ 6 Loop 8 < /!#$ while $ ! "# /!#$ do while '/ $ # %; 8!$ "#%' $ /!#$A < loop % 8 1 ! ?$ << 8! '/ $ # %; " ! $ C $ # %; &$ $ /!#$ loop C $ # %; > /!#$ do while ! " ! " "3 4 Loop do while "* +, loop - "

condition "* +, . - ! " while 18

121002 Introduction to Computer Programming


' "# do/while ' : do statement; while (loop repetition condition);

19

121002 Introduction to Computer Programming


0 +#!( "# do/while 0 +#!( : /* Display Round No.*/ i=1; do { cout << VRound : V << i << endl; i = i + 1; int main() } { while ( i < 5); int i; R44 FS): Round : 1 Round : 2 Round : 3 Round : 4

}

i = 1; do { cout << "Round : " << i << "\n"; i = i + 1; } while (i <5); getch(); return 0; 20

121002 Introduction to Computer Programming


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

121002 Introduction to Computer Programming


3.10 break 4% continue

break !1 k ! # "# / '

break;

continue ! 4. ! 4,##!'( 41(/ k ! # "# / '

continue; 22

121002 Introduction to Computer Programming


/012356 78590:6 break; #include <iostream.h> #include <conio.h> int main() { int x; for(x = 1; x < 10; x+=1) { if (x == 5) { break; // >?22AB5ACDE F5 x == 5 } cout << x ;

}

} getch(); return 0;

R44 FS) 1234 121002 Introduction to Computer Programming

23


/012356 78590:6 break; #include <iostream.h> #include <conio.h> int main() { int x; for(x = 1; x < 10; x+=1) { if (x == 5) { continue; // GHI:2J0KLEJMAJH x==5 BNO8565K/2O0KOP LQ3RHO859M//GHK/ ?C06 continue } cout << x ; } getch(); return 0; }

R44 FS) 12346789

121002 Introduction to Computer Programming

24


3.11 Formulating Algorithm c d& l 1, ! 0 + 1/

% + +.S while ,# do/while

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

25

121002 Introduction to Computer Programming


0 +#!( c d& l 1, ! 0 + 1/ $ !) 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) R44 FS)## / " ! /$% F, # % " 3 4% & 26

121002 Introduction to Computer Programming


0 +#!( c d& l 1, ! 0 + 1/ /* Class average program with counter-controlled repetition */ #include <iostream.h> Enter grade: 98 int main() { Enter grade: 76 int counter, grade, total, average; Enter grade: 71 /* initialization phase */ Enter grade: 87 total = 0; counter = 1; Enter grade: 83 /* processing phase */ Enter grade: 90 while (counter <= 10) Enter grade: 57 { cout << "Enter grade: "; Enter grade: 79 cin >> grade; Enter grade: 82 total = total + grade; Enter grade: 94 counter = counter + 1; Class average is 81 } 27

121002 Introduction to Computer Programming


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

}

Enter grade: 98 /* termination phase */ Enter grade: 76 average = total / 10; Enter grade: 71 cout<<"Class average is " << average << endl; Enter grade: 87 Enter grade: 83 return 0; Enter grade: 90 /* indicate program ended successfully */ Enter grade: 57 Enter grade: 79 Enter grade: 82 Enter grade: 94 Class average is 81 28

121002 Introduction to Computer Programming


c d& l 2 3 FA3,6A3 / >?2@E3% '4% 3@G! % + +.S while ,# do/while 0($% 0 + 0 + & ! +( ! (sentinel value) F, # # +( " #/'4 " / e$ . 4 +

29

121002 Introduction to Computer Programming


0 +#!( c d& l 2, ! ( 0 +$0 1/ $ !) 0 # % r4 !"# d& l ! ( !+ c d& l 1 0(3/( $ + d& l

( # 4(+

% + +.S

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

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

121002 Introduction to Computer Programming


z !/0 +#!( c d& l 2 ' ( )* +,' total - (./ 0 ' ( )* +,' counter - (./ 0 % ( " 3 While R' ! 3/(3 |# ( 0 +$0 " 3 + % " total F. / ( "# counter "& & % ( 0(#3 ( & # $$% ( 0 +$0 e3 )

If counter 3/( ( 0 { average ( total +! counter F./F) average } Else F./F) VNo grades were enteredi (! 3/(/ |# % " / ) 31

121002 Introduction to Computer Programming


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 + */ 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) { total = total + grade; counter = counter + 1; cout << "Enter grade, -1 to end: "; cin >> grade; } 32

121002 Introduction to Computer Programming


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 */

33

121002 Introduction to Computer Programming


R44 FS) c d& l 2, ! ( 0 +$0 1/ Enter grade: 75 Enter grade: 94 Enter grade: 97 Enter grade: 88 Enter grade: 70 Enter grade: 64 Enter grade: 83 Enter grade: 89 Enter grade: -1 Class average is 8250 34

121002 Introduction to Computer Programming


c d& l 3 + 1/ # + 1/ R( / + 1/ / f / 0(# 3 , #! b 3

0( + 1/ # . $ % # + 1/ # " +!

35

121002 Introduction to Computer Programming


0 +#!( c d& l 3, @ +. 23+@'0@G! 00,25 $ !) 0 # " ! / F, # 1 R44 FS)R4 # "# # 1* 0 0 + $ ( ! . !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 - $ + 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 36

121002 Introduction to Computer Programming


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

( passes Else F./ &

( failures F./ & student counter F./F)$ + "# passes F./F)$ + "# failures If d& l / +( 8 # R( F./F) VGood Testi 37

121002 Introduction to Computer Programming


0 +#!( c d& l 3, @ +. 23+@'0@G! 00,25 /* Analysis of examination results */ #include <iostream.h> int main() { /* 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 */ passes = passes + 1; else failures = failures + 1; student = student + 1; }

121002 Introduction to Computer Programming

38


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

}

cout << VPassed V << passes; cout << "Failed " << failures); if (passes > 8) cout << "Raise tuition\n"; /* successfully termination */ return 0;

39

121002 Introduction to Computer Programming


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

121002 Introduction to Computer Programming


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.