C++ Chapter 4 : Making Decisions

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Note:

As you will see later in this chapter, 1 is not the only value regarded as true.

Relational Operators

4.1

!=

not equal to

In C++ 1 means what Boolean outcome?

true

The if statement

4.2

Expanding the if statement

4.3

The if /else statement

4.4

Nested if statements

4.5

Flags

4.7

Logical operators

4.8

concept:

A flag is a boolean or integer variable that signals when a condition exists.

Flags

A flag is typically a bool variable that signals when some condition exists in the program. When the flag is set to false, it indicates that the condition does not exist.

note:

Anytime your program has a block of code, all the statements inside the braces should be indented.

When using an if statement, don't forget to use -------.

BRACES

Concept:

Logical operators connect two or more relational expressions into one or reverse the logic of an expression

>

Greater than

Note...

In most editors, each time you press the tab key, you are indenting one level. By indenting the conditionally executed statement you are causing it to stand out visually. This is so you can tell at a glance what part of the program the if statement executes. This is a standard way of writing if statements and is the method you should use.

<=

Less than or equal to

Note...

Relational expressions have a higher precedence than the assignment operator. In the statement z = x <y; the expression x < y is evaluated first, and then the value is assigned to z.

Warning!

Notice the quality operator is two = symbols together. Don't confuse this operator with the assignment operator, which is one = symbol. The == operator determines whether the variable is equal to another value, but the = operator assigns the value on the operator's right to the variable on the left

Back to TRUTH

Now that you've gotten your feet wet with relational expressions and if statements, lets look at the subject of truth again. You have seen that a relational expression has the value 1 when it is true and 0 when false. In the world of the if statement, however, the concept of truth is expanded. 0 is still false, but all values other than 0 are considered true. This means that any value, even a negative number, represents true as long as it is not 0. Just as in real life, truth is a complicated thing. Here is a summary of the rules you have seen so far

In C++ 0 means which Boolean outcome?

false

To avoid round-off errors from occurring, you should stick with __________ and ____________________ comparisons with floating-point numbers

greater than and less than;

>=

greater than or equal to

The if statement -

You might think of the steps of a procedural program as individual steps as you are walking down a road. To reach the destination, you must start at the beginning and take each step, one after the other, until you reach the destination. The programs you have written so far like a "path" of execution for the program to follow.

The if/else if statement

tests a series of conditions; It is often simpler to tests a series of conditions with the if/else if statement than with a set of nested if/else statements

<

less than

conditionally executed

performed only when a certain condition is true

if/else statement:

As with the if statement, an expression is evaluated. If the expression is true, a statement or block of statements is executed. If the expression is false, however, a separate group of statements is executed.

Note

All the relational operators have left to right associativity. Recall that associativity is the order in which an operator works with its operands.

relational expression

An expression used to determine whether a specific relationship exists between two values.

Notes on if statement programming style

The conditionally executed statement should appear on the line after the if statement. The conditionally executed statement should be indented one "level" from the "if" statement

continued:

The fact that the if statement considers any nonzero value as true opens many possibilities. Relational expressions are not the only conditions that may be tested. For example, the following is a legal if statement in C++: if (value) cout << "It is true!";

Note

The general format shows braces surrounding each block of conditionally executed statements. As with other forms of the if statement, the braces are required only when more than one statement is conditionally executed

Concept:

The if statement can cause other statements to execute only under certain conditions.

Concept:

The if statement can conditionally execute a block of statements enclosed in braces

concept:

The if/else statement will execute one group of statements if the expression is true, or anohter group of statements in the expression is false

What is truth?

The question "what is truth?" is one you should expect to find in a philosophy book, not a C++ programming text. It's a good question for us to consider, though. If a relational expression can be either true or false, how are those values represented internally in a program? How does a computer store TRUE in memory? How does it store false?

Note..

Indentation and spacing are for human readers of a program, not the compiler. Even though the cout statement following the if statement is indented, the semicolon still terminates the if statement.

