Quadratic Parametric Programming for Portfolio Selection ...
Computer Programming TCP1224 Chapter 5 The Selection Structure.
-
Upload
mark-parrish -
Category
Documents
-
view
233 -
download
0
Transcript of Computer Programming TCP1224 Chapter 5 The Selection Structure.
Computer ProgrammingTCP1224
Chapter 5The Selection Structure
Comments
•Note that quizzes, mid-term and exam questions may not come exactly from lecture slides.
▫The text book helps! (only about 15-20 pages per chapter which you can read and understand in about 30-60 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 alocal 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 productrating == 'A' && sales > 10000
•To send a letter to all A-rated salespeople and all B-rated salespeoplerating == '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 <algorithm>
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