DOC PREVIEW
UNF COP 2551 - Lecture Notes

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Conditionals and LoopsOutlineFlow of ControlConditional StatementsThe if StatementLogic of an if statementBoolean ExpressionsSlide 9IndentationSlide 11Logical OperatorsLogical NOTLogical AND and Logical ORSlide 15Slide 16Slide 17Short-Circuited OperatorsSlide 19The if-else StatementLogic of an if-else statementThe Coin ClassSlide 23Slide 24Indentation RevisitedBlock StatementsSlide 27Slide 28The Conditional OperatorSlide 30Slide 31Nested if StatementsSlide 33© 2004 Pearson Addison-Wesley. All rights reserved 5-2Conditionals and Loops•Now we will examine programming statements that allow us to:make decisionsrepeat processing steps in a loop•Chapter 5 focuses on:boolean expressionsconditional statementscomparing datarepetition statementsiteratorsmore drawing techniquesmore GUI components© 2004 Pearson Addison-Wesley. All rights reserved 5-3OutlineThe if Statement and ConditionsOther Conditional StatementsComparing DataThe while StatementIteratorsOther Repetition StatementsDecisions and GraphicsMore Components© 2004 Pearson Addison-Wesley. All rights reserved 5-4Flow 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© 2004 Pearson Addison-Wesley. All rights reserved 5-5Conditional 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© 2004 Pearson Addison-Wesley. All rights reserved 5-6The if Statement•The if statement has the following syntax:if ( condition ) statement;if is a Javareserved wordThe condition must be aboolean expression. It mustevaluate to either true or false.If the condition is true, the statement is executed.If it is false, the statement is skipped.© 2004 Pearson Addison-Wesley. All rights reserved 5-7Logic of an if statementconditionevaluatedstatementtruefalse© 2004 Pearson Addison-Wesley. All rights reserved 5-8Boolean 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 (=)higherprecedence© 2004 Pearson Addison-Wesley. All rights reserved 5-9The if Statement•An example of an if statement:if (sum > MAX) delta = sum - MAX;System.out.println ("The sum is " + sum);•First the condition is evaluated -- the value of sum is either greater than the value of MAX, or it is not•If the condition is true, the assignment statement is executed -- if it isn’t, it is skipped.•Either way, the call to println is executed next© 2004 Pearson Addison-Wesley. All rights reserved 5-10Indentation•The statement controlled by the if statement is indented to indicate that relationship•The use of a consistent indentation style makes a program easier to read and understand•Although it makes no difference to the compiler, proper indentation is crucial"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live."-- Martin Golding© 2004 Pearson Addison-Wesley. All rights reserved 5-11The if Statement•What do the following statements do?if (top >= MAXIMUM) top = 0;Sets top to zero if the current value of top is greater than or equal to the value of MAXIMUMif (total != stock + warehouse) inventoryError = true;Sets a flag to true if the value of total is not equal to the sum of stock and warehouse The precedence of the arithmetic operators is higher than the precedence of the equality and relational operators© 2004 Pearson Addison-Wesley. All rights reserved 5-12Logical 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)© 2004 Pearson Addison-Wesley. All rights reserved 5-13Logical 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 tablea !atrue falsefalse trueConsider a ‘condition’ something like (age > 25) It is either true or false (boolean result)© 2004 Pearson Addison-Wesley. All rights reserved 5-14Logical AND and Logical OR•The logical AND expression a && bis true if both a and b are true, and false otherwise•The logical OR expression a || bis true if a or b or both are true, and false otherwiseExamples: if ( a> 14 && b == 6)a++; if (a > 14 || b == 6)b--;© 2004 Pearson Addison-Wesley. All rights reserved 5-15Logical Operators•Expressions that use logical operators can form complex conditionsif (total < MAX+5 && !found) System.out.println ("Processing…");•All logical operators have lower precedence than the relational operators, which have lower precedence than arithmetic operators.•Logical NOT has higher precedence than logical AND and logical OR© 2004 Pearson Addison-Wesley. All rights reserved 5-16Logical 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 a and ba b a && b a || btrue true true truetrue false false truefalse true false truefalse false false false© 2004 Pearson Addison-Wesley. All rights reserved 5-17Boolean Expressions•Specific expressions can be evaluated using truth tablestotal < MAX found !found total < MAX && !foundfalse false true falsefalse true false falsetrue false true truetrue true false false© 2004 Pearson Addison-Wesley. All rights reserved 5-18Short-Circuited Operators•The processing of logical AND and logical


View Full Document

UNF COP 2551 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?