Concept:

Relational operators allow you to compare numeric and char values and determine whether one is greater than, less than, equal to, or not equal to another.

The value of relationship:

So, how are relational expressions used in a program? Remember, all expressions have a value. Relational expressions are also known as Boolean expressions, which mean there value can only be true or false. If x is greater than y, the expression x > y will be true, while the expression y == x will be false. The == operator determines whether the operand on its left is equal to the operand on its right. If both operands have the same value, the expression is true.

decision structure

A fundamental control structure used in computer programming that deals with the different conditions that occur based on the values entered into an application. Or --- a specific action is taken only when a specific condition exists. If the condition does not exist, the action is not performed.

division by zero

Dividing any number by 0 results in a quotient that is undefined - division by zero is mathematically impossible to perform.

==

Equal to

Programming style and decision structures

For readability and easier debugging, it's important to use proper alignment and indentation in a set of nested if statements. This make it easier to see which actions are performed by each part of the decision structure

Rules:

When a relational expression is true it has the value 1. When a relational expression is false it has the value 0. Any expression that has the value 0 is considered false by the if statement. This includes the bool value false, which is equivalent to 0. Any expression that has any value other than 0 is considered true by the if statement. This includes the bool value true, which is equivalent to 1.

The "if/else if" statement compared to a nested decision structure

You never have to use the if/else if statement because its logic can be coded with nested if/else statements. However, a long series of nested if/else statements has two particular disadvantages when you are debugging code: 1- the code can grow complex and become difficult to understand 2- because indenting is important in nested statements, a long series of nested if/else statements can become too long to be displayed on the computer screen without horizontal scrolling. Also, long statements tend to "wrap around" when printed on paper, making the code even more difficult to read.

The last relational operator is !=, which is the not equal operator. It determines whether the operand on its left is not equal to the operand on the right, which is opposite of the == operator. As before, assuming a is 4, b is 6, and c is 4, both of the following expressions are true:

a != b b != c These expressions are true because a is not equal to b and b is not equal to c. But the following expression is false because a is equal to c: a != c

The <= operator determines whether the operand on its left is less than or equal to the operand on its right. Once again, assuming that a is 4, b is 6, and c is 4, both of the following expressions are true:

a <= c b <= 10 but the following is false: b <= a

A couple of the relational operators actually test for two relationships. the >= operator determines whether the operand on its left is greater than or equal to the operand on the right. Assuming that a is a 4, b is a 6, and c is 4, both of the following expressions are true:

b >= a a >= c but the following is false: a >= 5

In this statement the sum of x and y is tested like any other value in an if statement: 0 is false and all other values are true. You may also use the return value of function calls as conditional expressions. Her eis an example that uses the pow function:

if (pow(a, b)) cout << "It is true!"; This is statement uses the pow function to raise a to the power of b. If the result is anything other than 0, the cout statement is executed. This is a powerful programming technique that you will learn more about in chapter 6

The if statement above does not test a relational expression, but rather the contents of a variable. If the variable, value, contains any number, other than 0, the message "It is true!" will be displayed. If value is set to 0, however, the cout statement will be skipped. here is an example:

if (x + y) cout << "It is true!";

integer flags

integer variables may also be used as flags. This is because in C++ the value 0 is considered false, and any nonzero value is considered true.

&& operator

means "AND" - Connexts two expressions into one. Both expressions must be true fr the overall expression to be true.

trailing else

placed at the end of an if/else statement, provides a default set of actions when none of the if expressions are true.

sequence structure

the control structure that directs the computer to process each instruction in the order listed in the program

Concept:

to test more than one condition, an if statement can be nested inside another if statement.


संबंधित स्टडी सेट्स

ขรกที่ดี 50 คะแนน

View Set

Ch 8-11 Comm, Culture and Community

View Set

Economics 2.2- Opportunity Cost, Trade-Offs, and Choices.

View Set