Chapter 3. Decisions and Loops

WHAT YOU WILL LEARN IN THIS CHAPTER:

  • How to compare data values

  • How to alter the sequence of program execution based on the result

  • How to apply logical operators and expressions

  • How to deal with multiple choice situations

  • How to write and use loops in your programs

In this chapter, you will look at how to add decision-making capabilities to your C++ programs. You'll also learn how to make your programs repeat a set of actions until a specific condition is met. This will enable you to handle variable amounts of input, as well as make validity checks on the data that you read in. You will also be able to write programs that can adapt their actions depending on the input data, and that can deal with problems where logic is fundamental to the solution.

I'll start with one of the most powerful and fundamental tools in programming: the ability to compare variables and expressions with other variables and expressions and, based on the outcome, execute one set of statements or another.

COMPARING VALUES

Unless you want to make decisions on a whim, you need a mechanism for comparing things. This involves some new operators called relational operators. Because all information in your computer is ultimately represented by numerical values (in the last chapter you saw how character information is represented by numeric codes), comparing numerical values is the essence of practically all decision making. You have six fundamental operators for comparing two values available:

<

less than

<=

less than or equal to

>

greater than

>=

greater than or equal to

==

equal to

!=

not equal to

Note

The "equal to" comparison operator has two successive = signs. This is not the same as the assignment operator, which consists only of a single = sign. It's a common mistake to use the assignment operator instead of the comparison operator, so watch out for this potential cause of confusion.

Each of these operators compares the values of two operands and returns one of the two possible values of type bool: true if the comparison is true, or false if it is not. You can see how this works by having a look at a few simple examples of comparisons. Suppose you have created integer variables i and j with the values 10 and −5, respectively. The expressions,

i > j      i != j      j > −8      i <= j + 15

all return the value true.

Assume that you have defined the following variables:

char first = 'A', last = 'Z';

Here are some examples of comparisons using these character variables:

first == 65   first < last 'E' <= first   first != last

All four expressions involve comparing ASCII code values. The first expression returns true because first was initialized with 'A', which is the equivalent of decimal 65. The second expression checks whether the value of first, which is 'A', is less than the value of last, which is 'Z'. If you check the ASCII codes for these characters in Appendix B, notice that the capital letters are represented by an ascending sequence of numerical values from 65 to 90, 65 representing 'A' and 90 representing 'Z', so this comparison also returns the value true. The third expression returns the value false because 'E' is greater than the value of first. The last expression returns true because 'A' is definitely not equal to 'Z'.

Consider some slightly more complicated numerical comparisons. With variables defined by the statements

int i = −10, j = 20;
double x = 1.5, y = −0.25E-10;

take a look at the following expressions:

−1 < y      j < (10 - i)     2.0*x >= (3 + y)

As you can see, you can use expressions that result in numerical values as operands in comparisons. The precedence table for operators that you saw in Chapter 2 shows that none of the parentheses are strictly necessary, but they do help to make the expressions clearer. The first comparison is true, and so returns the bool value true. The variable y has a very small negative value, −0.000000000025, and so is greater than −1. The second comparison returns the value false. The expression 10 - i has the value 20, which is the same as j. The third expression returns true because the expression 3 + y is slightly less than 3.

You can use relational operators to compare values of any of the fundamental types, or of the enumeration types as I mentioned in Chapter 2, so all you need now is a practical way of using the results of a comparison to modify the behavior of a program.

The if Statement

The basic if statement allows your program to execute a single statement — or a block of statements enclosed within braces — if a given condition expression evaluates to true, or to skip the statement or block of statements if the condition evaluates to false. This is illustrated in Figure 3-1.

FIGURE 3-1

Figure 3.1. FIGURE 3-1

A simple example of an if statement is:

if('A' == letter)
   cout << "The first capital, alphabetically speaking.";

