What is Program Control?
Program control is how a program makes decisions or organizes its activities.
Program control typically involves executing particular code based on the
outcome of a prior operation or a user input. Here is an example:
#include <iostream>
using namespace std;
// tell the user what to do
void tell()
{
cout << "Official Presidential Control Panel\n\n";
cout << "Choose:\n\n";
cout << "\t1. Start the coffee maker.\n";
cout << "\t2. Start World War III.\n";
cout << "\t3. Start the electric pencil sharpener.\n\n";
}
// accept the user's choice and return it
int choose()
{
// declare the result variable
int choice;
// do while the user entry is an error
do {
cout << "Enter (1,2,3):" << flush;
// accept user input, test for validity
// (if false, it's an entry error)
if(!(cin >> choice)) {
// flag the error
choice = -1;
// "repair" the input stream
cin.clear();
cin.ignore(1000,'\n');
}
// if true, test the acceptable input range
else if((choice < 1) || (choice > 3)) {
// flag the error
choice = -1;
}
// if there was an error
if(choice == -1) {
cout << "Entry Error. Type a number in the range 1-3.\n";
}
}
while(choice == -1);
return choice;
}
// act on the user's input
void act(int choice)
{
cout << "You chose " << choice << ", so I am ";
if(choice == 1) {
cout << "starting the coffee maker...\n";
}
else if(choice == 2) {
cout << "starting World War III...\n";
}
else if (choice == 3) {
cout << "starting the pencil sharpener...\n";
}
}
int main()
{
int choice;
tell();
choice = choose();
act(choice);
return 0;
}
Copy this program into your programming editor, compile it, and run it.
A brief review:
-
Each C++ program must have a function called main().
-
When the program is run, the function main() is executed.
-
The code in main() determines what your program does. No matter how much code
you have written, if main() does nothing, your program does nothing.
In the above program, there are three functions called by main():
-
tell() tells the user about his choices.
-
choose() accepts the user's choice, entered at the keyboard.
-
act() takes an action, depending on the user's choice.
In the function choose(), several decisions are made.
One decision is made based on the "signal" provided by the stream "cin":
if(!(cin >> choice)) {
If the input stream "cin" cannot acquire an integer (from the keyboard in this
case), it provides the boolean value "false" and the enclosing "if-test"
succeeds.
Why does this work?
Because the code "if(!(x))" means "if x is NOT true ...".
The exclamation point "!" inverts the meaning of a boolean value — true
becomes false, false becomes true.
Another decision is made based on the numerical value of "choice":
if((choice < 1) || (choice > 3)) {
This line of code means "If choice is less than 1 or greater than 3 ...". If
this is true, an error has been made, and the user must try again.
In the function act(), only one choice is made, but it has more than two
alternatives.
Basically, act() must choose between three alternatives, using a number to
control the choice.
The "if-else" method in the program above is not particularly efficient --
there are many others. As decision code gets more complicated, this "if-else"
structure becomes increasingly less efficient, and also less readable. Here's
an alternative:
void act(int choice)
{
cout << "You chose " << choice << ", so I am ";
switch(choice) {
case 1:
{
cout << "starting the coffee maker...\n";
break;
}
case 2:
{
cout << "starting World War III...\n";
break;
}
case 3:
{
cout << "starting the pencil sharpener...\n";
break;
}
}
}
If you wonder why each case-clause has a "break" — and if you wonder what
"break" does — why not put this version of "act()" in your program and try it
out? Then try removing the "break" statements, one by one.
The "break" statement causes program execution to "break out" of the nearest
enclosing braces.
A digression --
The student may think the listed program structure is excessively formal --
after
all, we could simply have thrown all the code into main() and avoided the
trouble of writing those three extra functions tell(), choose(), and act().
But a program structure like this is more than merely well-ordered and
aesthetically pleasing. In computer programming, structure has a deep purpose.
In this program the structure has several purposes:
-
The program shows its structure clearly in the program listing. Different
actions are placed in different modules. This helps the programmer understand
the program.
-
Because the functions are separate, each one can be modified or replaced very easily.
-
The order of the actions can be easily changed.
-
You may easily substitute one module for another, as in the above "act()"
example.
-
Because they are independent of each other and the program, each module may be
exported to
other programs and used there.