Computer Programming TCP1224 Chapter 5 The Selection Structure.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Programming with Microsoft Visual Basic th Edition
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.
JavaScript, Fourth Edition
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Chapter Four The Selection Structure
CIS162AD - C# Decision Statements 04_decisions.ppt.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Programming Logic and Design Sixth Edition
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Decision Structures and Boolean Logic
Using the selection structure (Unit 7) Visual Basic for Applications.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
Chapter 4 Selection Structures: Making Decisions.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Computer Programming TCP1224 Chapter 8 More On Repetition Structure.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
Tutorial 4: The Selection Structure 1 Tutorial 4 The Selection Structure.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
Random Functions Selection Structure Comparison Operators Logical Operator
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Exam 1 Review Jeff has 100 students in his MIS120 class. He is giving a 50 question exam worth 100 points. The following commands are available to you.
More on the Selection Structure
Selection—Making Decisions
Data Types, Identifiers, and Expressions
The Selection Structure
Chapter 4: The Selection Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
An Introduction to Programming with C++ Fifth Edition
Making Decisions in a Program
Data Types, Identifiers, and Expressions
Objectives After studying this chapter, you should be able to:
Chapter 8: More on the Repetition Structure
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Selection Structures: Making Decisions
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Chapter 5: The Selection Structure
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

Computer Programming TCP1224 Chapter 5 The Selection Structure

Comments Note that quizzes, mid-term and exam questions may not come exactly from lecture slides. ▫The text book helps! (only about pages per chapter which you can read and understand in about minutes per week). ▫You need to do some of your own programming in order to prepare yourself better. 2

3 important aspects of programming ▫Sequence (we have experience it) ▫Selection (is that we are going to talk about this next 2 weeks) ▫Repetition 3

Objectives Write pseudocode for the selection structure Create a flowchart for the selection structure Code the if and if/else forms of the selection structure Write code that uses comparison operators and logical operators 4

Objectives (continued) Convert the contents of a char variable to uppercase or lowercase Convert the contents of a string variable to uppercase or lowercase 5

Using the Selection Structure Also called the decision structure ▫Condition specifies decision  Results in either a true or false answer only ▫Three forms: if, if/else, and switch (or case ) 6

Using the Selection Structure (continued) 7

Pseudocode for Selection Structures 8

Flowcharting the if and if/else Selection Structures 9 selection/repetition symbol

Coding the if and if/else Selection Structures 10

11

switch (or case ) statements next week. Go back and practice on if/else and flowcharts. 12

Comparison Operators Often called relational operators If expression has multiple comparison operators at same precedence, it is evaluated from left to right Comparison operators are evaluated after any arithmetic operators in the expression 13

Comparison Operators (continued) 14

Comparison Operators (continued) 15 Comparison operators are evaluated after any arithmetic operators in the expression

Comparison Operator Program 1: Swapping Numerical Values 16

17

18 !! temp is a local variable

Comparison Operator Program 1: Swapping Numerical Values 19

Comparison Operator Program 1: Swapping Numerical Values 20 How the program evaluates and run

Comparison Operator Program 2: Displaying the Sum or Difference 21

22

23

Comparison Operator Program 2: Displaying the Sum or Difference 24

We have looked at the combination of selection if/else and comparison operators such as != and == We now look at logical operators 25

Logical Operators Logical operators allow you to combine two or more conditions into one compound condition ▫Sometimes called Boolean operators And/Or operators are evaluated after any arithmetic or comparison operators in an expression 26

Logical Operators (continued) 27

Logical Operators (continued) 28 !! use short-circuit evaluation

Using the Truth Tables To receive a bonus, a salesperson must be rated A and he/she must sell more than $10,000 in product rating == 'A' && sales > To send a letter to all A-rated salespeople and all B-rated salespeople rating == 'A' || rating == 'B' 29

Logical Operators (continued) 30

Logical Operators (continued) 31

Logical Operator Program: Calculating Gross Pay 32

33 data validation

Logical Operator Program: Calculating Gross Pay 34

Comparing Characters Display the word “Pass” if user enters the letter P (uppercase or lowercase) Display “Fail” if user enters anything else 35

36

Converting a Character to Uppercase or Lowercase 37

38

Comparing Strings String comparisons are case-sensitive ▫“yes”, “YES”, and “Yes” are different Before using a string in a comparison, convert it to uppercase or lowercase ▫Use transform() 39

40 Converting a String to Uppercase or Lowercase #include using std::transform;

transform() Function Program: Calculating Sales Tax Calculate and display the amount of sales tax that a customer owes Tax rate is 3% of purchase price If purchase is made in Gunter County, tax rate is 3.25% 41

42

transform() Function Program: Calculating Sales Tax (continued) 43

Summary The selection structure (decision structure) is one of the three programming structures ▫Forms: if, if/else, and switch (or case ) ▫Represented by a diamond in a flowchart ▫Use the C++ if statement to code both the if and if/else forms  Condition can contain variables, constants, functions, and arithmetic, comparison, or logical operators 44

Summary Character and string comparisons are case sensitive ▫Use toupper(), tolower() and transform() 45