{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Ch5Conditionals - Chapter 5 Decisions Conditionals Boolean...

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

View Full Document Right Arrow Icon
Chapter 5 Decisions / Conditionals
Background image of page 1

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

View Full Document Right Arrow Icon
Boolean Expressions In if (< condition > ) < condition > is a Boolean expression. A Boolean expression evaluates to either true or false. Boolean expressions are written using boolean variables and relational and logical operators.
Background image of page 2
The boolean Type George Boole (1815-1864): pioneer in the study of logic value of expression amount < 1000 is true or false. boolean type: one of these 2 truth values
Background image of page 3

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

View Full Document Right Arrow Icon
boolean Data Type George Boole (1815 - 1864) boolean variables may have only two values, true or false . You define boolean fields or boolean local variables the same way as other variables: private boolean hasMiddleName; boolean isRolling = false; boolean true false Reserved words
Background image of page 4
RELATIONAL EXPRESSION Always true or false True and false are NOT numbers, NOT objects of a class but rather a separate type of data – Boolean type.
Background image of page 5

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

View Full Document Right Arrow Icon
Relational Operators comparison of values Java Description > Greater than >= Greater than or equal < Less than <= Less than or equal == Equal != Not equal
Background image of page 6
Boolean Variables private boolean married; Set to truth value: married = input.equals("M"); Use in conditions: if (married) ... else ... if (!married) ... Also called flag Don't test Boolean variables against truth values--sign of cluelessness: if (married == true) // DON'T if (married == false) // DON'T if (married != false) // NO!!!
Background image of page 7

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

View Full Document Right Arrow Icon
if statements Allows a program to carry out different actions depending on the outcome of a condition. 2 parts Test Body of statements
Background image of page 8
if Statement if (amount <= balance) balance = balance - amount; Test Body
Background image of page 9

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

View Full Document Right Arrow Icon
Syntax 5.1. The if Statement if( condition ) statement if ( condition ) statement else statement Example: if (amount <= balance) balance = balance - amount;
Background image of page 10
if (amount <= balance) balance = balance - amount; else balance = balance - OVERDRAFT_PENALTY; Purpose: To execute a statement when a condition is true or false
Background image of page 11

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

View Full Document Right Arrow Icon
Relational Operators < , > , <= , >= , == , != is equal to is NOT equal to
Background image of page 12
Relational Operators (cont’d) Apply to numbers or char s: if ( x <= y ) ... if ( a != 0 ) ... if ( letter == ‘Y’ ) ... Do not use == or != with double s because they may have rounding errors double x = 7.0; double y = 3.5; if (x / y == 2.0) ...
Background image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}