Vous êtes sur la page 1sur 8

ICS 103 Computer Programming in C

Lab# 4 Repetition Statements


Objectives:
Learn C repetition statements by covering the following topics:
1. while loops
. !o"while loops
#. for"loops
4. $este! loops
while"statement
% while"statement is &se! to e'ec&te a statement or a compo&n!"statement (ero or more times as
long as the while"con!ition is tr&e:
while(condition)
while-body
)he while"bo!y can be a simple statement in which case it m&st be terminate! by a semicolon or it
may be a compo&n!"statement in which case it sho&l! not be terminate! by a semicolon:

Simple statement while"bo!y Compo&n!"statement while"bo!y
while(condition)
statement;
while(condition)
compound-statement

*'amples:
while"loop o&tp&t
int n + 1,
while-n .+ 1/01
printf-23! 24 n0,
n 5+ ,
6
1 # 7 8 9
int : + 1,
while-: ; <0
printf-23! 24 :""0,
1 11 1/ 9 = 8
int n&m + ,
printf-2n&mber>ts?&are root>ts?&are>n20,
while-n&m .+ 701
printf-23!>t3.f>t3!>n24 n&m4 s?rt-n&m04 n&m @ n&m0,
n&m55,
6
n&mber s?&are root s?&are
1.41 4
# 1.8# 9
4 .// 1<
7 .4 7
Sentinel controlle! loops
An a program4 a sentinel is a val&e that mar:s the en! of a series of !ata val&es, b&t is not a !ata
val&e itself. Sentinels may be &se! to control loops:
*'ample: Brite a C program fragment that prompts for an! rea!s st&!ent gra!es in a ?&i(. At then
calc&lates an! !isplays the average. Cse a negative val&e as the sentinel.
int count = 0;
double grade, sumOfGrades = 0.0;
printf("nter grade!"d (-#e #alue to terminate)$n", count%&);
scanf(""lf", 'grade);
while(grade (= 0))
count%%;
sumOfGrades %= grade;
printf("nter grade!"d (-#e #alue to terminate)$n", count%&);
scanf(""lf", 'grade);
*
if(count == 0)
printf("rror+ ,o #alid grade entered$n");
else
printf("-#erage = "..f$n", sumOfGrades / count);
Exampe: Do!ify the above C program fragment s&ch that it also !isplays the ma'im&m an!
minim&m gra!e.
int count = 0;
double grade, sumOfGrades = 0.0;
printf("nter grade!"d (-#e #alue to terminate)$n", count%&);
scanf(""lf", 'grade);
double ma0 = grade; // -ssume the first grade is the ma0
double min = grade; // -ssume the first grade is the min
while(grade (= 0))
count%%;
sumOfGrades %= grade;
if(grade ( ma0)
ma0 = grade;
else if(grade 1 min)
min = grade;
printf("nter grade!"d (-#e #alue to terminate)$n", count%&);
scanf(""lf", 'grade);
*
if(count == 0)
printf("rror+ ,o #alid grade entered$n");
else)
printf("-#erage = "..f$n", sumOfGrades / count);
printf("2a0imum = "..f$n", ma0);
printf("2inimum = "..f$n", min);
*
!o"while statement
% !o"while statement is &se! to e'ec&te a statement or a compo&n!"statement one or more times
as long as the !o"while con!ition is tr&e:
do
do-while body
while(condition);

)he !o"while bo!y can be a simple statement in which case it m&st be terminate! by a semicolon
or it may be a compo&n!"statement in which case it DCS) $O) be terminate! by a semicolon:

Simple statement !o"while bo!y Compo&n!"statement !o"while bo!y
do
statement;
while(condition);
do
compound3statement
while(condition);

*'amples:
!o"while loop o&tp&t
int n = &;
do)
printf(""d ", n);
n %= .;
* while(n 1= &0);
1 # 7 8 9
int 0 = .4;
do
printf(""d ", 0 -= 4);
while(0 ( 0);
/ 17 1/ 7 /
Example: Brite a C program fragment that helps a chil! learn m&ltiplication. )he fragment
!isplays two integers to be m&ltiplie!. )he chil! is then given three chances to provi!e the correct
answer.
int num& = 5, num. = 6, response, count = 0;
printf("7hat is "d 8 "d 9$n", num&, num.);
do)
scanf(""d", 'response);
count%%;

if((response := num& 8 num.) '' (count 1 ;))
printf("7rong response. <lease tr= again$n");


* while((response := num& 8 num.) '' (count 1 ; ));
if(response := num& 8 num.)
printf(">orr= : ?ou were not successful$n");
else
#
printf("@ongratulations : ?ou got the correct answer$n");
Example: % !o"while loop can be &se! to vali!ate inp&t
int n;
do)
printf ("nter an integer number in A&0,&00B inter#al (");
scanf(""d",'n);
if(n1&0 CC n(&00)
printf(">orr= wrong input, tr= again$n");
*while (n 1 &0 CC n ( &00);
printf(",ow =our input is correct");
% !o"while loop can be &se! in a men& !riven program. )he e'ample shown below will contin&e
r&nning as long as the &ser !i! not enter the n&mber 7.
!include 1stdio.h(
#oid menu(#oid); // function protot=pe
int main(#oid))
int choice;
do)
menu();
printf("nter =our choice (");
scanf(""d",'choice);
// Dere come the statements to do the different tasEs
* while (choice := 4);
return 0;
*
#oid menu(#oid) )
printf("&--ddition$n");
printf(".->ubtraction$n");
printf(";-2ultiplication$n");
printf("F-Gi#ision$n");
printf("4-0it$n");
*
for"statement
% for"statement is &s&ally &se! to repeat a statement or a compo&n!"statement if the n&mber of
repetitions is :nown. )he bo!y of a for"loop may be e'ec&te! (ero or more times.
or(initiali!ation; condition; update)
or-body
)he for"bo!y can be a simple statement in which case it m&st be terminate! by a semicolon4 or it
may be a compo&n!"statement in which case it sho&l! not be terminate! by a semicolon:
4
Simple statement for"bo!y Compo&n!"statement for"bo!y
for(initialiHation; condition; update)
statement;
for(initialiHation; condition;
update)
compound3statement

*'amples:
for"loop o&tp&t
int E;
for(E = 5; E 1= &.; E%%)
printf(""d ", E);
8 = 9 1/ 11 1
int 0 ;
for(0 = I; 0 ( .; 0--)
if( 0 " . == 0)
printf(""d is e#en$n", 0);
else
printf(""d is odd$n", 0);
= is even
8 is o!!
< is even
7 is o!!
4 is even
# is o!!
int counter = &;
int factorial = &;
for(counter = &; counter 1= 4; counter%%)
factorial = factorial 8 counter;
printf("factorial of 4 is "d", factorial);
factorial of 7 is 1/
// prints 6 characters per line
char ch;
for (ch=J-J; ch1=JKJ; ch%%))
printf(""c$t",ch);
if((ch-J-J%&)"6==0)
printf("$n");
*
% E C F * G
H I A J K
L
D $ O L M
R
S ) C N B O
P Q
Loops li:e the ones in the above e'amples in which a variable is &se! to control the n&mber of
repetitions of the loop are calle! counting loops.
7
*?&ivalent loops
% loop can always be converte! to an e?&ivalent loop of a !ifferent type. *'amples:
loop e?&ivalent loop
for(initialiHation;condition; update)
)
statement&;
statement.;
. . .
statement,;
*
initialiHation;
while(condition)
)
statement&;
statement.;
. . .
statement,;
update;
*
do)
statement&;
statement.;
. . .
statement,;
update;
* while(condition);
statement&;
statement.;
. . .
statement,;
update;
while(condition))
statement&;
statement.;
. . .
statement,;
update;
*
Gor e'ample the following !o"while loop:
int n = &;
double 0 = 0, s;
do)
s = &.0 / (n 8 n);
0 = 0 % s;
printf("0 = "f$n", 0);
n%%;
* while(s ( 0.0&);

is e?&ivalent to the while"loop:
int n = &;
double 0 = 0, s;
s = &.0 / (n 8 n);
0 = 0 % s;
printf("0 = "f$n", 0);
n%%;
while(s ( 0.0&))
s = &.0 / (n 8 n);
0 = 0 % s;
printf("0 = "f$n", 0);
n%%;
*
<
$este! loops
% loop statement may contain in its bo!y one or more loop statements.
*'amples:
$este! loops o&tp&t
int m, n;
for(m = 4; m (= &; m--))
printf("m is now "d$n", m);
for(n = &; n 1= F; n%%)
printf("n = "d ", n);
*
m is now 7
n + 1 n + n + # n+ 4
m is now 4
n + 1 n + n + # n+ 4
m is now #
n + 1 n + n + # n+ 4
m is now
n + 1 n + n + # n+ 4
m is now 1
n + 1 n + n + # n+ 4
int sum,i,L;
for(i=&;i1=&0;i%%))
sum = &;
printf("&");
for(L=.; L1=i; L%%))
sum = sum % L;
printf("%"d",L);
*
printf("="d$n",sum);
*
Example: Brite a C program that prompts for an! rea!s three ?&i( gra!es for each st&!ent in a
class of fo&r st&!ents. )he program then comp&tes an! !isplays the average for each st&!ent.
Po&r program m&st be easily mo!ifiable to han!le any n&mber of st&!ents an! ?&i((es.
!include 1stdio.h(
!include 1stdlib.h(
!define ,M2>NMG,N> F
!define ,M2OMPK> ;
int main(#oid))
double grade, studentNotal, student-#erage;
int m, n;

for(m = &; m 1= ,M2>NMG,N>; m%%)
)
studentNotal = 0.0;
for(n = &; n 1= ,M2OMPK>; n%%)
)
printf("nter OuiHGrade!"d for student!"d$n", n, m);
scanf(""lf", 'grade);
studentNotal %= grade;
*
student-#erage = studentNotal / ,M2OMPK>;
printf("Nhe a#erage for student!"d is "..f$n", student-#erage);

*

s=stem("pause");
return 0;
*
Exercise: Do!ify the above program s&ch that it also comp&tes an! !isplays the class average.
8
Laboratory )as:s
Task 1: Csing sentinel controlle! loop -while or for loop04 write a program that rea!s a ra!i&s from
the &ser an! !isplays the area an! circ&mference of the circle with that ra!i&s. )his tas: is repeate!
&ntil the &ser types /. %ss&me that the ra!i&s is in centimeters. Cse a name! constant LA with val&e
#.149
Sample r&n:
Task 2: Csing a while loop4 write a C program to !isplay a positive integer n&mber type! by the
&ser in reverse.
Sample r&n:
Iint: Cse remain!er an! !ivision by 1/.
Task 3: Csing nested for loops4 write a C program to print the pattern shown in the sample r&ns
below on the screen.
Sample r&ns:
Task 4: Csing a do-while loop4 write a C program to chec: that the &ser has type! a letter. )he
program m&st not stop &ntil the &ser types a lowercase or &ppercase letter.
Note: Cse getchar( or fflush(stdin to s:ip the new line character after rea!ing each character.
Sample r&ns:
=

Vous aimerez peut-être aussi