Title: The Selection Structure
1The Selection Structure
2Objectives
- Use the selection structure in a program
- 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 - Change the contents of a String variable to
uppercase or lowercase - Compare strings using the CompareTo() method
- Return a floating-point number when dividing two
integers
3Concept Lesson
4Using the Selection Structure
- Selection structure/decision structure allows
program to make a decision or comparison and then
select one of two paths, depending on the result
of the comparison - Condition
- Specifies the decision you are making
- Phrased so that it results in either a true or
false answer
5Selection Structures You Might Use Today
6Including the Selection Structure in Pseudocode
- True path - the instructions following the
condition - False path
- When using else, false path includes instructions
between else and end if - When else is not used, processing continues after
the end if
7Including the Selection Structure in Pseudocode
8Drawing a Flowchart of a Selection Structure
- Flowcharts - use standardized symbols to show
steps the computer must take to accomplish
programs goal - Diamond symbol (selection/repetition symbol)
used to represent both selection and repetition
9Drawing a Flowchart of a Selection Structure
10Coding the Selection Structure
- Items in square brackets () in the syntax are
optional - You create a statement block by enclosing the
statements in a set of braces () - Although it is not required to do so, it is a
good programming practice to use a comment, such
as //end if, to mark the end of each if statement
11 Syntax of the C if statement
12Coding the Selection Structure
13Comparison Operators
- Comparison (relational) operators - used to make
comparisons in a C program - Precedence numbers - indicate the order in which
the computer performs the comparisons in a C
expression - Parentheses - used to override the order of
precedence
14Comparison Operators
15Comparison Operators
16Comparison Operators
17Comparison Operators
18 Swapping Values
- Study closely the four instructions that swap the
two values - int temp 0 creates and initializes a local
variable named temp - temp first assigns the value contained in the
first variable to the temp variable - first second assigns the value contained in
the second variable to the first variable - second temp assigns the value contained in the
temp variable to the second variable
19Illustration of the Swapping Concept
20Logical Operators
- Logical/Boolean operators - And and Or
- Allow you to combine two or more conditions into
one compound condition - And logical operator - all of the conditions must
be true for the compound condition to be true - Or logical operator - only one of the conditions
must be true for the compound condition to be true
21Truth Tables for the And and Or Logical Operators
22Logical Operators
23Logical Operators
24Using Logical Operators in a Program
- Data validation - the process of verifying that
the input data is within the expected range
25Comparing Strings
- String comparisons are case-sensitive
- Yes is not the same as the string YES or
yes - ToUpper() method - converts a string to uppercase
- ToLower() method - converts a string to lowercase
- Member access operator (-gt) - used to access the
members of a class - CompareTo() method used to compare strings
26Comparing Strings
27Using the ToUpper() and CompareTo() Methods in a
Program
- ToUpper() method converts the contents of the
variable to uppercase - CompareTo() method - tells the computer to
compare the contents of the variable to each of
the three state IDs
28Comparing Strings
29Application Lesson
30Analyzing, Planning, and Desk-checking
31 Desk-checking
- Data for first desk-check
- Total calories 150
- Grams of fat 6
- Data for second desk-check
- Total calories 105
- Grams of fat 2
- Data for third desk-check
- Total calories 100
- Grams of fat -3
32Completed Desk-check Tables
33Coding the main() Function
- main() function - requires four memory locations
to store the values of its input and output items - Use the int data type for variables that store
- total calories, grams of fat, and fat calories
(these variables need to store whole numbers
only) - Use the double data type for the variable that
stores - fat percentage
34Coding the main() Function
35Coding the calcFatInfo() Function
36Data and Completed Desk-check Table for the
Program
37Completing the Health Club Program
- To open the partially completed C program
- Start Microsoft Visual Studio .NET. If necessary,
close the Start Page window - Click File on the menu bar, and then click Open
Solution. The Open Solution dialog box opens - Locate and then open the CppNet\Tut06 \T6App
Solution folder - Click T6App Solution (T6App Solution.sln) in the
list of filenames, and then click the Open button - If the T6App.cpp source file is not displayed,
right-click T6App.cpp in the Solution Explorer
window, and then click Open
38Summary
- Selection structure
- As pseudocode
- In a flowchart
- As an if and if/else statement
- Use comparison operators and logical operators
- String operations
- Uppercase to lowercase
- Compare strings using the CompareTo() method
- Return a floating-point number when dividing two
integers