The condition to be tested appears in parentheses immediately following the keyword, if, and this is followed by the statement to be executed when the condition is true. Note the position of the semicolon here. It goes after the statement following the if and the condition between parentheses; there shouldn't be a semicolon after the condition in parentheses because the two lines essentially make up a single statement. You also can see how the statement following the if is indented, to indicate that it is only executed when the if condition returns the value true. The indentation is not necessary for the program to execute, but it helps you to recognize the relationship between the if condition and the statement that depends on it. The output statement in the code fragment is executed only if the variable letter has the value 'A'.

Note

When you are comparing a variable to a constant of some kind using the == operator, it is a good idea to write the constant on the left of the == operator, as in 'A' == letter. That way, if you accidentally write 'A' = letter, you will get an error message from the compiler. If you write letter = 'A', this is perfectly legal, though not what you intended, so no error message will be produced.

You could extend this example to change the value of letter if it contains the value 'A':

if('A' == letter)
{
   cout << "The first capital, alphabetically speaking.";
   letter = 'a';
}

The block of statements that is controlled by the if statement is delimited by the curly braces. Here you execute the statements in the block only if the condition ('A' == letter) evaluates to true. Without the braces, only the first statement would be the subject of the if, and the statement assigning the value 'a' to letter would always be executed. Note that there is a semicolon after each of the statements in the block, but not after the closing brace at the end of the block. There can be as many statements as you like within the block. Now, as a result of letter having the value 'A', you change its value to 'a' after outputting the same message as before. If the condition returns false, neither of these statements is executed.

Nested if Statements

The statement to be executed when the condition in an if statement is true can also be an if. This arrangement is called a nested if. The condition for the inner if is only tested if the condition for the outer if is true. An if that is nested inside another can also contain a nested if. You can generally continue nesting ifs one inside the other like this for as long as you know what you are doing.

The Extended if Statement

The if statement that you have been using so far executes a statement if the condition specified returns true. Program execution then continues with the next statement in sequence. You also have a version of the if that allows one statement to be executed if the condition returns true, and a different statement to be executed if the condition returns false. Execution then continues with the next statement in sequence. As you saw in Chapter 2, a block of statements can always replace a single statement, so this also applies to these ifs.

If the remainder is 1, the condition is true, and the statement immediately following the if is executed. If the remainder is 0, the condition is false, and the statement following the else keyword is executed. It's obvious here what the if expression is doing, but with more complicated expressions it's better to add the extra few characters needed for the comparison with zero to ensure that the code is easily understood.

Note

In an if statement, the condition can be an expression that results in a value of any of the fundamental data types that you saw in Chapter 2. When the condition expression evaluates to a numerical value rather than the bool value required by the if statement, the compiler inserts an automatic conversion of the result of the expression to type bool. A non-zero value that is cast to type bool results in true, and a zero value results in false.

The remainder from the division of an integer by two can only be one or zero. After either outcome, the return statement is executed to end the program.

Note

The else keyword is written without a semicolon, similar to the if part of the statement. Again, indentation is used as a visible indicator of the relationship between various statements. You can clearly see which statement is executed for a true or non-zero result, and which for a false or zero result. You should always indent the statements in your programs to show their logical structure.

The if-else combination provides a choice between two options. The general logic of the if-else is shown in Figure 3-2.

FIGURE 3-2

Figure 3.2. FIGURE 3-2

The arrows in the diagram indicate the sequence in which statements are executed, depending on whether the if condition returns true or false.

Nested if-else Statements

As you have seen, you can nest if statements within if statements. You can also nest if-else statements within ifs, ifs within if-else statements, and if-else statements within if-else statements. This provides considerable room for confusion, so take a look at a few examples. The following is an example of an if-else nested within an if.

if('y' == coffee)
   if('y' == donuts)
      cout << "We have coffee and donuts.";
   else
      cout << "We have coffee, but not donuts";

The test for donuts is executed only if the result of the test for coffee returns true, so the messages reflect the correct situation in each case; however, it is easy to get this confused. If you write much the same thing with incorrect indentation, you can be trapped into the wrong conclusion:

