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

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

View Full Document Right Arrow Icon
1 Chapter 3 DECISION STRUCTURES
Background image of page 1

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

View Full DocumentRight Arrow Icon
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); }
Background image of page 2
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); }
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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 .
Background image of page 7

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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!
Background image of page 10
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.
Background image of page 11

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

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/14/2008 for the course CS 1336 taught by Professor Thompson during the Spring '08 term at University of Texas at Dallas, Richardson.

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 Right Arrow Icon
Ask a homework question - tutors are online