Chapter 5 Control Statements: Part 2; Logical Operators

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
JavaScript, Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 The initialization, loop-continuation condition and increment portions of a for statement can contain arithmetic expressions.  For example,
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Introduction to Computer Programming
Appendix C Control Statements
Chapter 4 Control Statements: Part 2
Chapter 4 – C Program Control
Chapter 4 C Program Control Part I
Chapter 5- Control Structures: Part 2
Chapter 4 C Program Control Part II
Chapter 5 Control Statements: switch Statement
Control Statements: Part 2
Topic 3, Selection Statements
Control Structures: Part 2
Chapter 4 - Program Control
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Java Programming: Guided Learning with Early Objects
Formulating Algorithms, Repetition Control Statements
Control Statements: Part 2; Logical Operators
Java Programming: Guided Learning with Early Objects
Chapter 5- part 2 Control Statements: Loops 2
Chapter 5 – part 1 Control Statements: switch Statement
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Control Statements: Part 2
MSIS 655 Advanced Business Applications Programming
Chapter 5, Control Statements: Part 2; Logical Operators
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4 - Program Control
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Chapter 5 Control Statements: Loops 2
Chapter 6 Control Statements: Part 2
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 - Program Control
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Chapter 5 Control Statements: Part 2; Logical Operators Java How to Program, 11/e Questions? E-mail paul.deitel@deitel.com © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Flow Control Already have seen sequenced flow-control such as if allows a program to do something conditionally if-else allows a program to have two cases, the conditional case and the typical case. if-else-if-else-if-else Multiway if-else statement allows us to handle a series of possible values. Nested if-else allows us to use a variable to decide how to process another variable. Already have seen looping flow-control such as while loop allows us to iterate using a counter or a sentinel (terminating) value do-while allows us to process a loop, but making sure to do at least one case. for-loop allows us to iterate using a counter with control over updates. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Examples Using the for Statement (Cont.) Compound interest application A person invests $1,000 in a savings account yielding 5% interest. Assuming that all the interest is left on deposit, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula to determine the amounts: a = p (1 + r)n where p is the original amount invested (i.e., the principal) r is the annual interest rate (e.g., use 0.05 for 5%) n is the number of years a is the amount on deposit at the end of the nth year. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Examples Using the for Statement (Cont.) The solution to this problem (Fig. 5.6) involves a loop that performs the indicated calculation for each of the 10 years the money remains on deposit. Java treats floating-point constants like 1000.0 and 0.05 as type double. Java treats whole-number constants like 7 and -22 as type int. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Math library functions – static class methods Java library functions are often static methods on classes, E.g. System Math static methods can be invoked without an object. Java does not include an exponentiation operator— Math class static method pow can be used for raising a value to a power. Call a static method using the class name, a dot (.) and the method name, as in ClassName.methodName(arguments) Math.pow(x, y) calculates the value of x raised to the yth power. The method receives two double arguments and returns a double value. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Additional Output formatting - Internationalization In %,20.2f, the comma (,) formatting flag indicates that the floating- point value should be output with a grouping separator. The 20 specifies field width, the .2 specifies the formatted number’s precision The Separator used is specific to the user’s locale (i.e., country). Symbol for the “decimal separator” In German: „EUR 999,50“ or „EUR 2,5 Millionen“ In English: “EUR 999.50” or “EUR 2.5 million” Symbol for the “thousands separator” In German: „US-$ 400.456,50“ In English: “US-$ 400,456.50” © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

