Copyright © 2012 Pearson Education, Inc. Chapter 3 Control Structures: Selection.

Slides:



Advertisements
Similar presentations
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Advertisements

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.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
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 (!)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
C++ for Engineers and Scientists Third Edition
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
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–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
C++ Programming: From Problem Analysis to Program Design, Fourth 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.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Selection (also known as Branching) Jumail Bin Taliba by
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
The Selection Structure
Engineering Problem Solving with C++, Etter/Ingber
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Using Decision Structures
Presentation transcript:

Copyright © 2012 Pearson Education, Inc. Chapter 3 Control Structures: Selection

Copyright © 2012 Pearson Education, Inc. Outline Objectives 1.Algorithm Development 2.Structured Programming 3.Conditional Expressions 4.Selection Statements: if Statement 5.Numerical Technique: Linear Interpolation 6.Problem Solving Applied: Freezing Temperature of Seawater 7.Selection Statements: switch Statement 8.Defining Operators for Programmer-defined data types

Copyright © 2012 Pearson Education, Inc. Objectives Develop problem-solving solutions in C++ containing: Conditional expressions that evaluate to true of false. Selection structures that allow us to provide alternative paths in a program. Algorithm development and descriptions of algorithms using flowcharts and pseudocode.

Algorithm Development Copyright © 2012 Pearson Education, Inc.

Algorithm Development An algorithm is a sequence of steps for solving a problem. Engineering problem solutions to real world problems require complex algorithms. Development of a good algorithm increases the quality and maintainability of a solution, and reduces the overall time required to implement a correct solution. Copyright © 2012 Pearson Education, Inc.

Top-Down Design Top-down design begins with a "big picture" description of a problem solution in sequential steps. The sequential steps are refined until the steps are detailed enough to translate to language statements. The refined steps, or algorithm, can be described using pseudo code or flowcharts. Copyright © 2012 Pearson Education, Inc.

Pseudocode Notation and Flowchart Symbols

Structured Programming Copyright © 2012 Pearson Education, Inc.

Structured Programming A structured program is written using simple control structures, including: –Sequence – steps are performed one after another. –Selection – one set of statements is executed if a given condition is true, a different set of statements, or no statements at all, is executed if the condition is false. –Repetition –A set of statements is executed repeatedly as long as a given condition is true. Copyright © 2012 Pearson Education, Inc.

Structured Programming Sequence Selection Repetition Copyright © 2012 Pearson Education, Inc. ? truefalse ? true false ? => conditional expression

Conditional Expressions Copyright © 2012 Pearson Education, Inc.

Conditional Expressions A conditional expression is a Boolean expression that evaluates to true or false. Selection structures and repetition structures rely on conditional expressions. Relational operators and logical operators are used to form conditional expressions. Copyright © 2012 Pearson Education, Inc.

Relational Operators ==equality !=non equality <less than >greater than <=less than equal to >=greater than equal to Copyright © 2012 Pearson Education, Inc.

Logical Operators !not &&and ||or Copyright © 2012 Pearson Education, Inc.

Logical Operators Truth table for conditional expressions Copyright © 2012 Pearson Education, Inc. ABA&&BA||B!A!B false true falsetruefalsetrue false truefalse truefalsetrue false

Short Circuiting C++ only evaluates as much of an expression as necessary to evaluate it. –E.g. if A is false, A && B is always false, regardless of the value of B. –E.g. if A is true, A || B is always true, regardless of the value of B. Copyright © 2012 Pearson Education, Inc.

Operator Precedence Copyright © 2012 Pearson Education, Inc. PrecedenceOperatorAssociativity 1()Innermost First 2Unary operators: ! (type)Right to left 3* / %Left to right 4+ -Left to right 5 =>Left to right 6== !=Left to right 7&&Left to right 8||Left to right 9= += -= *= /= %=Right to left

Selection Statements: if Statement Copyright © 2012 Pearson Education, Inc.

The if Statement Copyright © 2012 Pearson Education, Inc. Simplified Syntax if (boolean_expression) statement; if (boolean_expression) { statement1; … statement_n; } Examples if (x>0) ++k; //statement executed iff x>0 if (x>0) { //statement block executed iff x>0 x = sqrt(x); ++k; }

The if-else Statement Copyright © 2012 Pearson Education, Inc. Syntax if (boolean_expression) statement; [else statement;] if (boolean_expression) { … } [else { … } ] Example if (x>0) { //statement block executed iff x>0 } else { //statement block executed iff x<=0 }

Avoiding Bugs To avoid confusion and possible errors when using if/else statements, you should use {} to clearly define statement blocks. Do not use == with real values –Instead of x==3.14, use fabs(x-3.14)< Do not confuse relational equality and assignment operators! Copyright © 2012 Pearson Education, Inc.

Numerical Technique: Linear Interpolation Copyright © 2012 Pearson Education, Inc.

Why Linear Interpolation? Collecting data in a computer usually results in a collection of samples. Often, we are interested in what happens ‘in between’ the sample points. –i.e. we need to interpolate between the samples. Linear and cubic spline interpolation common techniques. Copyright © 2012 Pearson Education, Inc.

Interpolation Example Copyright © 2012 Pearson Education, Inc.

Linear Interpolation Copyright © 2012 Pearson Education, Inc. f(a), a, f(c), and c are known. We wish to estimate f(b) where a < b < c. Linear interpolation is a straight-line approximation between the known points.

Problem Solving Applied: Freezing Temperature of Seawater Copyright © 2012 Pearson Education, Inc.

Selection Statements: switch Statement Copyright © 2012 Pearson Education, Inc.

The switch Statement Copyright © 2012 Pearson Education, Inc. Syntax switch (control_expression) { case constant: statement(s); break; [ case constant: statement(s); break; […] ] [ default: statement(s); ] } Control expression must be an integral type (e.g. char, int, etc…) Break statements are not required; without a break statement execution ‘runs through’ other case labels.

Example char ch; int ecount=0, vowels=0, other=0; cin.get(ch); while(!cin.eof()) { switch(ch) { case ‘e’: ecount++; case ‘a’: case ‘i’: case ‘o’: case ‘u’: vowels++; break; default:other++; } //end switch cin.get(ch); } //end while cout << ecount << ‘,’ << vowels << ‘,’ << other << endl Copyright © 2012 Pearson Education, Inc.

Defining Operators for Programmer-defined data types Copyright © 2012 Pearson Education, Inc.

Operators and Programmer- defined Types Only the assignment operation is defined for programmer-defined types. The programmer may overload operators by providing a new definition of an existing operator to work with programmer-defined types. –Operators may not be overloaded for standard C++ datatypes. Copyright © 2012 Pearson Education, Inc.