if('y' == coffee)
   if('y' == donuts)
      cout << "We have coffee and donuts.";
else                                  // This else is indented incorrectly
   cout << "We have no coffee...";    // Wrong!

The mistake is easy to see here, but with more complicated if structures you need to keep in mind the rule about which if owns which else.

Note

An else always belongs to the nearest preceding if that is not already spoken for by another else.

Whenever things look a bit complicated, you can apply this rule to sort things out. When you are writing your own programs you can always use braces to make the situation clearer. It isn't really necessary in such a simple case, but you could write the last example as follows:

if('y' == coffee)
{
   if('y' == donuts)
      cout << "We have coffee and donuts.";
   else
      cout << "We have coffee, but not donuts";
}

and it should be absolutely clear. Now that you know the rules, understanding the case of an if nested within an if-else becomes easy.

if('y' == coffee)
{
   if('y' == donuts)
      cout << "We have coffee and donuts.";
}
else
   if('y' == tea)
      cout << "We have tea, but not coffee";

Here the braces are essential. If you leave them out, the else would belong to the second if, which is looking out for donuts. In this kind of situation, it is easy to forget to include the braces and create an error that may be hard to find. A program with this kind of error compiles fine and even produces the right results some of the time.

If you removed the braces in this example, you get the correct results only as long as coffee and donuts are both equal to 'y' so that the if ('y' == tea) check wouldn't be executed.

Here you'll look at if-else statements nested in if-else statements. This can get very messy, even with just one level of nesting.

if('y' == coffee)
   if('y' == donuts)
      cout << "We have coffee and donuts.";
   else
      cout << "We have coffee, but not donuts";
else
   if('y' == tea)
      cout << "We have no coffee, but we have tea, and maybe donuts...";
   else
      cout << "No tea or coffee, but maybe donuts...";

The logic here doesn't look quite so obvious, even with the correct indentation. No braces are necessary, as the rule you saw earlier verifies that each else belongs to the correct if, but it would look a bit clearer if you included them.

if('y' == coffee)
{
   if('y' == donuts)
      cout << "We have coffee and donuts.";
   else
      cout << "We have coffee, but not donuts";
}
else
{
   if('y' == tea)
      cout << "We have no coffee, but we have tea, and maybe donuts...";
   else
      cout << "No tea or coffee, but maybe donuts...";
}

There are much better ways of dealing with this kind of logic in a program. If you put enough nested ifs together, you can almost guarantee a mistake somewhere. The following section will help to simplify things.

Logical Operators and Expressions

As you have just seen, using ifs where you have two or more related conditions can be a bit cumbersome. We have tried our iffy talents on looking for coffee and donuts, but in practice you may want to test much more complex conditions.

Logical operators provide a neat and simple solution. Using logical operators, you can combine a series of comparisons into a single logical expression, so you end up needing just one if, virtually regardless of the complexity of the set of conditions, as long as the decision ultimately boils down to a choice between two possibilities—true or false.

You have just three logical operators:

&&

Logical AND

||

Logical OR

!

Logical negation (NOT)

Logical AND

You would use the AND operator, &&, where you have two conditions that must both be true for a true result. You want to be rich and healthy. Thus the && operator produces the result true when both operands have the value true, and false otherwise.

You could use the && operator when you are testing a character to determine whether it's an uppercase letter; the value being tested must be both greater than or equal to 'A' AND less than or equal to 'Z'. Both conditions must return true for the value to be a capital letter.

Note

As before, the conditions you combine using logical operators may return numerical values. Remember that in this case a non-zero value converts to the value true; zero converts to false.

Taking the example of a value stored in a char variable letter, you could replace the test that uses two ifs with one that uses only a single if and the && operator:

if((letter >= 'A') && (letter <= 'Z'))
   cout << "This is a capital letter.";

The parentheses inside the expression that is the if condition ensure that there is no doubt that the comparison operations are executed first, which makes the statement clearer. Here, the output statement is executed only if both of the conditions that are combined by the && operator are true.