End-of-File Indicator The end-of-file indicator is a system-dependent keystroke combination which the user enters to indicate that there is no more data to input. On UNIX/Linux/Mac OS X systems, end-of-file is entered by typing the sequence <Ctrl> d This notation means to simultaneously press both the Ctrl key and the d key. On Windows systems, end-of-file can be entered by typing <Ctrl> z On some systems, you must press Enter after typing the end-of-file key sequence. Windows typically displays the characters ^Z on the screen when the end-of-file indicator is typed. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java String Concatentation Assembling Strings with String Concatenation String concatenation Assemble String objects into larger strings with operators + or +=. When both operands of operator + are Strings, operator + creates a new String object characters of the right operand are placed at the end of those in the left operand Every primitive value and object in Java can be represented as a String. When one of the + operator’s operands is a String, the other is converted to a String, then the two are concatenated. If a boolean is concatenated with a String, the boolean is converted to the String "true" or "false". All objects have a toString method that returns a String representation of the object. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java String Concatentation Confusing the operator + in String concatenation with + for addition. Java evaluates most binary operators left to right, if a String is to the left of the + then the next operand will be promoted to a String if a numeric type is to the left of the + then the next operand will be added arithmetically. Let’s say y=5, if it’s found in the following expression “y + 2 = “ + y + 2 is compiled to “y + 2 = 52” and not “y + 2 = 7” “y + 2 = “ + (y + 2) is compiled to “y + 2 = 7”. Why? © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java Constants are final Java constants are class variables defined with the word static We do this because a constant will not change, so there is no need for each instance to have it’s own copy of the constant. Java constants are defined with the keyword final which is used to specify that the value cannot change. public static final String CONSTANT = “CONSTANT”; Java has a feature called enum types—enum type constants can also be used in case labels. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Strings in switch Statements Strings can be used as controlling expressions in switch statements, and String literals can be used in case labels. App requirements: You’ve been hired by an auto insurance company that serves these northeast states—Connecticut, Maine, Massachusetts, New Hampshire, New Jersey, New York, Pennsylvania, Rhode Island and Vermont. The company would like you to create a program that produces a report indicating for each of their auto insurance policies whether the policy is held in a state with “no-fault” auto insurance—Massachusetts, New Jersey, New York and Pennsylvania. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Class AutoPolicy Case Study Class AutoPolicy represents an auto insurance policy. The class contains: int instance variable accountNumber to store the policy’s account number String instance variable makeAndModel to store the car’s make and model (such as a "Toyota Camry") String instance variable state to store a two-character state abbreviation representing the state in which the policy is held (e.g., "MA" for Massachusetts) a constructor that initializes the class’s instance variables © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Class AutoPolicyTest Case Study Class AutoPolicyTest (Fig. 5.12) creates two AutoPolicy objects. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java break Statement The break statement, when executed in a while, for, do…while or switch, causes immediate exit from that statement. Execution continues with the first statement after the control statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java continue Statement The continue statement, when executed in a while, for or do…while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. In while and do…while statements, the program evaluates the loop- continuation test immediately after the continue statement executes. In a for statement, the increment expression executes, then the program evaluates the loop-continuation test. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java Logical Operators Java’s logical operators enable you to form more complex conditions by combining simple conditions. The logical operators are && (conditional AND) || (conditional OR) & (boolean logical AND) | (boolean logical inclusive OR) ^ (boolean logical exclusive OR) ! (logical NOT). [Note: The &, | and ^ operators are also bitwise operators when they are applied to integral operands.] © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java Logical And Operator The && (conditional AND) operator ensures that two conditions are both true before choosing a certain path of execution. The table in Fig. 5.15 summarizes the && operator. The table shows all four possible combinations of false and true values for expression1 and expression2. Such tables are called truth tables. Java evaluates to false or true all expressions that include relational operators, equality operators or logical operators. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java Logical Or Operator The || (conditional OR) operator ensures that either or both of two conditions are true before choosing a certain path of execution. Figure 5.16 is a truth table for operator conditional OR (||). Operator && has a higher precedence than operator ||. Both operators associate from left to right. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Short Circuit Evaluation The parts of an expression containing && or || operators are evaluated only until it’s known whether the condition is true or false. This feature of conditional AND and conditional OR expressions is called short-circuit evaluation. This is the same in C++. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java Boolean Logical Operators – & and | Always evaluate both Operands The boolean logical AND (&) and boolean logical inclusive OR (|) operators are identical to the && and || operators, except that the & and | operators always evaluate both of their operands (i.e., they do not perform short-circuit evaluation). This is useful if the right operand has a required side effect—a modification of a variable’s value. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java ^ Logical Exclusive Or Operator A simple condition containing the boolean logical exclusive OR (^) operator is true if and only if one of its operands is true and the other is false. If both are true or both are false, the entire condition is false. Figure 5.17 is a truth table for the boolean logical exclusive OR operator (^). This operator is guaranteed to evaluate both of its operands. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java ! Logical Not Operator The ! (logical NOT, also called logical negation or logical complement) operator “reverses” the meaning of a condition. The logical negation operator is a unary operator that has only one condition as an operand. The logical negation operator is placed before a condition to choose a path of execution if the original condition (without the logical negation operator) is false. In most cases, you can avoid using logical negation by expressing the condition differently with an appropriate relational or equality operator. Figure 5.18 is a truth table for the logical negation operator. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Java Output of boolean Figure 5.19 produces the truth tables discussed in this section. The %b format specifier displays the word “true” or the word “false” based on a boolean expression’s value. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

UML Structured Programming Summary Figure 5.21 uses UML activity diagrams to summarize Java’s control statements. Connecting control statements in sequence to form structured programs is simple. The final state of one control statement is connected to the initial state of the next—that is, the control statements are placed one after another in a program in sequence. We call this control-statement stacking. The rules for forming structured programs also allow for control statements to be nested. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

Structured Programming Summary (Cont.) Structured programming promotes simplicity. Bohm and Jacopini: Only three forms of control are needed to implement an algorithm: sequence selection iteration The sequence structure is trivial. Simply list the statements to execute in the order in which they should execute. © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

(Optional) GUI and Graphics Case Study: Drawing Rectangles and Ovals © Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.