Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

60

Part I: Introduction to C++ Programming

cout << “Arg1 cout << “Arg2 cout << “~nArg1 cout << “~nArg2

=0x” << nArg1 << “\n”;

=0x” << nArg2 << “\n”;

=0x” << ~nArg1 << “\n”;

=0x” << ~nArg2 << “\n”;

// now the binary operators cout << “nArg1 & nArg2 = 0x”

<<(nArg1 & nArg2)

<<“\n”;

cout << “nArg1 | nArg2 = 0x”

<<(nArg1 | nArg2)

<<“\n”;

cout << “nArg1 ^ nArg2 = 0x”

<<(nArg1 ^ nArg2)

<<“\n”;

//wait until user is ready before terminating program

//to allow the user to see the program results system(“PAUSE”);

return 0;

}

The first expression in our program, cout.setf(ios::hex), sets the output format from the default decimal to hexadecimal (you’ll have to trust me for now that it works).

The remainder of the program is straightforward. The program assigns nArg1 the test value 0x1234 and nArg2 the value 0x00ff. The program then out­ puts all combinations of bitwise calculations. The process looks like this:

Arg1

=

0x1234

Arg2

=

0xff

~nArg1

=

0xffffedcb

~nArg2

=

0xffffff00

nArg1 & nArg2 =

0x34

nArg1 | nArg2 =

0x12ff

nArg1 ^ nArg2 =

0x12cb

Press any key to continue . . .

Do something logical with logical calculations

Running through simple and bitwise logical calculations in your head at par­ ties is fun (well, okay, for some of us), but a program has to make actual, prac­ tical use of these values to make them worth the trouble. Coming right up:

Chapter 5 demonstrates how logical calculations are used to control program flow.

Chapter 5

Controlling Program Flow

In This Chapter

Controlling the flow through the program

Executing a group of statements repetitively

Avoiding infinite loops

The simple programs that appear in Chapters 1 through 4 process a fixed number of inputs, output the result of that calculation, and quit. However,

these programs lack any form of flow control. They cannot make tests of any sort. Computer programs are all about making decisions. If the user presses a key, the computer responds to the command.

For example, if the user presses Ctrl+C, the computer copies the currently selected area to the Clipboard. If the user moves the mouse, the pointer moves on the screen. If the user clicks the right mouse button with the Windows key depressed, the computer crashes. The list goes on and on. Programs that don’t make decisions are necessarily pretty boring.

Flow-control commands allow the program to decide what action to take, based on the results of the C++ logical operations performed (see Chapter 4). There are basically three types of flow-control statements: the branch, the loop, and the switch.

Controlling Program Flow with the Branch Commands

The simplest form of flow control is the branch statement. This instruction allows the program to decide which of two paths to take through C++ instruc­ tions, based on the results of a logical expression (see Chapter 4 for a descrip­ tion of logical expressions).

62

Part I: Introduction to C++ Programming

In C++, the branch statement is implemented using the if statement:

if (m > n)

{

//Path 1

//...instructions to be executed if

//m is greater than n

}

else

{

//Path 2

//...instructions to be executed if not

}

First, the logical expression m > n is evaluated. If the result of the expression is true, control passes down the path marked Path 1 in the previous snip­ pet. If the expression is false, control passes to Path 2. The else clause is optional. If it is not present, C++ acts as if it is present but empty.

Actually, the braces are optional (sort of) if there’s only one statement to exe­ cute as part of the if. If you lose the braces, however, it’s embarrassingly easy to make a mistake that the C++ compiler can’t catch. The braces serve as a guide marker; it’s much safer to include ’em. (If your friends try to entice you into not using braces, “Just say No.”)

The following program demonstrates the if statement (note all the lovely braces):

//BranchDemo - input two numbers. Go down one path of the

//program if the first argument is greater than

//the first or the other path if not

#include <cstdio> #include <cstdlib> #include <iostream> using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{

//input the first argument...

int arg1;

cout << “Enter arg1: “; cin >> arg1;

//...and the second

int arg2;

cout << “Enter arg2: “; cin >> arg2;

// now decide what to do: if (arg1 > arg2)