Add your company slogan Skill Area 306: Develop and Implement Computer Program Computer Programming (YPG) LOGO
Skill Area 306.2: Produce Structured Program 306.2.1 Write Algorithm 306.2.2 Apply appropriate program design tools 306.2.3 Apply logical and operational operators 306.2.4 Apply control structures 306.2.5 Write program 306.2.6 Tests and debug program
The Selection Structure Selection structure (or decision structure): Used to select a path to take based on the outcome of a decision or comparison Condition: The decision to be made Results in a Boolean (True or False) answer Four forms of selection structure: If If/Else If/ElseIf/Else Case
The Selection Structure (continued) Figure 306.2-1: Selection structures you might use today
Writing Pseudocode for the If and If/Else Selection Structures If selection structure: contains one set of instructions to process when the condition is true If/Else selection structure: Contains two sets of instructions One set is processed when the condition is true The other set is processed when the condition is false True path: path to follow when condition is true False path: path to follow when condition is false
Figure 306.2-2: Examples of the If and If/Else selection structures written in pseudocode
Flowcharting the If and If/Else Selection Structures Flowchart uses standardized symbols to show the steps a computer must take Selection/repetition symbol: Diamond shape Represents both selection and repetition structures One flowline entering and two flowlines leaving
Figure 306.2-3: Examples of the If and If/Else selection structures in flowchart form
Coding the If and If/Else Selection Structures If Then Else statement: used to code the If and If/Else selection structures Else clause: an optional part of the If statement Condition must be a Boolean expression Must evaluate to either True or False Statement block: set of statements terminated by an Else or End If If Then Else statement can contain variables, literal constants, named constants, properties, methods, arithmetic operators, comparison operators, and logical operators
Coding the If and If/Else Selection Structures (continued) Figure 306.2-4: How to use the If/Then/Else statement to code the If/Then/Else selection structure
Figure 306.3-4: How to use the If/Then/Else statement to code the If/Then/Else selection structure (continued)
Comparison Operators Comparison operators (or relational operators): Used as part of the condition in an If statement Most commonly used comparison operators: Equal to: = Greater than: > Greater than or equal to: >= Less than: < Less than or equal to: <= Not equal to: <>
Figure 306.2-5: How to use the most commonly used comparison operators
Comparison Operators (continued) Comparison operators: Have no order of precedence Are evaluated from left to right in an expression Are evaluated after any arithmetic operators in the expression All expressions containing comparison operators evaluate to True or False only
Comparison Operators (continued) Figure 306.2-6: Evaluation steps for an expression containing arithmetic and comparison operators
Using Comparison Operators Swapping Numeric Values Pseudocode for a procedure that displays highest and lowest of two numbers Figure 306.2-7: Sample run of the Number Swap application
Using Comparison Operators Swapping Numeric Values (continued) Figure 306.2-8: Pseudocode showing the If selection structure
Figure 306.2-9: Flowchart showing the If selection structure
Using Comparison Operators Swapping Numeric Values (continued) Figure 306.2-10: The If selection structure shown in the displaybutton s Click event procedure
Using Comparison Operators Swapping Numeric Values (continued) Block scope: the scope of a variable created within a statement block A block-scope variable can only be used within the statement block in which it was declared Concatenation operator (&): connects or links two strings together ControlChars.NewLine constant: Advances the insertion point to the next line
Using Comparison Operators Swapping Numeric Values (continued) Figure 306.2-11: Illustration of the swapping concept
Using Comparison Operators Swapping Numeric Values (continued) Figure 306.2-12: How to concatenate strings
Using Comparison Operators Example 2 Pseudocode for a procedure to allow the user to display the sum or difference of two numbers: Figure 306.2-13: Sample run of the Addition and Subtraction Calculator application
Using Comparison Operators Example 2 (continued) Figure 306.2-14: Pseudocode showing the If/Else selection structure
Figure 306.2-15: Flowchart showing the If/Else selection structure
Using Comparison Operators Example 2 (continued) Figure 306.2-16: The If/Else selection structure shown in the calcbutton s Click event procedure
Using Comparison Operators Example 2 (continued) MaxLength property: text box property that specifies the maximum number of characters that can be entered CharacterCasing property: text box property that indicates if text should remain as typed or be converted to upper- or lowercase
Using the ToUpper and ToLower Methods String comparisons in Visual Basic are case-sensitive ToUpper method: converts a string to uppercase ToLower method: converts a string to lowercase ToUpper and ToLower can be used to permanently or temporarily convert a variable s contents
Figure 306.2-17a: How to use the ToUpper and ToLower methods
Using the ToUpper and ToLower Methods (continued) Figure 306.2-17b: How to use the ToUpper and ToLower methods (continued)
Using the ToUpper and ToLower Methods (continued) Figure 306.2-18: Code showing the ToUpper method in the assignment statement
Using the ToUpper and ToLower Methods (continued) Figure 306.2-19: Code showing the ToUpper method in the If Then Else statement s condition
Logical Operators Logical operators (or Boolean operators): Used to combine two or more conditions into one compound condition Compound condition: a combination of conditions using logical operator(s)
Logical Operators (continued) Figure 306.2-20: How to use the logical operators
Figure 306.2-20: How to use the logical operators (continued)
Logical Operators (continued) Truth tables: used to evaluate logical operators in an expression Short-circuit evaluation: an evaluation in which the second condition may not be evaluated And and Or operations always evaluate both conditions AndAlso and OrElse operations do not evaluate the second condition if the first condition is false
Logical Operators (continued) Figure 4-21: Truth tables
Using the Truth Tables Use And or AndAlso when both conditions must be true to give a true result Use Or or OrElse when one or both conditions must be true to give a true result Use XOr when exactly one condition must be true to give a true result Logical operators are evaluated after arithmetic or comparison operators in an expression
Using the Truth Tables (continued) Figure 306.2-22: Order of precedence for arithmetic, comparison, and logical operators
Using the Truth Tables (continued) Figure 306.2-23: Evaluation steps for an expression containing arithmetic, comparison, and logical operators
Using Logical Operators in an If Then Else Statement Data validation: Process of verifying that the input data is within the expected range Use an If Then Else statement to validate input data Figure 306.2-24: AndAlso and OrElse logical operators in the If Then Else statement
Figure 306.2-24: AndAlso and OrElse logical operators in the If Then Else statement (continued)
Using Logical Operators in an If Then Else Statement (continued) Figure 306.2-25: Sample run of the application that contains the calcbutton s Click event procedure
Generating Random Integers Pseudo-random number generator: a device that produces a sequence of numbers that meets certain statistical requirements for randomness Random object: represents a pseudorandom number generator Random.Next method: Generates a random integer Can specify a minimum and maximum value
Generating Random Integers (continued) Figure 306.2-26: How to generate random numbers
Generating Random Integers (continued) Figure 306.2-27: Sample run of the Random Integers application
Generating Random Integers (continued) Figure 306.2-28: The generatebutton s Click event procedure
Programming Tutorial Figure 306.2-30: User interface
Programming Example Figure 306.2-38: User interface