1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

2Object-Oriented Program Development Using C++ 3 Relational Expressions Decision-making: comparison of two numerical values Relational expression –Also.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
True or false A variable of type char can hold the value 301. ( F )
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
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)
true (any other value but zero) false (zero) expression Statement 2
Chapter 4: Control Structures I (Selection)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Fundamentals of Python: From First Programs Through Data Structures
Objectives You should be able to describe: Data Types
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
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.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
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.
TK 1914 : C++ Programming Control Structures I (Selection)
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Java Programming Fifth Edition
CNG 140 C Programming (Lecture set 3)
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
JavaScript: Control Statements.
Part II The Switch Statement
Chapter 8 JavaScript: Control Statements, Part 2
Controlling Program Flow
Presentation transcript:

1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically used as part of a selection statement A simple relational expression consists of a relational operator connecting two variable and/or constant operands

2

3

4 Logical Operators Complex conditions can be created using Boolean logical operations: –AND –OR –NOT AND operator –&& –True only if both individual expressions are true by themselves

5 Logical Operators (continued) OR operator –|| –True if either one or both of two expressions are true NOT operator –! –Changes expression to opposite state Relational operators have higher precedence than logical operators

6 Logical Operators (continued) && and || operators: –Can only be used with Boolean operands –Second operand not evaluated if evaluation of first is sufficient to determine final value of logical operation

7

8 A Numerical Accuracy Problem Tests for equality of numbers using relational operator == should be avoided –Applies to: Floating-point Double-precision Many decimal numbers cannot be represented exactly in binary using a finite number of bits Require that absolute value of difference between operands be less than an extremely small value

9 The if-else Statement Directs computer to select a sequence of instructions based on the result of a comparison Condition is evaluated first –If the value of condition is true statement1 is executed –If the value is false the statement after reserved word else is executed

10 The if-else Statement (continued) if (condition) < no semicolon here statement1; else < no semicolon here statement2;

11

12 Compound Statements Any number of single statements contained between braces Takes place of single statement Semicolon is not placed after braces that define compound statement

13

14 The Boolean Data Type Tested condition in if-else statement must always evaluate to a Boolean value Value of condition must be either true or false The boolean data type is restricted in its usage as the value of a relational expression Boolean values can be: –Displayed –Compared –Assigned

15 One-Way Selection No else expression Syntax: if (condition) statement; The statement following if (condition) is only executed if condition has a true value

16 Placement of Braces in a Compound Statement Common practice for some programmers: –Place opening brace of compound statement on same line as if and else statements if (condition) { statement1; } else { statement2; }

17 Nested if Statements One or more if-else statements can be included within either part of if-else statement Last else is with closest unpaired if –Unless braces alter default pairing Process of nesting if statements can be extended indefinitely

18 The if-else Chain Else part of an if statement contains another if-else statement Syntax: if (expression1) statement1; else if (expression2) statement2; else statement3;

19 The if-else Chain (continued) Alternate syntax: if (expression1) statement1; else if (expression2) statement2; else statement3; Used extensively in programming applications

20 The switch Statement Provides alternative to if-else chain –For cases that compare the value of an integer expression to a specific value Reserved words: –Switch –Case –Default –Break

21 Switch Syntax switch (expression) { // start of compound statement case value-1: < terminated with a colon statement1; statement2; break; case value-2: < terminated with a colon statementm; statementn; break; default: < terminated with a colon statementaa; statementbb; } // end of switch and compound statement

22 The switch Statement (continued) Once entry point has been located: –All further case evaluations are ignored –Execution continues through end of compound statement –Unless break statement is encountered The break statement causes an immediate exit from the switch statement

23 Program Design and Development: Introduction to UML Explicit design –Should always be undertaken before coding begins –Referred to as program modeling Unified Modeling Language (UML) –Program modeling language with its own set of rules and notations –Not part of Java language

24 Program Design and Development: Introduction to UML (continued) Must understand and specify: –What the objects in the system are –What can happen to these objects –When it can happen Each item is addressed by a number of individual and separate views and diagrams

25 UML Diagram Types Class Object State Sequence Activity Use-case Component Deployment Collaboration

26

27 Class and Object Diagrams Class diagrams –Used to describe classes and their relationships Object diagrams –Describe specific objects and relationships Attribute –Characteristic that each object in class must have Attribute type –Primitive type –Class type

28 Class and Object Diagrams (continued) Visibility –Defines where the attribute can be seen –Whether the attribute can be used in other classes or is restricted to the class defining it Operations –Transformations that can be applied to attributes –Ultimately coded as Java methods

29

30

31 Relationships The three basic relationships include: –Association –Aggregation –Generalization

32

33

34

35 Application: A Date Class Need to develop a date class Extremely important in financial programs Design stages: –Identify type of objects –Decide how Date object is internally represented Identify attributes –Identify initial set of operations

36

37 User Interfaces, Definitions, and Information Hiding User interface consists of: –Class’s public member methods’ header lines –Supporting comments Definition consists of: –Class’s definition section –Class’s private data members Information hiding refers to the principle that how a class is internally constructed is not relevant to any programmer who wishes to use the class

38 Simplifying the Code Once a program is working, simplify the code: –Remove print statements –Use methods such as setDate() to initialize values in constructors

39 Adding Class Methods Leap Year Algorithm If the year is divisible by 4 with a remainder of 0 AND the year is divisible by 100 with a nonzero remainder OR the year is divisible by 400 with no remainder then the year is a leap year Else the year is not a leap year EndIf

40 Common Programming Errors Assuming the if-else statement is selecting an incorrect choice when the problem is really the values being tested Using nested if statements without including braces to clearly indicate the desired structure Mistakenly using the assignment operator = instead of the relational operator == when comparing Boolean data Forgetting to use a break statement to close off a case within a switch statement

41 Summary Relational expressions: –Referred to as conditions –Used to compare operands if-else statements: –Used to select between two alternative statements –Based on a value of a relational or logical expression –Can contain other if-else statements

42 Summary (continued) A compound statement consists of a number of individual statements enclosed within the brace pair { and } The switch statement is a multiway selection statement