Before writing a program, it is a good practice to break the program down to pieces that can be thought independently. Once the program has been completed, we can think of its execution as being a series of these pieces that work together in a certain sequence. These pieces then pass the control of the program between each other. While one piece has the control, the other pieces are inactive. This is known as the flow of control in a program.
Control statements are ways for a programmer to control what pieces of the program are to be executed at certain times. The syntax of control statements are very similar to regular english, and are very similar to choices that we make every day. There are two basic types of control statements: branching statements and loops.
In a program, depending on certain conditions, we want a certain series
of events to be executed. In this section, we will learn about if
conditions. (Another type of branching statements are switch
statements. We will learn about them in a later section).
Consider the following pseudocode:
if
student's grade is greater than or equal to 60
Print
"Passed"
This pseudocode statement determines if the condition "student's
grade is greater that or equal to 60" is true. If true, then
"Passed" is printed, else the print statement is ignored.
Then, the next statement after the if statement will be performed. The
above pseudocode can be easily be translated to C++ code as follows:
if
(grade >= 60)
cout
<< "Passed" << endl;
We can specify a different action to be performed if the condition
fails. For example,
if
student's grade is greater than or equal to 60
Print
"Passed"
else
Print
"Failed"
The corresponding C++ code will be the following:
if
(grade >= 60)
cout
<< "Passed" << endl;
else
cout
<< "Failed" << endl;
Sometimes, we may want a series of statements to be executed repeatedly
until some condition holds true. In this section, we will learn about
three kinds of looping statements: while, do-while
and for loops. We start with while loops and then
go on to show this can be converted to do-while and for
loops.
While loops: Consider the following example:
Set
total_price to zero
Set
item_counter to one
While
item_counter is less than or equal to 10
Input
item_price
Add
item_price to total_price
Add
one to the item_counter
Set
average_price to total_price divided by 10
Print
average_price
This pseudocode takes as input the price of 10 items and then computes
the average_price. The condition in the while statement
"item_counter is less than or equal to 10" makes the
following three statements to be executed 10 times until the condition
is satisfied. After the condition is satisfied (when we have inputted
the item prices 10 times), we can then compute the average price. The
C++ code looks very similar to this pseudocode:
int
total_price = 0;
int
item_counter = 1;
int
average_price;
while
(item_counter <= 10) {
cout
<< "Enter item_price: ";
cin
>> item_price;
total_price
= total_price + item_price;
item_counter
= item_counter + 1;
}
average_price
= total_price/10;
cout
<< "Average price of item = " << average_price
<< endl;
Follow this link to find an example program
with while statement from the textbook "C++ How to Program".
do-while loops: These are similar to the while loops except that
the condition is specified after the statements have been executed
once. Unlike while loops, do-while loops are guaranteed to execute the
statements once even if the condition fails. Compare the following code
(the loop portion only) to the code above:
do
{
cout
<< "Enter item_price: ";
cin
>> item_price;
total_price
= total_price + item_price;
item_counter
= item_counter + 1;
}
while (item_counter < 10);
Note that we had to change the "<=" in the while loop to
"<". Can you explain why?
Follow this link
to find an example program with do-while statement from the
textbook.
for loops: These are just another implementation of looping
statements. The following code does exactly the same as the codes given
above:
for
(item_counter = 1; item_counter <=10; item_counter++) {
cout
<< "Enter item_price: ";
cin
>> item_price;
total_price
= total_price + item_price;
}
Note that here we directly increment the item_counter in the for
statement.
Follow this link
to find an example program with for loop from the textbook.
Functions allow to modularize a program. The program can be divided into many parts (where each part does a specific set of tasks). Each part can then be implemented as a function. This approach makes the program development easy and easy to manage. Also, the parts that are designed to do specific work, can be specialized so that they can be used in more than one program. This is commonly known as software reusability.
The function definition comprises the sequence of statements
that will be executed each time the function is invoked. The general
format of a function definition is the following:
return-value-type
function-name (parameter-list with their types)
{
local
variable declarations
statements
return
result
}
The most common function that everybody uses is the main function.
Each C++ program requires a main function which is called by the system
on execution of the program. Each function has to declare its own local
variables and contains a set of statements that do a specific task (see example or a program
from the book about functions).
A function prototype tells the compiler the name of the function, the type of data returned and the number and types of parameters that the function requires. It is usually specified at the beginning of a C++ program.
Write a program that takes as input 10 numbers from the user and for each number outputs its factorial. To get you started you can download this skeleton program and modify it.