2Object-Oriented Program Development Using C++ 3 Relational Expressions Decision-making: comparison of two numerical values Relational expression –Also.

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
JavaScript, Third Edition
C++ for Engineers and Scientists Third Edition
CIS162AD - C# Decision Statements 04_decisions.ppt.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Flow of Control Part 1: Selection
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CPS120: Introduction to Computer Science Decision Making in Programs.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
The Selection Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
Programming Fundamentals
Topics The if Statement The if-else Statement Comparing Strings
Expression Review what is the result and type of these expressions?
Introduction to C++ Programming
Structured Program
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Using C++ Arithmetic Operators and Control Structures
Structural Program Development: If, If-Else
Presentation transcript:

2Object-Oriented Program Development Using C++

3 Relational Expressions Decision-making: comparison of two numerical values Relational expression –Also known as a condition –Evaluates to 1 (true) or 0 (false) –Simple type: two operands and relational operator Six relational operators:, =, = =, != Char type coerced to int for comparison Strings compared at character level

4Object-Oriented Program Development Using C++ Figure 5-1 Anatomy of a Simple Relational Expression

5Object-Oriented Program Development Using C++ Table 5-1 Relational Operators for Primitive Data Types

6Object-Oriented Program Development Using C++ Logical Operators Complex expressions –Comprised of simple relational expressions –Logical connectors required AND ( && ), OR ( | | ), NOT (!) Precedence –AND and OR lower than relational operators –NOT (unary) higher than relational operators Associativity –AND and OR: left to right –NOT: right to left

7Object-Oriented Program Development Using C++ Table 5-2 Operator Precedence

8Object-Oriented Program Development Using C++ Table 5-3 Equivalent Expressions

9Object-Oriented Program Development Using C++ A Numerical Accuracy Problem Caveat: equality comparison of floating-point types –Avoid use of equality operator –Computer representation slightly inaccurate Work around problem –If possible, replace floating-point data with integers –If not, use alternative syntax abs (operandOne - operandTwo) < EPSILON EPSILON is very small value such as

10Object-Oriented Program Development Using C++ The if-else Statement if-else statement: fundamental selection structure Purpose: alter instruction sequence Syntax: if (expression) statement1; else statement2; Expression –Relational expression –May consist of single variable, such as type bool

11Object-Oriented Program Development Using C++ Figure 5-2 The if-else Flowchart

