Statements and expressions for making decisions, or choices, are similar to those in Java, with a few small differences.
if-else
Statement if (condition) { statements1 } else { statements2 }starts by evaluating condition, an expression that normally yields either true or false. If the condition yields true, then statements1 are performed. If the condition yields false, then statements2 are performed. For example, if(x > 0) { y = x; } else { y = -x; }ends with y equal to the absolute value of x. The parentheses around the condition in an if-statement are required. Do not omit them. |
Example: finding the larger of two numbers, version 1
// maximum(x,y) returns the larger of x and y. // If x and y are equal, it returns that shared value. // Examples: // maximum(1,5) = 5 // maximum(3,2) = 2 // maximum(4,4) = 4 int maximum(const int x, const int y) { if(x > y) { return x; } else { return y; } } |
Omitting the else part
If you omit the word else and the
compound statement that
follows it, then nothing is done when the condition is false.
For example,
if(x < 0) { x = -x; }will always end with x ≥ 0, since it does nothing if x ≥ 0 when it starts. |
Example: finding the larger of two numbers, version 2
// maximum(x,y) returns the larger of x and y. // If x and y are equal, it returns that shared value. // Examples: // maximum(1,5) = 5 // maximum(3,2) = 2 // maximum(4,4) = 4 int maximum(const int x, const int y) { int result = x; if(x < y) { result = y; } return result; } |
More than two cases
If you have more than two cases, use the following
indentation scheme.
if(test1) { … } else if(test2) { … } else if(test3) { … } else { … } |
Example: Getting the sign of a number
// sign(x) is // -1 if x < 0 // 1 if x > 0 // 0 if x = 0 int sign(const double x) { if(x < 0) { return -1; } else if(x > 0) { return 1 } else { return 0; } } |
Making choices in expressions
Expression a ? b : c starts by evaluating expression a.
If a is true (or nonzero), then it yields the value of
expression b (and does not
compute c at all). If a is false (or 0), it yields the value
of expression c (and does not compute b at all).
For example,
int m = x > 0 ? x : -x;creates variable m and stores the absolute value of x into m. Only use this for expressions, not for statements. The coding standards require that. |
Example: finding the larger of two numbers, version 3
// maximum(x,y) returns the larger of x and y. // If x and y are equal, it returns that shared value. // Examples: // maximum(1,5) = 5 // maximum(3,2) = 2 // maximum(4,4) = 4 int maximum(const int x, const int y) { return x > y ? x : y; } |
Details on if-statements
The parentheses around the condition in an if-statement are
required. Do not omit them.
The condition is not required (by C++) to be a boolean value. It can be an integer or anything that is represented internally as an integer, such as a character or pointer. In all cases, 0 means false and anything other than 0 means true. However, for this course the coding standards require (with very minor exceptions) that conditions be boolean values. The condition should not be a pointer, for example. The statements following the condition and following else are shown as compound statements (enclosed in braces) above. C++ does not require that. You can write any single statement at all. However, the coding standards for this course do require a compound statement. |
Watch out: equality tests
If you are doing an equality test, be sure to use
==, not =. Statement
if(n = 0) { m = 1; } else { m = 2; }always makes m = 2. Expression n = 0 is not a test, it is an assignment. It stores 0 into n, and yields result 0. Since 0 is treated as false, the else-part is performed. If you ask the compiler to give common warnings (option -Wall), it will let you know about this mistake. Do not use = in a test even if it is what you want. The coding standards do not allow that. |
Watch out: semicolons
Be careful about semicolons. A lone semicolon is
a statement that does nothing.
Statement
if(n == 0); { printf("n is zero\n"); }always writes "n is zero", regardless of the value of n. The semicolon at the end of the first line is an empty statement, so the if-statement says, if n equals 0, do nothing. The statement after that is just a compound statement. This really does two statements in a row. The coding standards require you not to use a semicolon as an empty statement. |
Use an if-statement to make a decision.
The else part of an if-statement is optional. If it is omitted, and implicit
else {}is automatically added.
Assume that integer variable n already has a value. Write a statement that makes m equal to 3n+1 if n is odd and to n/2 if n is even. Answer
Suppose that you want to set y equal to
Explain why the following does not do the job.
if(x < 0) { y = -1; } if(x == 0) { y = 0; } else { y = 1; }Answer
Are the parentheses around the condition in an if-statement required or optional? Answer
Is the condition in an if-statement required to use a comparison operator? Answer
Is the following allowed (a) in C++, (b) by the coding standards for this course?
if(x > 0) y = 0; else y = x - 1;Answer