{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Ch5Conditionals

# Ch5Conditionals - Chapter 5 Decisions Conditionals Boolean...

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

Chapter 5 Decisions / Conditionals

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

View Full Document
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.
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

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

View Full Document
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
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.

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

View Full Document
Relational Operators comparison of values Java Description > Greater than >= Greater than or equal < Less than <= Less than or equal == Equal != Not equal
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!!!

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

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

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

View Full Document
Syntax 5.1. The if Statement if( condition ) statement if ( condition ) statement else statement Example: if (amount <= balance) balance = balance - amount;
if (amount <= balance) balance = balance - amount; else balance = balance - OVERDRAFT_PENALTY; Purpose: To execute a statement when a condition is true or false

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

View Full Document
Relational Operators < , > , <= , >= , == , != is equal to is NOT equal to
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) ...

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 ]}