The Selection Structure CGI/Perl Programming

advertisement

The Selection

Structure

CGI/Perl

Programming

By Diane Zak

1

6

6

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

2

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

6

3

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

6

4

The Super Bowl Form and Script

6

• 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 the @errors array

5

6

The Super Bowl Form and Script

6

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

}

6

7

Coding the Selection

Structure in Perl

• if statement:

– The else clause is optional

– The condition expression must be a Boolean

– An expression that results in true or false.

– Can contain variables, numbers, strings, functions, arithmetic operators, comparison operators , and logical operators

6

8

Comparison Operators

6

• 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

9

Comparison Operators

6 for Numeric Values

• Test for equality using 2 equal signs ==

– Easy to confuse with the assignment operator (=)

10

Comparison Operators

6 for Numeric Values

11

Comparison Operators

6 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

12

Comparison Operators

6 for String Values

13

Logical Operators

• and and or are the most commonly used logical operators

– Combine multiple compound conditions into one 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

14

6

Logical Operators

6

15

Logical Operators

6

16

Validating Script

Input Data

6

• Can choose to check for correct or incorrect values

17

6

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 (@states, “Hawaii”);

• push (@nums, 10, 25);

– Can also add items to the end of an array with an assignment statement

• Syntax:

– array = ( array , list );

• Example:

– @nums = (@nums, 10, 25);

18

Modifying the Super

Bowl Script

6

– Two if statements are used to validate data

– push is used to append an error message to the @errors array.

19

Determining the

Size of an Array

6

• There are 2 ways to determine the size of an array:

– Assign the name of an array to a scalar variable

• Example:

– $size = @errors;

– Now, $size will be equal to the number of scalar variables in the array

– The scalar function

• Syntax:

– scalar ( array );

• Example:

– print scalar (@errors);

– Will print the number of scalar variables in the

@errors array

20

6

Completing the Super Bowl Script

21

6

Completing the Super Bowl Script

22

Completing the

Super Bowl Script

6

23

Using the if / elsif / else

6

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

24

Using if / elsif / else

6

25

Summary

6

• 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)

26

6

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

27

Summary

• The push function is used to add one or more items to the end of an array.

– Syntax: push ( array , list );

6

• 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

28

Download