Unformatted text preview: h as s hown above, us ing the "for" loop res ults in a muc h more c onc is e
and c leaner c ode. Henc e, we s ugges t y ou us e the "for" loop (ins tead of the "while" loop) to c arry out s imple tas k s lik e this .
Below, we s how a muc h more appropriate us e of the while loop. A more appropriate use of the While loop
The c ode s hown below is us ed to ac c ept a s eries of c harac ters from a us er. W hen the us er hits return (enter on the k ey board), the
program s tops ac c epting c harac ters and dis play s how many c harac ters the us er has entered. In a real program y ou would s tore
thes e c harac ters in an array for further manipulation. W e will introduc e array s and operations on them in later ex erc is es .
wie( ! 'n)
hl c = \'
} } pit(\ Yuhv etrd% caatr\" cut1;
rnf"n o ae nee d hrcesn, on-)
eun ; The rest of this lab's assignment
Here y ou are given a program s tored in lab4.c file in whic h the iteration is done us ing a "for" loop. W he n re w rite the "for" loop into "w hile " loop, don't try to re a ssign "a " to som e othe r va lue s othe r tha n 256. #nld<ti.>
n; } fr( =26 a>1 a=sr()
rnf"dn, ) Your tas k is to c onvert the above program and us e the "while" looping c ons truc t ins tead of the "for" looping c ons truc t".
Edit this c ode in lab4.c to us e only the "while" loop and upload the modified C file to y our SVN repos itory .
Remember, the output of the s eries s hould be ex ac tly the s ame as the output of the program given us ing "for" loop. HINT: Try unrolling the "for" loop as s hown above. This will give y ou a c lear idea of what is happening and will help y ou
trans form y our c ode to u...
View Full Document
This document was uploaded on 03/22/2014.
- Fall '14