Note

If the left operand for the && operator is false, the right operand will not be evaluated. This becomes signi. cant if the right operand is an expression that can change something, such as an expression involving the ++ or -- operator. For example, in the expression x>=5 && ++n<10, n will not be incremented if x is less than 5.

Logical OR

The OR operator, ||, applies when you have two conditions and you want a true result if either or both of them are true. For example, you might be considered creditworthy for a loan from the bank if your income was at least $100,000 a year, or if you had $1,000,000 in cash. This could be tested using the following if.

if((income >= 100000.00) || (capital >= 1000000.00))
   cout << "How much would you like to borrow, Sir (grovel, grovel)?";

The ingratiating response emerges when either or both of the conditions are true. (A better response might be, "Why do you want to borrow?" It's strange how banks lend you money only if you don't need it.)

You only get a false result with the || operator when both operands are false.

Note

If the left operand for the || operator is true, the right operand will not be evaluated. For example, in the expression x>=5 || ++n<10, the variable n will not be incremented if x is greater than or equal to 5.

Logical NOT

The third logical operator, !, takes one operand of type bool and inverts its value. So, if the value of a variable test is true, !test is false; and if test is false, !test is true. To take the example of a simple expression, if x has the value 10, consider the expression

!(x > 5)

The result is false, because x > 5 is true.

You could also apply the ! operator in an expression that was a favorite of Charles Dickens':

!(income > expenditure)

If this expression is true, the result is misery, at least as soon as the bank starts bouncing your checks.

Finally, you can apply the ! operator to other basic data types. Suppose you have a variable, rate, that is of type float and has the value 3.2. For some reason, you might want to verify that the value of rate is non-zero, in which case you could use the expression:

!(rate)

The value 3.2 is non-zero and thus converts to the bool value true, so the result of this expression is false.

The Conditional Operator

The conditional operator is sometimes called the ternary operator because it involves three operands. It is best understood by looking at an example. Suppose you have two variables, a and b, and you want to assign the maximum of a and b to a third variable, c. You can do this with the following statement:

c = a > b ? a : b;           // Set c to the maximum of a or b

The first operand for the conditional operator must be an expression that results in a bool value, true or false, and in this case it is a > b. If this expression returns true, the second operand—in this case a — is selected as the value resulting from the operation. If the first argument returns false, the third operand — in this case b — is selected as the value that results from the operation. Thus, the result of the conditional expression a > b ? a : b is a if a is greater than b, and b otherwise. This value is stored in c as a result of the assignment operation. The use of the conditional operator in this assignment statement is equivalent to the if statement:

if(a > b)
   c = a;
else
   c = b;

The conditional operator can be written generally as:

condition ? expression1 : expression2

If the condition evaluates as true, the result is the value of expression1, and if it evaluates to false, the result is the value of expression2.

The switch Statement

The switch statement enables you to select from multiple choices based on a set of fixed values for a given expression. It operates like a physical rotary switch in that you can select one of a fixed number of choices. Some makes of washing machine provide a means of choosing an operation for processing your laundry in this way. There are a given number of possible positions for the switch, such as cotton, wool, synthetic fiber, and so on, and you can select any one of them by turning the knob to point to the option you want.

In the switch statement, the selection is determined by the value of an expression that you specify. You define the possible switch positions by one or more case values, a particular one being selected if the value of the switch expression is the same as the particular case value. There is one case value for each possible choice in the switch, and all the case values must be distinct.

If the value of the switch expression does not match any of the case values, the switch automatically selects the default case. You can, if you want, specify the code for the default case, as you will do below; otherwise, the default is to do nothing.

Unconditional Branching

