|
|
|
|
bool |
|
break statement |
|
continue statement |
|
Verified Conditional Input |
|
|
|
|
|
|
Syntax: bool declarators; |
|
This keyword is an integral type. A variable of
this type can have values true and false. |
|
All conditional expressions now return a value
of type bool. |
|
For example, i!=0 now returns true or false
depending on the value of i. |
|
|
|
|
|
|
This keyword is one of the two values that a
variable of type bool can have. |
|
If i is of type bool, then the statement i=true;
assigns true to i. |
|
|
|
|
The keyword is one of the two values that a
variable of type bool can have. |
|
For example, if i is a variable of type bool,
the i=false; statement assigns false to i. |
|
|
|
|
|
|
|
The values true and false have the following
relationship: |
|
!false == true |
|
!true == false |
|
In the following statement: |
|
if
(expres1) statement1; |
|
If expres1
is true, statement1 is always executed; if expres1 is false, statement1 is
never executed. |
|
|
|
|
|
|
In for, while, and do-while loops, break exists
from the lowest-level loop in which it occurs. |
|
Used for early exist from loop, or for existing
a forever loop. |
|
|
|
|
for (int i = 1; i <= 10; i++) { |
|
int r = i % 3; |
|
if ( r == 0 ) { |
|
break; // jump out of for
statement |
|
} |
|
else { |
|
cout << " i = "
<< i; |
|
} |
|
} |
|
cout << “\n End of loop” <<
endl; |
|
|
|
|
|
|
|
|
|
The continue statement cause immediate loop
iteration, skipping the rest of the loop statements. |
|
Jumps to the loop test when used with while or
do-while |
|
Jump to loop update, then test when used with
used with for |
|
Does not exist the loop(unless next loop test is
false) |
|
|
|
|
|
|
|
|
for (int i = 1; i <= 10; i++) { |
|
int r = i % 3; |
|
if ( r == 0 ) { //skip every 3th
time |
|
continue; |
|
} |
|
else { |
|
cout << " i = "
<< i; |
|
} |
|
} |
|
cout << “\n End of loop” << endl; |
|
|
|
|
|
|
|
|
To ask the user to type in an integer and to
assign the user's answer to be the new value of variable x we use the
following statement: |
|
x = RequestInt("Next number:"); |
|
|
|
|
|
|
The only difference from the verified input is
that the program supplies a default value. The default value is displayed
after the prompt, to alert the user to its availability and to inform the
user of the default value. The statements have the format: |
|
x = RequestInt("Next number:", 3); |
|
|
|
|
|
|
|
In both previous cases, the user is required to
supply input and the program will not proceed further until satisfactory
data has been typed in. |
|
Sometimes, it is desirable to let the user
decide whether or not she will supply the next data item. |
|
For example, if a user is typing in a series of
data, but the program does not know ahead of the time how many items there
will be in the series. The program keeps asking for the next item, but when
it does not receive a new entry, it knows that it can continue with
printing the totals. |
|
|
|
|
|
|
The function that supports verified conditional
input returns a boolean value true, if the input was supplied, and returns
false when no input was given. |
|
The function is best illustrated as follows: |
|
int total = 0; int item; |
|
while (ReadingInt("Next Bar
Code"), item){ |
|
total = total + item * Price(item); |
|
} |
|
cout << "Total is: "
<< total << endl; |
|
|
|
|
|
|
Conditional Verified Read:(counts the
number of inputs, prints it at the
end) |
|
int n; |
|
int i = 0; |
|
while (ReadingInt("Type in the next
number:", n); |
|
i++; |
|
cout << n << " : "
<< n * n << endl; |
|
} |
|
cout << "Done " << i
<< " numbers entered." << endl; |
|
|
|
When you do not know how many times will the
input be supplied, use Conditional Verified Read, giving the user the
option to decline further input. |
|
|
|
|
|
|
bool ReadingInt(const string& prompt,
int& answer); |
|
When you do not know how many times will the
input be supplied, use Conditional Verified Read, giving the user the
option to decline further input. |
|
|
|
If user responds with a valid double then |
|
store data in parameter answer |
|
return true as function
value |
|
If user simply presses return then |
|
return false as function value |
|
If user responds with erroneous data then
give feedback and ask again |
|
|
|