12Object-Oriented Program Development Using C++ Compound Statements Compound statement –Sequence of statements enclosed by braces –Supports construction of complex selection structures Syntax: if (expression){ //sequence of statements } else{ //sequence of statements }

13Object-Oriented Program Development Using C++ Block Scope Code block –Set of compound statements –May be nested Variable scope –Variable meaningful between closing braces –Name conflict resolved by location Inner block takes precedence over outer Compiler seeks declaration moving inside out

14Object-Oriented Program Development Using C++ One-Way Selection One-way selection: excludes else portion Syntax: if (expression) statement; // code block might follow Non-zero expression triggers statement execution

15Object-Oriented Program Development Using C++ Figure 5-3 Flowchart for the One-Way if Statement

16Object-Oriented Program Development Using C++ Problems Associated with the if-else Statement Semantic problems –Logical form –Correct by reviewing original design Syntax problems –Misuse of assignment operator (=) in expression Assigns value to operand Non-zero assignments always evaluate to true –Use equality operator (= =) for comparisons

17Object-Oriented Program Development Using C++ Nested if Statements Selection structures may be nested –if or if-else statements nest in either (or both) parts of larger if-else statement –Nesting may be deeper than one level Syntax caveat –Use braces to define logical unit –Misused (or missing) braces may cause fatal logical error

18Object-Oriented Program Development Using C++ Figure 5-4a The if-else Nested Within the if Part

19Object-Oriented Program Development Using C++ Figure 5-4b The if-else Nested Within the else Part

20Object-Oriented Program Development Using C++ The if-else chain if-else chain: useful, readable form of nesting Syntax: if (expression1) statement1; // may be code block else if (expression2) statement2; // may be code block else statement3; // may be code block Additional else-if components may be added

21Object-Oriented Program Development Using C++ The Switch Statement Switch statement –Variation on chained if-else statement –Control “switches” to case based on condition –Caveat: condition evaluates to an integer Cases may include complex structures Break statement follows each case Default statement is optional

22Object-Oriented Program Development Using C++ Figure 5-5 The Expression Determines an Entry Point

23Object-Oriented Program Development Using C++ Program Design and Development: Introduction to UML Think and plan before coding –Primary concern: classes and objects needed Uniform Modeling Language (UML) –Supports object-oriented design –Set of rules and diagrams Focus on four UML diagrams –Class, object, state, and sequence –Analogy to specialized blueprints

24Object-Oriented Program Development Using C++ Figure 5-7 Basic UML Symbols and Notation

25Object-Oriented Program Development Using C++ Class and Object Diagrams Commonality of class and object diagrams –Both employ rectangular containers –Names, attributes, behaviors found in both Chief differences –Class diagram: describes classes and relationships –Object diagram: describes objects and relationships –Class at higher level of abstraction –One class can generate many particular objects

26Object-Oriented Program Development Using C++ Figure 5-6 A Class and Object Representation

27Object-Oriented Program Development Using C++ Figure 5-8 Including Attributes in UML Class and Object Diagrams

28Object-Oriented Program Development Using C++ Class and Object Diagrams (continued) Two aspects to class attributes –Data type: may be primitive or class –Visibility: where variable may be seen (or used) Plus (+) sign indicates public Minus (-) sign indicates private No sign for protected status Operations –Become methods that transform attributes –Follow attribute sign convention for visibility

29Object-Oriented Program Development Using C++ Figure 5-9 A Class with Attributes

30Object-Oriented Program Development Using C++ Figure 5-10 Including Operations in Class Diagrams

31Object-Oriented Program Development Using C++ Relationships Three basic relationships –Association, aggregation, generalization Association –Signified by phrases such as “works for”, “has a” –Indicated by straight line connecting classes/ objects –Multiplicity Numerical relationship between objects/classes Quantities: 0, 1, many, unlimited

32Object-Oriented Program Development Using C++ Figure 5-11 An Association

33Object-Oriented Program Development Using C++ Table 5-4 UML Association Notation

34Object-Oriented Program Development Using C++ Relationships (continued) Aggregation –One class/object consists of other classes/objects –Visualize as relation of whole to parts –Symbolized by diamond Generalization –Relationship between class and its refinement –Ford Taurus is a type of automobile

35Object-Oriented Program Development Using C++ Figure 5-12 Single-Level Aggregation

36Object-Oriented Program Development Using C++ Figure 5-13 Another Single-Level Aggregation

37Object-Oriented Program Development Using C++ Figure 5-14 Multi-Level Aggregation

38Object-Oriented Program Development Using C++ Figure 5-15 A Generalization Relationship

39Object-Oriented Program Development Using C++ Application: A Date Class Stage one: identify and name objects Stage two –Define attributes Month, day, and year Integer data types Stage three –Create object diagram –Object diagram shows assignment of values

40Object-Oriented Program Development Using C++ Figure 5-16 Initial Date Class Diagram

41Object-Oriented Program Development Using C++ Figure 5-17 First Refinement-Date Class Diagram

42Object-Oriented Program Development Using C++ Figure 5-18 A Date Object Diagram

43Object-Oriented Program Development Using C++ Application: A Date Class (continued) Stage four –Identify operations that become methods –Basic operations: constructor, mutator, accessor –Additional operations: queries with comparisons Stage five –Construct second refinement of class diagram –Name, attributes, operations detailed –Visibility denoted

44Object-Oriented Program Development Using C++ Table 5-5 Required Operations for the Date Class

45Object-Oriented Program Development Using C++ Figure 5-19 Second Refinement-Date Class Diagram

46Object-Oriented Program Development Using C++ Explanation of the Basic Date Class Default constructor –Initializes month, day, year –cout object echo prints default data Overloaded constructor –Initializes Date object with parameterized interface –cout object echo prints default data setDate( ): almost identical to overloaded constructor showDate( ): accessor manipulates output stream

47Object-Oriented Program Development Using C++ Using the Basic Date Class Constructors instantiate two Date objects Syntax of object declaration –If default constructor used, follow primitive form –If overloaded constructor used, supply arguments Accessors retrieve data in attributes Output modified according to current values

48Object-Oriented Program Development Using C++ Simplifying the Code Opportunities for optimization exist Call internal methods when possible –Eliminate redundant code –Do not reinvent your own wheel –Example: call setDate( ) in constructors Dealing with other redundancies –Target common or repeated actions –Fold action into method

49Object-Oriented Program Development Using C++ Adding Additional Class Methods Virtue of OO programming: scalability Add isLeapYear ( ) –Based on leap year algorithm –Returns a Boolean value Add dayOfWeek ( ) –Based on Zeller’s algorithm –Returns an integer value Include appropriate declarations, definition, visibility

50Object-Oriented Program Development Using C++ A Closer Look at Program Testing Added complexity increases likelihood of errors Selection structures introduce new control paths –Ideally, programmer tests each path –Not physically possible –Growth of test paths: 2 n –n corresponds to number of if-else statements Choose critical elements to test –Legal and limiting input values

51Object-Oriented Program Development Using C++ Summary Relational expression (condition) evaluates to 1 (true) or 0 (false) Relational operators:, =, = =, != Logical connectors: AND (&&), OR (| |), NOT (!) Basic selection structure: if-else statement Compound statement: set enclosed by braces

52Object-Oriented Program Development Using C++ Summary (continued) Selection variations: nesting, chained if-else, switch Uniform Modeling Language (UML): OO design rules/templates Basic UML diagrams: class, object, state, sequence Use UML tools to construct/implement Date class Testing: selectivity avoids combinatorial explosion