The if statement provides you with the flexibility to choose to execute one set of statements or another, depending on a specified condition, so the statement execution sequence is varied, depending on the values of the data in the program. The goto statement, in contrast, is a blunt instrument. It enables you to branch to a specified program statement unconditionally. The statement to be branched to must be identified by a statement label, which is an identifier defined according to the same rules as a variable name. This is followed by a colon and placed before the statement requiring labeling. Here is an example of a labeled statement:

myLabel: cout << "myLabel branch has been activated" << endl;

This statement has the label myLabel, and an unconditional branch to this statement would be written as follows:

goto myLabel;

Whenever possible, you should avoid using gotos in your program. They tend to encourage convoluted code that can be extremely difficult to follow.

Note

Because the goto is theoretically unnecessary in a program — there's always an alternative approach to using goto — a significant cadre of programmers say you should never use it. I don't subscribe to such an extreme view. It is a legal statement, after all, and there are occasions when it can be convenient, such as when you must exit from a deeply nested set of loops (you learn about loops in the next section). I do, however, recommend that you only use it where you can see an obvious advantage over other options that are available; otherwise, you may end up with convoluted, error-prone code that is hard to understand and even harder to maintain.

REPEATING A BLOCK OF STATEMENTS

The capability to repeat a group of statements is fundamental to most applications. Without this capability, an organization would need to modify the payroll program every time an extra employee was hired, and you would need to reload your favorite game every time you wanted to play. So, let's first understand how a loop works.

What Is a Loop?

A loop executes a sequence of statements until a particular condition is true (or false). You can actually write a loop with the C++ statements that you have met so far. You just need an if and the dreaded goto. Look at the following example.

// Ex3_07.cpp
// Creating a loop with an if and a goto
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
   int i(1), sum(0);
   const int max(10);

loop:
   sum += i;             // Add current value of i to sum
   if(++i <= max)
      goto loop;         // Go back to loop until i = 11

   cout << endl
        << "sum = " << sum << endl
        << "i = "   << i   << endl;
   return 0;
}
                                                                 
What Is a Loop?

This example accumulates the sum of integers from 1 to 10. The first time through the sequence of statements, i has the initial value 1 and is added to sum, which starts out as zero. In the if, i is incremented to 2 and, as long as it is less than or equal to max, the unconditional branch to loop occurs, and the value of i, now 2, is added to sum. This continues with i being incremented and added to sum each time, until finally, when i is incremented to 11 in the if, the branch back is not executed. If you run this example, you get the following output:

sum = 55
i = 11

This shows quite clearly how the loop works; however, it uses a goto and introduces a label into the program, both of which you should avoid, if possible. You can achieve the same thing, and more, with the for statement, which is specifically for writing a loop.

Variations on the for Loop

Most of the time, the expressions in a for loop are used in a fairly standard way: the first to initialize one or more loop counters, the second to test if the loop should continue, and the third to increment or decrement one or more loop counters. You are not obliged to use these expressions in this way, however, and quite a few variations are possible.

The initialization expression in a for loop can also include a declaration for a loop variable. In the previous example, you could have written the loop to include the declaration for the loop counter i in the first control expression.

for(int i = 1; i <= max; i++)       // Loop specification
   sum += i;                        // Loop statement

Naturally, the original declaration for i would need to be omitted in the program. If you make this change to the last example, you will find that it does not compile because the loop variable, i, ceases to exist after the loop, so you cannot refer to it in the output statement. A loop has a scope which extends from the for expression to the end of the body of the loop, which of course can be a block of code between braces, as well as just a single statement. The counter i is now declared within the loop scope, so you cannot refer to it in the output statement, which is outside the scope of the loop. If you need to use the value in the counter after the loop has executed, you must declare the counter variable outside the scope of the loop.

You can omit the initialization expression altogether from the loop. Because i has the initial value 1, you can write the loop as:

for(; i <= max; i++)                  // Loop specification
   sum += i;                          // Loop statement

You still need the semicolon that separates the initialization expression from the test condition for the loop. In fact, both semicolons must always be present, regardless of whether any or all of the control expressions are omitted. If you omit the first semicolon, the compiler is unable to decide which expression has been omitted, or even which semicolon is missing.

