The Selection Structure - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

The Selection Structure

Description:

... function - requires four memory locations to store the values of its input and output items. Use the int data type for variables that store: ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 39
Provided by: www2Pie
Category:

less

Transcript and Presenter's Notes

Title: The Selection Structure


1
The Selection Structure
  • Tutorial 6

2
Objectives
  • 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

3
Concept Lesson
4
Using 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

5
Selection Structures You Might Use Today
6
Including 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

7
Including the Selection Structure in Pseudocode
8
Drawing 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

9
Drawing a Flowchart of a Selection Structure
10
Coding 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
12
Coding the Selection Structure
13
Comparison 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

14
Comparison Operators
15
Comparison Operators
16
Comparison Operators
17
Comparison 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

19
Illustration of the Swapping Concept
20
Logical 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

21
Truth Tables for the And and Or Logical Operators
22
Logical Operators
23
Logical Operators
24
Using Logical Operators in a Program
  • Data validation - the process of verifying that
    the input data is within the expected range

25
Comparing 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

26
Comparing Strings
27
Using 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

28
Comparing Strings
29
Application Lesson
30
Analyzing, 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

32
Completed Desk-check Tables
33
Coding 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

34
Coding the main() Function
35
Coding the calcFatInfo() Function
36
Data and Completed Desk-check Table for the
Program
37
Completing 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

38
Summary
  • 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
Write a Comment
User Comments (0)
About PowerShow.com