Software Development Technique - Topic 05

33
Topic 5 : Selection Software Development Techniques

description

 

Transcript of Software Development Technique - Topic 05

Page 1: Software Development Technique - Topic 05

Topic 5 : SelectionSoftware Development Techniques

Page 2: Software Development Technique - Topic 05

Selection

• Selection let us provide branches of the flow of execution.

• Only one kind of selection is discussed in this topic, but some languages have several types.

• We call it an If then structure.

• It can also have otherwise parts

Page 3: Software Development Technique - Topic 05

The If Structure

• We have a formal syntax that we use for this in our pseudocode.

if <condition> then

// Statements to be executed if condition is true

end if

Page 4: Software Development Technique - Topic 05

Pseudocode to find a number is even or not.

Page 5: Software Development Technique - Topic 05

Desk-Checking

Line num remainder Input Output Remarks

1 0

2 0 0

3 0 0 "Please enter a number" Output

4 4 0 4 User enters 4

5 4 0 4 % 2 = 0

6 4 0 If remainder is 0 then go to

7. Otherwise go to 8

7 4 0 "That's an even number!"

8 4 0 End of if

9 4 0 "Thanks for your input!"

Page 6: Software Development Technique - Topic 05

Again Desk-Checking

Line num remainder Input Output Remarks

1 0

2 0 0

3 0 0 "Please enter a number" Output

4 7 0 7 User enters 7

5 7 1 7 % 2 = 1

6 7 1 If remainder is 0 then go to

7. Otherwise go to 8

8 7 1 End of if

9 7 1 "Thanks for your input!"

Page 7: Software Development Technique - Topic 05

The If Structure with otherwise

if <condition> then

// Statements to be executed if condition is true

otherwise

// Statements to be executed if condition is false

end if

Page 8: Software Development Technique - Topic 05

Pseudocode to find a number is even or odd.

Page 9: Software Development Technique - Topic 05

Desk-Checking

Line num remainder Input Output Remarks

1 0

2 0 0

3 0 0 "Please enter a number" Output

4 9 0 9 User enters 9

5 9 1 9 % 2 = 1

6 9 1 If remainder is 0 then go to

7. Otherwise go to 8

8 9 1 Otherwise – go to 9

9 9 1 "That's an odd number!"

10 9 1 End of if

11 9 1 "Thanks for your input!"

Page 10: Software Development Technique - Topic 05

If Otherwise … Tasks

1. A program takes whole number. Find whether it is greater than 100 or not.

2. A program takes input from user to find what day is today. If today is Saturday, display "It is a happy holiday." else "It is a Weekday".

3. Find whether user inputs positive or negative number.

4. WAP to take monthly salary. If yearly salary is above 200,000 then display “You have to pay tax” otherwise display “You don’t have to pay tax.”

Page 11: Software Development Technique - Topic 05

Multiple Courses of Action

• we can provide for many courses of action by using otherwise if

if <condition 1> then

// Statements to be executed if condition 1 is true

otherwise if <condition 2> then

// Statements to be executed if condition 2 is true

otherwise if <condition 3> then

// Statements to be executed if condition 3 is true

otherwise

// Statements to be executed if condition is false

end if

Page 12: Software Development Technique - Topic 05

Is a number greater or less than or is 0

Page 13: Software Development Technique - Topic 05

Now Desk-Checking

Line num Input Output Remarks

1 0

2 0 "Please enter a number" Output

3 10 10 User enters 10

4 10 If num (10) is less than 0 then go to

5. Otherwise, go to 6

6 10 If num (10) is 0 then go to 7.

Otherwise go to 8.

8 10 Otherwise – go to 9

9 10 "It must be a positive number!"

10 10 End of if

Page 14: Software Development Technique - Topic 05

Multiple Selection :: Tasks

• Write pseudocode to find child, young, old or invalid input.

• Display "Sunday" for 1, "Monday" for 2... "Saturday" for 7.

• Convert single digit number to Word (Hint : 7 to Seven)

• WAP to take monthly salary. Calculate the tax amount if yearly salary is .• 1 to 200,000 tax is 1%

• 200,000 to 350,000 tax is 1% of first 200,000+15% of remaining salary

• 350,000 to above tax is 1% of first 200,000 + 15% of next 150,000 + 25% of remaining salary

Page 15: Software Development Technique - Topic 05

Desk-Check Complexities

