|
|
|
|
|
|
|
hypotenuse = sqrt(a*a + b*b); |
|
|
|
|
|
|
|
|
The purpose of indenting statements in a program is to provide visual cues to the reader and to make the program easier to debug. When a program is properly indented, the way the statements are grouped is immediately obvious. Compare the following two program fragments: |
|
|
|
|
|
|
|
|
while (count <= 10) while (count <= 10)
{ {
cin >> num; cin >> num;
if (num == 0) if (num == 0)
{ {
count++; count++;
num = 1; num = 1;
} }
cout << num << endl; cout << num << endl;
cout << count << endl; cout << count << endl;
} } |
|
|
|
|
|
|
|
|
As a basic rule in this text, each nested or lower-level item is indented by four spaces. Exceptions to this rule are formal parameters and statements that are split across two or more lines. Indenting by four spaces is a matter of personal preference. Some people prefer to indent by three, five, or even more than five spaces. |
|
|
|
|
|
|
|
|
In this book, we indent the entire body of a function. Also, in general, any statement that is part of another statement is indented. For example, the If-Then-Else contains two parts, the then-clause and the else-clause. The statements within both clauses are indented four spaces beyond the beginning of the If-Then-Else statement. The If-Then statement is indented like the If-Then-Else, except that there is no else-clause. Here are examples of the If-Then-Else and the If-Then: |
|
|
|
|
|
|
|
|
if (sex == MALE)
{
maleSalary = maleSalary + salary;
maleCount++;
}
else
femaleSalary = femaleSalary + salary;
if (count > 0)
average = total / count; |
|
|
|
|
|