The loop statement can be empty. For example, you could place the loop statement in the for loop from the previous example inside the increment expression; in this case the loop becomes

for(; i <= max; sum += i++);     // The whole loop

You still need the semicolon after the closing parentheses, to indicate that the loop statement is now empty. If you omit this, the statement immediately following this line is interpreted as the loop statement. Sometimes you'll see the empty loop statement written on a separate line, like the following:

for(; i <= max; sum += i++)     // The whole loop
  ;

Using the continue Statement

There is another statement, besides break, that you can use to affect the operation of a loop: the continue statement. This is written simply as:

continue;

Executing continue within a loop starts the next loop iteration immediately, skipping over any statements remaining in the body of the loop. I can show how this works with the following code:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
   int value(0), product(1);

   for(int i = 1; i <= 10; i++)
   {
     cout << "Enter an integer: ";
      cin >> value;

      if(0 == value)                    // If value is zero
         continue;                      // skip to next iteration
      product *= value;
}

   cout << "Product (ignoring zeros): " << product
        << endl;

   return 0;                            // Exit from loop
}

This loop reads 10 values with the intention of producing the product of the values entered. The if checks each value entered, and if it is zero, the continue statement skips to the next iteration. This is so that you don't end up with a zero product if one of the values is zero. Obviously, if a zero value occurred on the last iteration, the loop would end. There are clearly other ways of achieving the same result, but continue provides a very useful capability, particularly with complex loops where you may need to skip to the end of the current iteration from various points in the loop.

The effect of the break and continue statements on the logic of a for loop is illustrated in Figure 3-4.

FIGURE 3-4

Figure 3.4. FIGURE 3-4

Obviously, in a real situation, you would use the break and continue statements with some condition-testing logic to determine when the loop should be exited, or when an iteration of the loop should be skipped. You can also use the break and continue statements with the other kinds of loop, which I'll discuss later on in this chapter, where they work in exactly the same way.

Floating-Point Loop Counters

You can also use a floating-point value as a loop counter. Here's an example of a for loop with this kind of counter:

double a(0.3), b(2.5);
for(double x = 0.0; x <= 2.0; x += 0.25)
   cout << "
	x = " << x
        << "	a*x + b = " << a*x + b;

This code fragment calculates the value of a*x+b for values of x from 0.0 to 2.0, in steps of 0.25; however, you need to take care when using a floating-point counter in a loop. Many decimal values cannot be represented exactly in binary floating-point form, so discrepancies can build up with cumulative values. This means that you should not code a for loop such that ending the loop depends on a floating-point loop counter reaching a precise value. For example, the following poorly-designed loop never ends.

for(double x = 0.0 ; x != 1.0 ; x += 0.2)
  cout << x;

The intention with this loop is to output the value of x as it varies from 0.0 to 1.0; however, 0.2 has no exact representation as a binary floating-point value, so the value of x is never exactly 1. Thus, the second loop control expression is always false, and the loop continues indefinitely.

The while Loop

A second kind of loop in C++ is the while loop. Where the for loop is primarily used to repeat a statement or a block for a prescribed number of iterations, the while loop is used to execute a statement or block of statements as long as a specified condition is true. The general form of the while loop is:

while(condition)
   loop_statement;

Here loop_statement is executed repeatedly, as long as the condition expression has the value true. After the condition becomes false, the program continues with the statement following the loop. As always, a block of statements between braces could replace the single loop_statement.

The logic of the while loop can be represented as shown in Figure 3-5.

FIGURE 3-5

Figure 3.5. FIGURE 3-5

The do-while Loop

The do-while loop is similar to the while loop in that the loop continues as long as the specified loop condition remains true. The main difference is that the condition is checked at the end of the loop — which contrasts with the while loop and the for loop, where the condition is checked at the beginning of the loop. Consequently, the do-while loop statement is always executed at least once. The general form of the do-while loop is:

