Ch5Conditionals

Ch5Conditionals - Chapter 5 Decisions / Conditionals...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Chapter 5 Decisions / Conditionals 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 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. 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!!! 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 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 Relational Operators < , > , <= , >= , == , != is equal to is NOT equal to Relational Operators (contd) 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) ... May be false! Relational Operators (contd) Be careful using == and != with objects (e.g., String s): they compare references (addresses) rather than values (the contents) String cmd = Keyboard.readString(); if ( cmd == "Help" ) ......
View Full Document

This note was uploaded on 02/28/2010 for the course COMP COMP taught by Professor Huye during the Spring '10 term at Eastern Oregon.

Page1 / 56

Ch5Conditionals - Chapter 5 Decisions / Conditionals...

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

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