Wednesday, March 22, 2017

C++ Part 40

Review Part III
Naming Constants with the const Modifier
When you initialize a variable inside a declaration, you can mark the
variable so that the program is not allowed to change its value. To do this
place the word const in front of the declaration, as described below:
SYNTAX const Type_Name Variable_Name = Constant;
EXAMPLES const int MAX_TRIES = 3;
const double PI = 3.14159;

Boolean (bool) values are true and false
In C++, a Boolean expression evaluates to the bool value true when it is
satisfied and to the bool value false when it is not satisfied.
The Type bool Is New
Older versions of C++ have no type bool, but instead use the integers 1 and
0 for true and false. If you have an older version of C++ that does not
have the type bool, you should obtain a new compiler.


Multiway if-else Statement
SYNTAX
if (Boolean_Expression_1)
Statement_1
else if (Boolean_Expression_2)
Statement_2
.
.
.
else if (Boolean_Expression_n)
Statement_n
else
Statement_For_All_Other_Possibilities
EXAMPLE
if ((temperature < −10) && (day == SUNDAY)) cout << "Stay home."; else if (temperature < −10) //and day != SUNDAY cout << "Stay home, but call work."; else if (temperature <= 0) //and temperature >= −10
cout << "Dress warm."; else //temperature > 0
cout << "Work hard and play hard."; The Boolean expressions are checked in order until the first true Boolean expression is encountered, and then the corresponding statement is executed. If none of the Boolean expressions is true, then the Statement_For_All_Other_Possibilities is executed.

Blocks
A block is some C++ code enclosed in braces. The variables declared in a
block are local to the block and so the variable names can be used outside
of the block for something else (such as being reused as the name for a
different variable).

Scope Rule for Nested Blocks
If an identifier is declared as a variable in each of two blocks, one within
the other, then these are two different variables with the same name. One
variable exists only within the inner block and cannot be accessed outside
of the inner block. The other variable exists only in the outer block and
cannot be accessed in the inner block. The two variables are distinct, so
changes made to one of these variables will have no effect on the other of
these two variables.


The break Statement
The break statement can be used to exit a loop statement. When the
break statement is executed, the loop statement ends immediately and
execution continues with the statement following the loop statement. The
break statement may be used in any form of loop, in a while loop, in a dowhile
loop, or in a for loop. This is the same break statement that we
have already used in switch statements.

Repeat “This Many Times”
A for statement can be used to produce a loop that repeats the loop body
a predetermined number of times.
Pseudocode
Repeat the following This_Many times:
Loop_Body
Equivalent for Statement
for (int count = 1; count <= This_Many; count++) Loop_Body Example for (int count = 1; count <= 3; count++) cout << "Hip, Hip, Hurray\n";

Testing a Loop
Every loop should be tested with inputs that cause each of the following
loop behaviors (or as many as are possible): zero iterations of the loop
body, one iteration of the loop body, the maximum number of iterations
of the loop body, and one less than the maximum number of iterations of
the loop body. (This is only a minimal set of test situations. You should also
conduct other tests that are particular to the loop you are testing.)

Debugging a Very Bad Program
If your program is very bad, do not try to debug it. Instead, throw it out
and start over.

Post a Comment