do
{
   loop_statements;
}while(condition);

The logic of this form of loop is shown in Figure 3-6.

FIGURE 3-6

Figure 3.6. FIGURE 3-6

You could replace the while loop in the last version of the program to calculate an average with a do-while loop:

do
{
   cout << endl
        << "Enter a value: ";
   cin >> value;                 // Read a value
   ++i;                          // Increment count
   sum += value;                 // Add current input to total

   cout << "Do you want to enter another value (enter y or n)?";
   cin >> indicator;             // Read indicator
} while(('y' == indicator) || ('Y' == indicator));

There's little difference between the two versions of the loop, except that this version doesn't depend on the initial value set in indicator for correct operation. As long as you want to enter at least one value, which is not unreasonable for the calculation in question, this version of the loop is preferable.

Nested Loops

You can nest one loop inside another. The usual application for this will become more apparent in Chapter 4 — it's typically applied to repeating actions at different levels of classification. An example might be calculating the total marks for each student in a class, and then repeating the process for each class in a school.

C++/CLI PROGRAMMING

Everything I have discussed in this chapter applies equally well in a C++/CLI program. Just to illustrate the point, we can look at some examples of CLR console programs that demonstrate some of what you have learned so far in this chapter. The following is a CLR program that's a slight variation on Ex3_01.

The for each Loop

All the loop statements I have discussed apply equally well to C++/CLI programs, and the C++/CLI language provides you with the luxury of an additional kind of loop called the for each loop. This loop also works with native C++ code in Visual C++ 2010, although formally the for each loop is not part of the ISO/IEC standard C++ language. The for each loop is specifically for iterating through all the objects in a particular kind of set of objects, and because you haven't learned about these yet, I'll just introduce the for each loop briefly here, and elaborate on it some more a bit later in the book.

Since you do know a little about the String object, which represents a set of characters, you can use a for each loop to iterate through all the characters in a string. Let's try an example of that.

In the last statement, the value of vowels replaces the "{0}" in the string and the value of consonants replaces the "{1}". This is because the arguments that follow the first format string argument are referenced by index values starting from 0.

SUMMARY

In this chapter, you learned all the essential mechanisms for making decisions in C++ programs. The ability to compare values and change the course of program execution is what differentiates a computer from a simple calculator. You need to be comfortable with all of the decision-making statements I have discussed because they are all used very frequently. You have also gone through all the facilities for repeating a group of statements. Loops are a fundamental programming technique that you will need to use in every program of consequence that you write. You will find you use the for loop most often in native C++, closely followed by the while loop. The for each loop in C++/CLI is typically more efficient than other loop forms, so be sure to use that where you can.

WHAT YOU LEARNED IN THIS CHAPTER

TOPIC

CONCEPT

Relational operators

The decision-making capability in C++ is based on the set of relational operators, which allow expressions to be tested and compared, and yield a bool value — true or false — as the result.

Decisions based on numerical values

You can also make decisions based on conditions that return non-bool values. Any non-zero value is cast to true when a condition is tested; zero casts to false.

Statements for decision-making

The primary decision-making capability in C++ is provided by the if statement. Further flexibility is provided by the switch statement, and by the conditional operator.

Loop statements

There are three basic methods provided in ISO/IEC C++ for repeating a block of statements: the for loop, the while loop, and the do-while loop. The for loop allows the loop to repeat a given number of times. The while loop allows a loop to continue as long as a specified condition returns true. Finally, do-while executes the loop at least once and allows continuation of the loop as long as a specified condition returns true.

The for each statement

C++/CLI provides the for each loop statement in addition to the three loop statements defined in ISO/IEC C++.

Nested loops

Any kind of loop may be nested within any other kind of loop.

The continue keyword

The keyword continue allows you to skip the remainder of the current iteration in a loop and go straight to the next iteration.

The break keyword

The keyword break provides an immediate exit from a loop. It also provides an exit from a switch at the end of statements in a case.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.172.195