If then / If then else statements
 | Format
if condition
thenstatement
if condition
thenstatement
else
elsestatement2
for condition
- an expression followed by a relational operator followed by an expression (x+1 < y+2)
or
- a condition followed by a logical operation followed by a condition (x<y &&
y<z)
relational operators: < > <= >= == !=
logical operators: ! && ||
NOTES:
- Parentheses are REQUIRED around the entire condition.
- A semicolon ALWAYS follows the statement preceding else.
- Braces are required if the thenstatement or the elsestatement is composed of more than
one C++ statement.
|
 | All operators have precedence. Of those considered thus far, the precedence is:
()
++,--,!,+,-
*,/,%
+,- (binary operators)
< > <= >= == !=
&&
|| |
 | Examples
if (v1 < v2)
v2 = v2 - v1;
if (v1 < v2)
v2 = v2 - v1;
else
v1 = v1 - v2;
if (v1 != v2) {
t = v1;
v1 = v2;
v2 = t;
}
if (v1<v2 && v2<v3)
t ++;
else
t--; |
|

Example programs
 | Simple if-then-else
#include <iostream.h>
main () {
/* Determine whether a given integer is negative.*/
int i;
cout << "Give value.\n";
cin >> i;
if (i < 0)
cout << i << " is negative.\n";
else
cout << i << " is not negative.\n";
} |
|
 | Nested if-then-else
main () {
/* Determine whether a given integer is positive, negative, or zero.*/
int i;
cout << "Give value.\n";
cin >> i;
if (i < 0)
cout << i << " is negative.\n";
else
if (i == 0)
cout << i << "is zero.\n";
else
cout << i << " is positive.\n";
} |
|
 | More than one statement in then or else
main () {
int i;
cout << "Give value.\n";
cin >> i;
if (i < 0) {
cout << i << " is negative.\n";
i = -1 * i;
}
else {
cout << i << " is not negative.\n";
i = 2 * i;
}
} |
|

Use of other types of expressions as condition
 | There are no Boolean data types in C++; BUT any zero value used in the place of a
condition is considered false and any non-zero value is considered to be true. Therefore
the result of:
Example 1
if (37)
cout << "in then portion\n";
else
cout << "in else portion\n";
|
will be:
in then portion
and the result of |
Example 2
i = 0;
if (i)
cout << "in then portion\n";
else
cout << "in else portion\n"; |
will be:
|
 | The return value of an assignment is its final lvalue. Therefore, the result of
Example 3
if (a = 2)
cout << "a = 2\n";
else
cout << "a != 2\n"; |
is:
because the value of a = 2 is 2.
And the result of
Example 4
if (a = 0)
cout << "a = 0\n";
else
cout << "a != 0\n"; |
is:
because the value of a = 0 is 0 (false).
REMINDER: If the condition is true, the then portion is
executed. If the condition is false, the else portion is executed.
|
 | Input statements as conditions
- Input statements also have value. The value of cin.get(ch) and cin >> ch is zero
if eof was encountered and a reference to the istream otherwise, i.e. not zero.
Therefore, the result of
|
 | Example 5
if (cin >> b)
cout << "b\n";
else
cout << "eof encountered\n"; |
will result in the value of b being printed if there was a value to read and the eof
message if the last value HAS ALREADY BEEN READ.
|

Conditional operator (?:)
 | Format
<arithmeticif> ::= <condition> ? <expression1> : <expression2> |
 | Examples
i ? cout << "i != 0\n" : cout << "i == 0\n";
cout << (i ? "i != 0\n" : "i == 0\n");
a = (i>=0 ? i : -i); |


 | while
- Pretest loop
- Format
whilecondition
whilestatement
- Examples
while (x<y)
cout << x++ << y << '\n';
while (x>0 || y!=28) {
cout << x << y++;
x-=5;
}
numneg = 0;
sum = 0;
while (cin >> val)
if (val>=0)
sum +=val;
else
numneg++;
char ch;
while (cin.get(ch) && ch != ' ' && ch != '\n')
cout << ch;
sum = 0;
while (cin>>val && val>=0) {
sum += val;
}
char ch;
while (cin.get(ch) && (ch == ' ' || ch == '\n'));
char ch;
while (cin >> ch); |
|
 | for
- Pretest loop
- Usually used for counted looping
- Format
for (init_cntrl; cont_cond; mod_cntrl)
forstatement
- Examples
for (int i=0; i<5; i++)
cout << i;
cout << '\n'; |
Results in the following being sent to output: 01234
NOTE: This loop is equivalent to:
int i = 0;
while (i<5) {
cout << i;
i++;
} |
Note that the loop control variable is incremented at the END of the body of the loop.
for (int cnt=10; cnt>1; cnt--) {
cin >> next;
cout<<"next is "<<next<<'\n';
} |
- Scope: A loop control id which is declared in the for line is in scope only in the for
loop (per ANSI committee).
|
 | do
- Posttest loop
- Format
do
statement
while (condition);
- Example
do {
cout << '*';
x++; }
while (x < y); |
Example 2
#include <iostream.h>
main()
{
/* Find the sums of several pairs of integers and determine whether each sum is
positive or negative.*/
float f1, f2, avg;
while (cin >> f1 ) {
cin >> f2;
avg = (f1 + f2) / 2;
cout <<"The average of " << f1 << "
and " << f2 << is << avg << '/n';
if (avg < 0)
cout << avg << " is negative.\n";
else
cout << avg << " is positive.\n";
}
} |
|
 | Writing loops
- Indent properly
- In as far as is possible, arrange statements within loop to avoid repetition of checking
same condition twice.
- ONE ENTRANCE, ONE EXIT (no break or exit)
|
|