Chapter4 Notes - 1 Ch 4: Outline Boolean Expressions The if...

Info iconThis preview shows pages 1–6. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Ch 4: Outline Boolean Expressions The if Statement Comparing Data The switch Statement The while Statement Iterators The do Statement The for Statement 4.1 Flow of Control Unless specified otherwise, the order of statement execution through a method is linear: one statement after another in sequence Some programming statements allow us to decide whether or not to execute a particular statement execute a statement over and over, repetitively These decisions are based on boolean expressions (or conditions ) that evaluate to true or false The order of statement execution is called the flow of control 2 4.1 Conditional Statements A conditional statement lets us choose which statement will be executed next Therefore they are sometimes called selection statements Conditional statements give us the power to make basic decisions The Java conditional statements are the if statement if-else statement switch statement 4 4.1 The if Statement The if statement has the following syntax if ( condition ) statement ; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped. 3 5 4.1 Boolean Expressions A condition often uses one of Java's equality operators or relational operators , which all return boolean results == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between the equality operator ( == ) and the assignment operator ( = ) 6 4.1 Logical Operators Boolean expressions can also use the following logical operators ! Logical NOT && Logical AND || Logical OR They all take boolean operands and produce boolean results Logical NOT is a unary operator (it operates on one operand) Logical AND and logical OR are binary operators (each operates on two operands) 4 7 4.1 Logical NOT The logical NOT operation is also called logical negation or logical complement If some boolean condition a is true, then !a is false; if a is false, then !a is true Logical expressions can be shown using a truth table true false false true !a a 8 4.1 Logical AND and Logical OR The logical AND expression a && b is true if both a and b are true, and false otherwise The logical OR expression a || b is true if a or b or both are true, and false otherwise 5 9 4.1 Logical Operators Expressions that use logical operators can form complex conditions if (total < MAX+5 && !found) System.out.println ("Processing"); All logical operators have lower precedence than the relational operators Logical NOT has higher precedence than logical AND and logical OR 4.1 Logical Operators A truth table shows all possible true-false combinations of the terms Since && and || each have two operands, there are four possible combinations of conditions...
View Full Document

This note was uploaded on 03/31/2008 for the course IST 240 taught by Professor Tracymullen during the Spring '08 term at Pennsylvania State University, University Park.

Page1 / 48

Chapter4 Notes - 1 Ch 4: Outline Boolean Expressions The if...

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

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