Unformatted text preview:

Chapter 3Flow of ControlBranching with an if-else StatementCompound StatementsSlide 5Omitting the else PartNested StatementsMultiway if-else StatementsMultiway if-else StatementThe switch StatementSlide 11Slide 12Slide 13The switch StatementThe Conditional OperatorBoolean ExpressionsJava Comparison OperatorsPitfall: Using == with StringsLexicographic and Alphabetical OrderBuilding Boolean ExpressionsEvaluating Boolean ExpressionsTruth TablesShort-Circuit and Complete EvaluationSlide 24Precedence and Associativity RulesSlide 26Loopswhile statementwhile Syntaxdo-while Statementdo-while SyntaxThe for StatementThe for Statement SyntaxSemantics of the for Statementfor Statement Syntax and Alternate SemanticsSlide 36The Comma in for StatementsInfinite LoopsNested LoopsThe break and continue StatementsExample of Using BreakExample of Using ContinueOutput for the example programsThe exit StatementLoop BugsTracing VariablesGeneral Debugging TechniquesDebugging Example (1 of 9)Debugging Example (2 of 9)Debugging Example (3 of 9)Debugging Example (4 of 9)Debugging Example (5 of 9)Debugging Example (6 of 9)Debugging Example (7 of 9)Debugging Example (8 of 9)Debugging Example (9 of 9)Preventive CodingThe Use of Sentinel ValueUsing escape sequence "Empty loop bodyReviewString inputSlide 63Slide 64Slide 65String methodsMore string methodsSlide 68Penny guessing gameSlide 70Slide 71Slide 72Slide 73Logical operators (AND, OR, NOT)Boolean type and variablesExercise ProblemMore Exercise ProblemSummationFactorialMean (average)Chapter 3Flow of ControlCopyright © 2008 Pearson Addison-Wesley. All rights reservedFlow of Control•As in most programming languages, flow of control in Java refers to its branching and looping mechanisms•Java has several branching mechanisms: if-else, if, and switch statements•Java has three types of loop statements: the while, do-while, and for statements•Most branching and looping statements are controlled by Boolean expressions–A Boolean expression evaluates to either true or false–The primitive type boolean may only take the values true or false3-2Copyright © 2008 Pearson Addison-Wesley. All rights reservedBranching with an if-else Statement•An if-else statement chooses between two alternative statements based on the value of a Boolean expressionif (Boolean_Expression) Yes_Statementelse No_Statement–The Boolean_Expression must be enclosed in parentheses–If the Boolean_Expression is true, then the Yes_Statement is executed–If the Boolean_Expression is false, then the No_Statement is executed3-3Copyright © 2008 Pearson Addison-Wesley. All rights reservedCompound Statements•Each Yes_Statement and No_Statement branch of an if-else can be a made up of a single statement or many statements•Compound Statement: A branch statement that is made up of a list of statements –A compound statement must always be enclosed in a pair of braces ({ })–A compound statement can be used anywhere that a single statement can be used3-4Copyright © 2008 Pearson Addison-Wesley. All rights reservedCompound Statementsif (myScore > your Score) { System.out.println("I win!"); wager = wager + 100;}else{ System.out.println ("I wish these were golf scores."); wager = 0;}3-5Copyright © 2008 Pearson Addison-Wesley. All rights reservedOmitting the else Part•The else part may be omitted to obtain what is often called an if statementif (Boolean_Expression) Action_Statement–If the Boolean_Expression is true, then the Action_Statement is executed–The Action_Statement can be a single or compound statement–Otherwise, nothing happens, and the program goes on to the next statementif (weight > ideal) calorieIntake = calorieIntake – 500;3-6Copyright © 2008 Pearson Addison-Wesley. All rights reservedNested Statements•if-else statements and if statements both contain smaller statements within them–For example, single or compound statements•In fact, any statement at all can be used as a subpart of an if-else or if statement, including another if-else or if statement–Each level of a nested if-else or if should be indented further than the previous level–Exception: multiway if-else statements3-7Copyright © 2008 Pearson Addison-Wesley. All rights reservedMultiway if-else Statements•The multiway if-else statement is simply a normal if-else statement that nests another if-else statement at every else branch–It is indented differently from other nested statements–All of the Boolean_Expressions are aligned with one another, and their corresponding actions are also aligned with one another–The Boolean_Expressions are evaluated in order until one that evaluates to true is found–The final else is optional3-8Copyright © 2008 Pearson Addison-Wesley. All rights reservedMultiway if-else Statementif (Boolean_Expression) Statement_1else if (Boolean_Expression) Statement_2 else if (Boolean_Expression_n) Statement_nelse Statement_For_All_Other_Possibilities. . .3-9Copyright © 2008 Pearson Addison-Wesley. All rights reservedThe switch Statement•The switch statement is the only other kind of Java statement that implements multiway branching–When a switch statement is evaluated, one of a number of different branches is executed–The choice of which branch to execute is determined by a controlling expression enclosed in parentheses after the keyword switch•The controlling expression must evaluate to a char, int, short, or byte3-10Copyright © 2008 Pearson Addison-Wesley. All rights reservedThe switch Statement•Each branch statement in a switch statement starts with the reserved word case, followed by a constant called a case label, followed by a colon, and then a sequence of statements–Each case label must be of the same type as the controlling expression–Case labels need not be listed in order or span a complete interval, but each one may appear only once–Each sequence of statements may be followed by a break statement ( break;)3-11Copyright © 2008 Pearson Addison-Wesley. All rights reservedThe switch Statement•There can also be a section labeled default:–The default section is optional, and is usually last–Even if the case labels cover all possible outcomes in a given switch statement, it is still a good practice to include a default section•It can be used to output an error message, for example•When the controlling expression is evaluated, the code for the case label whose value matches the controlling expression


View Full Document

SJU CSC 3405 - 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?