70 likes | 181 Views
Software Development. Programming Language Features – Branching. Lesson Aims and Objectives. After completing this lesson you should be able to:. 1. Understand how to use If-Then, If-Then-Else and Select Case multiple outcome selections.
E N D
Software Development Programming Language Features – Branching
Lesson Aims and Objectives After completing this lesson you should be able to: 1. Understand how to use If-Then, If-Then-Else and Select Case multiple outcome selections 2. Understand why a series of If-Then-Else statements is a more efficient control structure than a series of If-Then statements 3. Understand nested control structures
Multiple Outcome Selections A selection construct allows a program to execute a series of instructions depending on whether a condition is either true or false. Multiple outcome selections involves more than one condition and several series of instructions that can be selected for execution. If-Then If age = 5 then start primary school If age = 12 then start secondary school If age = 18 then start College or University or Job Every condition is tested one after the other – inefficient if an earlier condition is true eg age = 5 in this case
Multiple Outcome Selections If-Then-Else If age = 5 then start primary school ElseIf age = 12 then start secondary school ElseIf age = 18 then start College or University or Job End If Starting from top, each condition is tested and when a condition is true the instruction(s) that immediately follow it is/are executed - after which the program branches to the End If – ie no further conditions are tested – more efficient as not every condition may necessarily be tested – ie fewer instructions are executed overall
Multiple Outcome Selections Problem with If-Then-Else • In the case where there are many conditions to be tested, using If-Then-Else is: • Hard to read and understand • Ugly • Cumbersome to write Alternative: Select Case Select Case age Case is = 5 start primary school Case is = 12 start secondary school Case is = 18 start College or University or Job End Select • Eliminates repetitive If-Elseif • Cleaner and clearer looking • Easier to read and understand
Nested Control Structure Testing on more than one condition A person may drive a motorcycle if they have a provisional license and they are at least 17 years of age. Here, we have two conditions that must be true. We can use a Nested If-Then (If-Then inside another If-Then) or a Nested Select Case (Select-Case inside another Select-Case) Solution: Nested If-Then Solution: Nested Select-Case If provisional license Then If age>= 17 Then drive motorcycle = TRUE End If End If Select Case provisional license Case TRUE Select Case age Case is >=17 drive motorcycle = TRUE End Select End Select
Activities 1. Enter and test code EXAM MARK GRADER using Select Case 2. Enter and test code for NESTED CONTROL STRUCTURES • Read and highlight notes to the end of Topic 4 • Homework • Read Topic 4 notes and refresh memory of code examples you have completed and printed off in class • Complete all questions on Branching worksheet