Chapter 2. Functions and Control Statements

In Chapter 1, “Expressions and Variables,” you learned about C++ expressions and the most important operators used in expressions. In addition, you learned how to manipulate character strings, display values, and write to disk files. So far we have not been programming. Programming involves making decisions and repeating operations; the building blocks of programs are functions and control statements. A C++ program consists of one or more files that contain statements (which are its sentences) organized into functions (which are the equivalent of paragraphs). For instance, a program to calculate the average value of some numbers would have to repeatedly get a value, add it to the sum, until there were no more values. And finally it would divide that sum by the number of values and print out the result. If that result was a student's final aggregate mark, then the program may print out a message if that mark is below some value. I could give this description to a person, and they would be able to sit down with a calculator and process some test results. Computers are not intelligent enough to use English, which is in any case far too vague and wordy. So programming C++ involves precisely specifying each step (for example, what does 'no more values' mean?) so there is no room for misinterpretation.

In this chapter you will learn

  • How to define functions

  • How to execute statements conditionally

  • How to repeat statements

  • The difference between global and local variables

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.59.192