Thursday, July 22, 2010

C++: Intermission 2...

For me, I struggled with for loop when I first learned it. This was way
different from what I used to do - doing a while loop. Not until last year
when I did an introductory course I came to the conclusion that for loop is
essentially an enhancement of if and while loop.

Another thing that I was confused about was the difference between a while
and a do-while loop. I knew that do-while was somewhat different from a
while loop. One day, I saw a post from a mailing list for blind programmers
which clarified the difference between these two. The post in question was
in response to a question from a beginning C++ student about the while and
do-while loop. According to the post, the while loop checks the condition at
the beginning to determine whether to run the while loop, whereas the
do-while requires checking conditions at the end i.e. a code that runs at
least once before continuing with another repetition.

Also, a general advice: if your program specs require that you run a part of
your program for a certain number of times, try picturing a for loop
(technically called iteration). There is a more advanced form called
recurssieve function calls where a function would call itself multiple times
until a base case is reached. It is perfectly fine to convert from iteration
to recurssive function calls and vice versa - but note that recursive
functions take up more storage space and takes longer to print out its
results (which is the perfect setting for me to go into hardware). We won't
go into recursive functions yet - both because it's so advanced and the fact
that I didn't really grasp the concept about it fully yet- this is one of
those that require your input...

Well, hope you are enjoying this series. On the next section, we'll take a
detour and investigate about hardware basics before continuing our journey
with arrays and vectors. If you have any comments, please let me know via
email, Blogger comment form or on my Facebook page...
// JL (UCR)

No comments:

Post a Comment