CS201 Introduction to Programming GDB Solution Feb 2015

GDB Scenario: Every programming language provides different features to make coding task easy. One of the main objectives is to keep things simple as possible. You have studied C++ in this course and have learnt many different concepts e.g. input/output, if-else statements, loops, arrays, functions etc.

Looping constructs are needed when a statement or a block of statement is repeatedly executed until a specific condition holds true. You have learned in this course that C++ provides three looping constructs; For Loop, While Loop and Do-while Loop.

For C++ designers, it is not easy to include a construct or structure while developing this computer language. On the other hand, it also increases the complexity for learners as languages with more construct are difficult to learn.

A language expert Mr. ABC objects the designers of C++ on the bases of following two arguments:

  • A single looping construct is sufficient to cover all types of situations.
  • Designers of C++ have not done any good with themselves and C++ programmers by making things complex with provision of three different looping constructs.

GDB Question: Your Task is to comment on the views of Mr. ABC.  Clearly state your position in favour or against for each argument of Mr. ABC with justified reasons.

Solution: The purpose of a high level language is to make it easy for a person to tell a computer what to do. Although a single looping construct would be sufficient for the computer, C++’s 3 constructs make it easier on the person by letting them express their intent. This is good when writing code, but even more important when maintaining it.

The do-while loop tells us that the person wants the loop to execute at least once. The while loop tells us that it might be skipped altogether. The “for” loop lets us isolate the looping logic from the code that executes during each iteration.

I use the “for” loop most often, and not just for simple counter loops. Most loops really do have some sort of setup logic, a loop test, and a step to move you to the next iteration. If a loops has at least 2 of these 3, then I use the for construct.

Mr. ABC for example would prefer only one loop type. and that is for loop.