{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Chapter3PPTa - Chapter 3 DECISION STRUCTURES 1 THE if...

This preview shows pages 1–12. Sign up to view the full content.

1 Chapter 3 DECISION STRUCTURES

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 THE if STATEMENT The if statement allows us to specify the condition under which a statement or block of statements is to be executed. The general form of the if statement is as follows: if (Boolean expression) { statement(s); }
3 THE if STATEMENT The key word if is followed by a parenthesized Boolean expression, often called the conditional expression . If the conditional expression is true , the statement(s) in the block that follows are executed. The statement(s) in the block are skipped if the conditional expression is false . We say that the block of statements is conditionally executed , because they are only executed when the conditional expression is true . if (Boolean expression) { statement(s); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 THE if STATEMENT Using Relational Operators to Form Boolean Expressions Frequently, the Boolean expression used as the test in an if statement is formed using a relational operator. A relational operator tests whether a particular relationship exists between its operands. *** See Table 3-1 of the text
5 THE if STATEMENT Using Relational Operators to Form Boolean Expressions is not equal to != is equal to == is less than or equal to <= is greater than or equal to >= is less than < is greater than > Meaning Relational Operator

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 THE if STATEMENT Using Relational Operators to Form Boolean Expressions All of the relational operators are Binary operators, meaning they take two operands. The relational operators have lower precedence than the arithmetic operators *, /, %, +, and -, but higher precedence than the assignment operators (including the combined assignment operators). The relational operators associate from left-to-right. *** See Appendix C of the text
7 THE if STATEMENT Using Relational Operators to Form Boolean Expressions Values of variables Expression Value? int a = 5, b = 6 b < a double a = 8.9, b = 4.2 a > b int a = 10, b = 12 a <= 10 int a = 10, b = 12 a != b char a = ‘a’, b = ‘A’ a == b char a = ‘a’, b = ‘A’ a >= ‘Z’ int a = 5, b = 6, c = 4 a * c != (b + c) * 2 Problem: Given that the variables a and b have the values specified at the left, fill in the table showing the values of the expressions given. The value of a relational expression is either true or false .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 THE if STATEMENT The statement below specifies that five is to be added to t only if x is greater than zero. if(x > 0) { t += 5; }
9 THE if STATEMENT The logic of the if statement given could be depicted as follows: if(x > 0) { t += 5; } x > 0 false true t = t + 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
10 THE if STATEMENT Notice that there is not a semicolon after the parenthesized Boolean expression. Remember that semicolons mark the end of a statement, not the end of a line. A complete if statement includes a conditionally executed statement or block. if(x > 0) { t += 5; } There should not be a semicolon here!
11 THE if STATEMENT If you accidentally put a semicolon after the conditional expression, the compiler will think that the conditionally executed statement is the null statement. The null statement is an empty statement that does nothing.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 50

Chapter3PPTa - Chapter 3 DECISION STRUCTURES 1 THE if...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online