|
|
|
|
|
|
|
(text box continued from previous page) |
|
|
|
|
|
|
|
|
if (n >= 2)
{
alpha = 5;
beta = 8;
}
else
{
alpha = 23;
beta = 12;
} |
|
|
|
| |
|
|
|
|
Another popular style is to place the left braces at the end of the if line and the else line; the right braces still line up directly below the words if and else. This way of formatting the If statement originated with programmers using the C language, the predecessor of C++. |
|
|
|
| |
|
|
|
|
if (n >= 2) {
alpha = 5;
beta = 8;
}
else {
alpha = 23;
beta = 12;
} |
|
|
|
| |
|
|
|
|
It makes no difference to the C++compiler which style you use (and there are other styles as well). It's a matter of personal preference. Whichever style you use, though, you should always use the same style throughout a program. Inconsistency can confuse the person reading your program and give the impression of carelessness. |
|
|
|
|
|
|
|
|
|
Sometimes you run into a situation where you want to say, If a certain condition exists, then perform some action; otherwise, don't do anything. In other words, you want the computer to skip a sequence of instructions if a certain condition isn't met. You could do this by leaving the else branch empty, using only the null statement: |
|
|
|
|
|
|
|
|
if (a <= b)
c = 20;
else
; |
|
|
|
|
|