Download presentation
Presentation is loading. Please wait.
Published byJulissa Gulliford Modified over 10 years ago
1
1 Software Development Programming Language Features – Branching
2
2 Lesson Aims and Objectives 1. Understand how to use If-Then, If-Then-Else and Select Case multiple outcome selections After completing this lesson you should be able to: 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
3
3 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 age = 5 then start primary school If age = 12 then start secondary school If age = 18 then start College or University or Job If-Then Every condition is tested one after the other – inefficient if an earlier condition is true eg age = 5 in this case
4
4 Multiple Outcome Selections 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 If-Then-Else 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
5
5 Multiple Outcome Selections In the case where there are many conditions to be tested, using If-Then-Else is: 1.Hard to read and understand 2.Ugly 3.Cumbersome to write Problem with If-Then-Else 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
6
6 Nested Control Structure Testing on more than one condition Solution: Nested If-Then Select Case provisional license Case TRUE Select Case age Case is >=17 drive motorcycle = TRUE End Select 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 Select-Case If provisional license Then If age>= 17 Then drive motorcycle = TRUE End If
7
7 Activities 1. Enter and test code EXAM MARK GRADER using Select Case 3.Read and highlight notes to the end of Topic 4 2. Enter and test code for NESTED CONTROL STRUCTURES Homework 1.Read Topic 4 notes and refresh memory of code examples you have completed and printed off in class 2.Complete all questions on Branching worksheet
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.