CH3 Notes - Flow of Control Chapter 3 Objectives learn...

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

View Full Document Right Arrow Icon
Flow of Control Chapter 3 Objectives learn about Java branching statements learn about loops learn about the type boolean (optional) learn to use color and other graphic enhancements Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean (optional) Graphics Supplement Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement chooses between two or more possible actions. A loop statement repeats an action until a stopping condition occurs. Branching Statements: Outline The if-else Statement Introduction to Boolean Expressions Nested Statements and Compound Statements Multibranch if-else Statements The switch Statament (optional) The Conditional Operator The if-else Statement A branching statement that chooses between two possible actions. syntax if ( Boolean_Expression ) Statement_1 else Statement_2 The if-else Statement, cont. example if (count < 3) total = 0 else total = total + count The if-else Statement, cont.
Background image of page 1

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

View Full DocumentRight Arrow Icon
class BankBalance Compound Statements To include multiple statements in a branch, enclose the statements in braces. if (count < 3) { total = 0; count = 0; } Omitting the else Part If the else part is omitted and the expression after the if is false, no action occurs. syntax if ( Boolean_Expression ) Statement example if (weight > ideal) caloriesPerDay -= 500; Introduction to Boolean Expressions The value of a boolean expression is either true or false. examples time < limit balance <= 0 Java Comparison Operators Compound Boolean Expressions Boolean expressions can be combined using the “and” ( ) operator. example ... not allowed if (0 < score <= 100) ... Compound Boolean Expressions, cont. syntax ( Sub_Expression_1 Sub_Expression_2 ) Parentheses often are used to enhance readability. The larger expression is true only when both of the smaller expressions are true. Compound Boolean Expressions, cont. Boolean expressions can be combined using the “or” ( || ) operator. example if ((quantity > 5) || (cost < 10)) ...
Background image of page 2
syntax ( Sub_Expression_1 ) || ( Sub_Expression_2 ) Compound Boolean Expressions, cont. The larger expression is true when either of the smaller expressions is true when both of the smaller expressions are true. The Java version of “or” is the inclusive or which allows either or both to be true. The exclusive or allows one or the other, but not both to be true. Negating a Boolean Expression A boolean expression can be negated using the “not” ( ! ) operator. syntax
Background image of page 3

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

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

Page1 / 17

CH3 Notes - Flow of Control Chapter 3 Objectives learn...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online