
if (area < 0) cerr << "Error: Negative area.\n";
if (area < 0)
{
cerr << "Error: Negative area.\n";
return 1;
}
|
Syntax 4.1 : if Statement if (condition) statement
|
|
Syntax 4.2 : Block Statement {
statement1
statement2
...
statementn
}
|
if (area >= 0) cout << "The side length is " << sqrt(area) << "\n"; else cout << "Error: Negative area.\n";
if (area >= 0) /* complementary conditions */ cout << "The side length is " << sqrt(area) << "\n"; if (area < 0) /* complementary conditions */ cout << "Error: Negative area.\n";
|
Syntax 4.3 : if Statement if (condition) statement1 else statement2
|
|
C++
|
Description
|
Example
|
Notes
|
|
>
|
greater than
|
a > 5
|
|
|
>=
|
greater than or equal to
|
x >= 5
|
Be careful to not write => Remember that the symbols appear in the order you say them |
|
<
|
less than
|
x < 10
|
|
|
<=
|
less than or equal to
|
x <= 100
|
Be careful to not write =< Remember that the symbols appear in the order you say them |
|
==
|
equal to
|
a == 5
|
Don't confuse with = which is assignment. |
|
!=
|
not equal
|
a != 5
|
The ! is supposed to be the line that "crosses through" the equal sign. |
"cargo" is less than "cathode"
c a r
c a r g o
c a r g o
c a t h o d e
user types "five" and hits return! (Causing cin to fail).double area; cin >> area;
if (cin.fail())
if (area < 0)
if (cin) /* the stream did not fail */ else /* the stream failed */
/* how long does it take an investment to double? */
while( balance < 2 * initial_balance)
{
balance = balance * ( 1 + rate / 100);
year++;
}
|
Syntax 4.4 : while Statement while (condition) statement
|
bool more = true;
while (more)
{
cin >> next;
if (cin.fail())
more = false;
else
{
// process next
}
}
while(more == false) /* don't */ while(more != false) /* don't */