Two Control Statements
Inside a function, execution proceeds from one statement to the next, top to bottom. It is possible, however, to alter this flow through the use of the various program control statements supported by C++. Although we will look closely at control statements later, two are briefly introduced here because we will be using them to write sample programs. The if Statement You can selectively execute part of a program through the use of C++’s conditional statement: the if. The if statement works in C++ much likethe IF statement
in any other language. For example, it is syntactically identical to the if statements in C, Java, and C#. Its simplest form is shown here:if(condition) statement;
where condition is an expression that is evaluated to be either true or false. In C++, true is nonzero and false is zero. If the condition is true, then the statement will execute. If it is false, then the statement will not execute. For example, the following fragment displays the phrase 10 is less than 11 on the screen because 10 is less than 11.
if(10 < 11) cout << "10 is less than 11";
However, consider the following:
if(10 > 11) cout << "this does not display";
In this case, 10 is not greater than 11, so the cout statement is not executed. Of course, the operands inside an if statement need not be constants. They can also be variables.
C++ defines a full complement of relational operators that can be used in a conditional expression. They are shown here:
< | Less than |
<= | Less than or equal |
== | equal to |
> | Greater than |
>= | Greater than or equal |
!= | Not equal |
The for Loop
You can repeatedly execute a sequence of code by creating a loop. C++ supplies a powerful assortment
of loop constructs. The one we will look at here is the for loop. If you are familiar with C# or Java, then
you will be pleased to know that the for loop in C++ works the same way it does in those languages. The
simplest form of the for loop is shown here:
for(initialization; condition; increment) statement;
Here, initialization sets a loop control variable to an initial value. condition is an expression that is tested
each time the loop repeats. As long as condition is true (nonzero), the loop keeps running. The increment is an expression that determines how the loop control variable is incremented each time the
loop repeats increment is an expression that determines how the loop control variable is incremented each time the
loop repeats
In the loop, count is initialized to 1. Each time the loop repeats, the condition
count <= 100
is tested. If it is true, the value is output and count is increased by one. When count reaches a value
greater than 100, the condition becomes false, and the loop stops running. In professionally written C++
code, you will almost never see a statement like
count=count+1
because C++ includes a special increment operator that performs this operation more efficiently. The
increment operator is ++ (two consecutive plus signs). The ++ operator increases its operand by 1. For
example, the preceding for statement will generally be written like this:
for(count=1; count <= 100; count++) cout << count << " ";
This is the form that will be used throughout the rest of this book.
C++ also provides a decrement operator, which is specified as – –. It decreases its operand by 1.
No comments:
Post a Comment