Date post: | 07-Jul-2018 |
Category: |
Documents |
Upload: | romel-leo-alojado |
View: | 217 times |
Download: | 0 times |
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 1/12
Chapter 5The Selection Structure
• 3 important aspects of programming▫ Sequence (we have experience it)▫ Selection (is that we are going to talk about this next 2 weeks)▫ Repetition
Objectives• Write pseudocode for the selection structure• Create a owchart for the selection structure• Code the if and if/else forms of the selection structure• Write code that uses comparison operators and logical operators• Convert the contents of a char variable to uppercase or lowercase• Convert the contents of a string variable to uppercase or lowercase
Using the Selection Structure• Also called the decision structure
▫ Condition species decision Results in either a true or false answer onl
▫ !hree forms" if # if/else# and switch $or case%
Pseudocode for Selection Structures
Flowcharting the if and if/else Selection Structures
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 2/12
Coding the if and if/else Selection Structures
selection/repetition symbol
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 3/12
Selection Statement&switch $or case% statements ne't wee() *o bac( and practice on if/else and owcharts)Comparison +perators
• +ften called relational operators• ,f e'pression has multiple comparison operators at same precedence# it is evaluated from left to
right • Comparison operators are evaluated after any arithmetic operators in the e'pression
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 4/12
Comparison operators are evaluated after any arithmetic operators in the e'pressionComparison Operator Program 1: Swapping umerical !alues
!!temp is alocal variable
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 5/12
"ow the program evaluates and run
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 6/12
Comparison Operator Program #: $ispla%ing the Sum or $i&erence
'ogical Operators• 'ogical operators allow ou to combine two or more conditions into one compound condition
▫ Sometimes called (oolean operators• And/+r operators are evaluated after an arithmetic or comparison operators in an e'pression
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 7/12
!! use short-circuit evaluation
Using the )ruth )ables• !o receive a bonus# a salesperson must be rated A and he/she must sell more than -.# in
productrating 00 1A1 22 sales .
• !o send a letter to all A&rated salespeople and all 4&rated salespeoplerating 00 1A1 55 rating 00 141
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 8/12
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 9/12
• 6ispla the word 78ass9 if user enters the letter 8 $uppercase or lowercase%• 6ispla 7:ail9 if user enters anthing else
Converting a Character to Uppercase or 'owercase
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 10/12
Comparing Strings• String comparisons are case&sensitive
▫ 7es9# 7;<S9# and 7;es9 are di=erent• 4efore using a string in a comparison# convert it to uppercase or lowercase
▫ >se transform$%
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 11/12
transform*+ Function Program: Calculating Sales )a,• Calculate and displa the amount of sales ta' that a customer owes• !a' rate is 3? of purchase price• ,f purchase is made in *unter Count# ta' rate is 3)@?
Summar%• !he selection structure $decision structure% is one of the three programming structures
▫ :orms" if# if/else# and switch $or case%▫ Represented b a diamond in a owchart▫ >se the CBB if statement to code both the if and if/else forms
Condition can contain variables# constants# functions# and arithmetic# comparison# orlogical operators
• Character and string comparisons are case sensitive▫ >se toupper$%# tolower$% and transform$%
8/18/2019 cpro15.docx
http://slidepdf.com/reader/full/cpro15docx 12/12