Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Selection Structure

Similar presentations


Presentation on theme: "The Selection Structure"— Presentation transcript:

1 The Selection Structure
CGI/Perl Programming By Diane Zak

2 Objectives In this chapter, you will:
Code the selection structure using the Perl if statement Compare data using comparison operators Include logical operators in an if statement’s condition

3 Objectives In this chapter, you will: Validate data passed to a script
Add items to an array using the push function Determine the size of an array Code a multiple-path selection structure

4 Introduction All scripts are written using one or more control or logic structures: Repetition Sequence Selection Making a decision or comparison and selecting one of two paths based on the result

5 The Super Bowl Form and Script
It is good programming practice for the script to verify that the data is valid Valid data: data that the script is expecting The Super Bowl form will check if the data is valid, and if not, ask the user to correct it Errors will be stored in array

6 The Super Bowl Form and Script

7 Coding the Selection Structure in Perl
if statement: Used for coding the selection structure Syntax: if (condition) { one or more statements to be processed when the condition evaluates to true } else { one or more statements to be processed when the condition evaluates to false

8 Coding the Selection Structure in Perl
if statement: The else clause is optional The condition must be a Boolean expression An expression that results in true or false. Can contain variables, numbers, strings, functions, arithmetic operators, comparison operators, and logical operators

9 Comparison Operators Comparison operators are also called relational operators One set to compare numeric values One set to compare string values Each operator has a precedence value The order in which Perl performs the comparisons in an expression For example, comparisons with a precedence number of 1 will be performed before comparisons with a precedence of 2

10 Comparison Operators for Numeric Values
Test for equality using 2 equal signs == Easy to confuse with the assignment operator (=)

11 Comparison Operators for Numeric Values

12 Comparison Operators for String Values
When comparing 2 strings, the ASCII value of each character in the first string is compared to the corresponding character in the second string Capital letters have a lower ASCII value than their corresponding lowercase letters

13 Comparison Operators for String Values

14 Logical Operators and and or are the most commonly used logical operators Combine multiple conditions into one compound condition and: All conditions must be true for the compound condition to be true If the first condition is false, the second condition will not be evaluated or: Any condition must be true for the compound condition to be true

15 Logical Operators

16 Logical Operators

17 Validating Script Input Data
Can choose to check for correct or incorrect values

18 Adding Items to an Array
The push function is used to add item(s) to the end of an array Syntax: push (array, list); Examples: push “Hawaii”); push 10, 25); Can also add items to the end of an array with an assignment statement array = (array, list); Example: @nums = 10, 25);

19 Modifying the Super Bowl Script
Two if statements are used to validate data push is used to append an error message to array.

20 Determining the Size of an Array
There are 2 ways to determine the size of an array: Assign the name of an array to a scalar variable Example: $size Now, $size will be equal to the number of scalar variables in the array The scalar function Syntax: scalar (array); print scalar Will print the number of scalar variables in array

21 Completing the Super Bowl Script

22 Completing the Super Bowl Script

23 Completing the Super Bowl Script

24 Using the if/elsif/else Form of the if statement
if/elsif/else structure can be used if there are multiple alternatives Can choose to use nested if statement(s) or use elsif for every alternative

25 Using if/elsif/else

26 Summary The selection structure allows a script to make a decision or comparison and based on the result, select one of two paths. Scripts should validate data. If the data is not valid, an error message should be displayed and the user should be aware of the errors and how to correct them. The if statement is used to code the selection structure. There are 2 clauses: if else (optional)

27 Summary The condition in an if statement can contain variables, numbers, strings, functions, arithmetic operators, comparison/relational operators, and logical operators Comparison operators: Numeric: <, <=, >, >=, ==, != String: lt, le, gt, ge, eq, ne String comparison operators should be used to compare strings and not numbers, and vice versa Logical operators: and: compound condition is true only if both conditions are true or: compound condition is true if any of the conditions are true

28 Summary The push function is used to add one or more items to the end of an array. Syntax: push (array, list); The size of an array can be determined by using the scalar function or by assigning an array to a scalar variable. The if/elsif/else form of the if statement can be used to code a multiple-path or extended selection structure


Download ppt "The Selection Structure"

Similar presentations


Ads by Google