• Good algorithms are succinct

• succinct means "briefly and clearly expressed"

• They should do what they need to do in the minimum lines of code.

• Before you begin a desk-check, see if your pseudocode is as succinct as it can be.

• Simplify structures whenever you can.

Page 16: Software Development Technique - Topic 05

Nesting

• Selections within selection

• Selections within loop

• Selections within selection within selection within.....

Page 17: Software Development Technique - Topic 05

Nested Selection

• Write a pseudo to take age. If age is greater than 0, display “Valid age” otherwise display "Invalid age". Also if age is above 17, display “You are eligible to vote.” otherwise "You are not eligible to vote".

Page 18: Software Development Technique - Topic 05

Find child, young, old or invalid input

Page 19: Software Development Technique - Topic 05

Compound Conditionals

• Sometimes we have more than one condition.

• Let us say we want a number between ten and twenty.

Page 20: Software Development Technique - Topic 05

Compound Conditionals [Contd.]

• This will work, but it is inappropriate

• As far as possible, we want as few lines of code as we can to achieve our goals.

• We can however construct compound conditionals.

• These are conditions with two or more parts, linked by a logical operator.

Page 21: Software Development Technique - Topic 05

Logical Operators

1. OR

2. AND

3. NOT

Page 22: Software Development Technique - Topic 05

Logical Operator - OR

• If any condition or input is true, output will be true

Truth Table (A OR B)• A table used to outline all the possible outcomes of a compound conditions

is known as Truth Table.

Condition 1 (A) Condition 2 (B) Output (X)

False (0) False (0) False (0)

False (0) True (1) True (1)

True (1) False (0) True (1)

True (1) True (1) True (1)

Page 23: Software Development Technique - Topic 05

Logical Operator - OR

Truth Table for 3 conditions (A OR B OR C)

A B C X = A OR B OR C

0 0 0 0

0 0 1 1

0 1 0 1

0 1 1 1

1 0 0 1

1 0 1 1

1 1 0 1

1 1 1 1

Page 24: Software Development Technique - Topic 05

Logical Operator - AND

• If any condition or input is false, output will be false

Truth Table (A AND B)

Condition 1 (A) Condition 2 (B) Output (X)

False (0) False (0) False (0)

False (0) True (1) False (0)

True (1) False (0) False (0)

True (1) True (1) True (1)

Page 25: Software Development Technique - Topic 05

Logical Operator - AND

Truth Table for 3 conditions (A AND B AND C)

A B C X = A AND B AND C

0 0 0 0

0 0 1 0

0 1 0 0

0 1 1 0

1 0 0 0

1 0 1 0

1 1 0 0

1 1 1 1

Page 26: Software Development Technique - Topic 05

Logical Operator - NOT

• If any condition or input is false, output will be true

• If any condition or input is true, output will be false

Truth Table (NOT A)

Condition 1 (A) Output (X)

False (0) True (1)

True (1) False (0)

Page 27: Software Development Technique - Topic 05

Compound Conditionals - Previous Slide

• Let us say we want a number between ten and twenty.

Page 28: Software Development Technique - Topic 05

Complete the Truth Table

A B C A AND B A OR C (A AND B) AND (A OR C) NOT B

T T T

T T F

T F F

T F F

F T T

F T F

F F T

F F F

Page 29: Software Development Technique - Topic 05

Complete the Truth Table - Result

A B C A AND B A OR C (A AND B) AND (A OR C) NOT B

T T T T T F

T T F T T F

T F F F T T

T F F F T T

F T T F T F

F T F F F F

F F T F T T

F F F F F T

Page 30: Software Development Technique - Topic 05

Complete the Truth Table - Result

A B C A AND B A OR C (A AND B) AND (A OR C) NOT B

T T T T T T F

T T F T T T F

T F F F T F T

T F F F T F T

F T T F T F F

F T F F F F F

F F T F T F T

F F F F F F T

Page 31: Software Development Technique - Topic 05

Now Compute Following

1. (A AND B) AND (A OR C) OR NOT B

2. A OR B AND C

3. A AND (B OR C) AND NOT B

4. (NOT A) OR (NOT B)

5. (NOT A) AND (NOT B)

Operator Precedence

1. NOT

2. AND

3. OR

Page 32: Software Development Technique - Topic 05
Page 33: Software Development Technique - Topic 05

Any Questions???End of Topic 5