COS 333: Chapter 8, Part 1

Willem van Heerden
3 Sept 202131:38

Summary

TLDRThis lecture delves into statement-level control structures, focusing on selection statements like if-else and switch-case. It explores the evolution of control structures post-FORTRAN, emphasizing the sufficiency of two-way selection and pre-test loops over go-to statements. The lecture discusses design issues in selection statements, including control expression types, clause specifications, and nested selector disambiguation across various programming languages. It also covers multiple-way selection statements, comparing their implementation in languages like C, C#, Java, JavaScript, and Ruby, highlighting differences in syntax and semantics.

Takeaways

  • πŸ“š The lecture introduces chapter eight on statement level control structures, focusing on selection statements and their design issues.
  • πŸ” The discussion differentiates between control within expressions, among program units, and among program statements, with the latter being the focus of the chapter.
  • πŸ”„ Historical context is provided on the development of control statements, highlighting the shift from hardware-specific to more generalized structures post-FORTRAN.
  • πŸ›‘ The redundancy of unconditional branching statements like 'go to' is established, with algorithms able to be represented using only two-way selection and pre-test loops.
  • πŸ”‘ Control structures are defined as comprising a control statement and the statements under its control, with a design issue being whether to allow multiple entry points.
  • ❓ The use of 'go to' statements for multiple entry points is generally seen as reducing readability without significantly increasing flexibility.
  • πŸ”Ό Two-way selection statements are detailed, with 'if-else' constructs being the primary example, and their design issues such as control expression form and type, clause specification, and nested selector disambiguation.
  • 🌐 Programming language variations in handling two-way selection statements are highlighted, including differences in how then and else clauses are specified and nested if-else structures are disambiguated.
  • πŸ”  The importance of the control expression's type is noted, with some languages allowing arithmetic expressions while others require boolean types.
  • πŸ”€ Multiple way selection statements, akin to 'switch' or 'case' statements, are introduced with five key design issues discussed, including form and type of control expression, segment specification, and handling of unrepresented values.
  • πŸ“ The chapter concludes with a look at iterative statements, unconditional branching, and guarded commands to be covered in the subsequent lecture.

Q & A

  • What is the main focus of the lecture on chapter eight?

    -The lecture on chapter eight focuses on statement level control structures, including an introduction to these structures and a detailed discussion on selection statements.

  • What are the different levels of control that can be discussed in programming languages?

    -The different levels of control in programming languages include control within expressions, control flow among program units (sub-programs or methods/functions), and control flow among program statements.

  • Why was there a heavy reliance on jumps or go-to statements in FORTRAN I?

    -FORTRAN I was designed to work with the IBM 704 hardware, which meant that the control statements were based directly on the hardware, making the control structures very specific and leading to a heavy reliance on jumps or go-to statements.

  • What was a significant outcome of the research and arguments in the 1960s regarding control statements?

    -A significant outcome was the understanding that all flowchart-represented algorithms can be coded with only two-way selection statements and pre-test logical loops, making unconditional branching statements like jumps or go-to statements unnecessary.

  • What is a control structure in the context of programming?

    -A control structure consists of a control statement, such as an if statement or a while statement, and the statements whose execution the control structure manages.

  • Why are go-to statements and multiple entry points generally considered to decrease readability in programming?

    -Go-to statements and multiple entry points can make the flow of a program less clear and more difficult to follow, which can lead to decreased readability and maintainability of the code.

  • What are the two general categories of selection statements?

    -The two general categories of selection statements are two-way selectors and multiple way selectors.

  • How does the use of parentheses affect the control expression in a two-way selection statement?

    -Parentheses are used to clearly delimit the control expression, ensuring that the condition is clearly defined and separated from the rest of the statement.

  • What is the purpose of an else clause in a selection statement used as an expression, as seen in some languages like ML or F#?

    -In languages where a selection statement is used as an expression and can return a value, the else clause is required to provide an alternative value when the condition is not met, ensuring that the expression always returns a value.

  • How does the design of switch statements in C and similar languages address the issue of control flowing through multiple segments?

    -The design requires an explicit unconditional branch, such as a break, at the end of each selectable segment to prevent control from flowing inadvertently to the next segment.

  • What is the difference between C and C# when it comes to multiple way selection statements?

    -C# differs from C by having a semantic rule that disallows implicit execution of more than one segment and by allowing both the control expression and the case constants to be strings.

  • How does Ruby handle multiple way selection statements with case expressions?

    -Ruby's case expressions can either use a sequence of when conditions without a control expression or use a case value as a control expression with when values, and they can return a result because they are treated as expressions.

  • What is the advantage of using boolean multiple-way selection statements over nested two-way selection statements?

    -Boolean multiple-way selection statements can improve readability by avoiding the complexity and potential confusion of nested if-then-else statements.

Outlines

00:00

πŸ“˜ Introduction to Statement Level Control Structures

This paragraph introduces the topic of statement level control structures, which is the focus of chapter eight. It outlines the lecture's agenda, including an introduction to these structures and an in-depth discussion on selection statements. The historical context of FORTRAN and the evolution of control statements is provided, highlighting the shift away from low-level, hardware-specific control structures towards more abstract, high-level constructs. The paragraph also emphasizes the debate in the 1960s about the best practices for control statements, leading to the conclusion that two-way selection statements and pre-test loops can suffice for all flowchart algorithms, rendering unconditional branches unnecessary.

05:01

πŸ” Design Issues of Two-Way Selection Statements

The second paragraph delves into the design considerations for two-way selection statements, such as 'if-else' constructs. It discusses the form and type of the control expression, the specification of 'then' and 'else' clauses, and the disambiguation of nested selectors. The paragraph explores different programming languages' approaches to these issues, including the use of special words, parentheses, and boolean types. It also touches on the impact of these design choices on language readability and flexibility, and how certain languages like Perl and Python use unique methods like braces and indentation to define clauses.

10:01

πŸ”„ Ambiguity in Nested Selection Statements

This paragraph examines the problem of ambiguity in nested selection statements, particularly in languages like Java, where static semantic rules dictate that an 'else' clause matches the nearest 'if'. The potential confusion this can cause is highlighted, and the use of compound statements with braces to disambiguate is explained. The paragraph also covers how languages like Perl, C, and C# handle this issue by requiring all 'then' and 'else' clauses to be compound statements, thus eliminating ambiguity. The impact of these approaches on programming language evaluation criteria is discussed.

15:01

πŸ“š Disambiguation Techniques in Programming Languages

The fourth paragraph continues the discussion on disambiguating nested selection statements, showcasing how different programming languages approach this issue. It describes Ruby's use of the 'end' keyword to clearly mark the end of selection statements, preventing ambiguity. The paragraph also contrasts this with Python's reliance on indentation to match 'if' and 'else' clauses, which inherently avoids ambiguity. The importance of the 'else' clause in languages like ML, F#, and Lisp, where selection statements can return values, is emphasized, and the requirement for an 'else' clause in these contexts is explained.

20:02

πŸ”„ Multiple Way Selection Statements Design

The fifth paragraph shifts focus to multiple way selection statements, commonly known as 'switch' or 'case' statements. It outlines the design issues related to these statements, including the form and type of the control expression, the specification of selectable segments, execution restrictions, case value specification, and handling of unrepresented expression values. Practical examples from C, C++, Java, and JavaScript illustrate how these languages implement 'switch' statements, with a focus on their syntax and semantics. The paragraph also discusses the impact of design decisions on language reliability and evaluation criteria.

25:04

πŸŽ›οΈ C# and Ruby's Multiple Way Selection Statements

This paragraph compares C# and Ruby's implementations of multiple way selection statements. It highlights C#'s semantic rule requiring unconditional branches to prevent accidental execution of multiple segments and its allowance of strings as case constants, which is not possible in C. The paragraph also explains Ruby's 'case' expressions, which can function as expressions returning results and can be used in two forms: one with only 'when' conditions and another with a case value and 'when' values. The impact of these features on the programming language evaluation criteria is discussed, inviting the viewer to consider the effects on reliability and expressiveness.

30:04

πŸ”„ Boolean Multiple-Way Selection as an Alternative to Nested If-Else

The final paragraph discusses boolean multiple-way selection statements as an alternative to nested if-else statements, which can sometimes be less readable. It provides an example of such a structure in Python, illustrating how 'if', 'elif', and 'else' keywords are used to create a sequence of conditions and associated statements. The paragraph concludes the chapter's discussion on selection statements and teases the next lecture's topics, which will include iterative statements, unconditional branching, and guarded commands.

Mindmap

Keywords

πŸ’‘Statement Level Control Structures

Statement level control structures refer to the mechanisms within programming languages that manage the flow of execution among individual statements. They are the focus of the video's discussion and are integral to the organization and logic of a program. The script delves into the specifics of these structures, such as selection and iteration statements, which are essential for making decisions and repeating tasks within code.

πŸ’‘Selection Statements

Selection statements are used to create conditional branches in a program, allowing the execution to follow different paths based on certain conditions. The video script spends a substantial amount of time on these, discussing their importance in programming and how they are implemented in various programming languages. Examples include if-else statements, which provide a basic form of decision-making.

πŸ’‘Two-Way Selection Statements

Two-way selection statements, such as if-else constructs, offer a choice between two execution paths based on the evaluation of a condition. The script explains how these statements are structured and the design issues associated with them, such as the form and type of the control expression and how nested selectors are disambiguated in different programming languages.

πŸ’‘Control Expression

A control expression is a part of a selection statement that determines which path of execution to follow. It is evaluated to a boolean value or an arithmetic expression that is interpreted as true or false. The video script discusses how the form and type of control expressions can vary between programming languages and how they influence the language's design and readability.

πŸ’‘Nested Selectors

Nested selectors occur when a selection statement is used within another selection statement, creating a more complex decision-making structure. The script addresses the ambiguity that can arise with nested if statements and how different programming languages, such as Java and Python, handle this with static semantic rules or indentation to ensure clarity.

πŸ’‘Multiple Entry Points

The concept of multiple entry points refers to the ability to jump into the middle of a control structure using statements like 'goto'. The script mentions that while this can be achieved with 'goto' statements, it is generally considered to decrease readability and is not favored in modern programming languages due to its potential to create 'spaghetti code'.

πŸ’‘Flowchart Represented Algorithms

The script discusses the research from the 1960s that concluded all flowchart-represented algorithms can be coded using only two-way selection statements and pre-test logical loops, making unconditional branching statements unnecessary. This highlights an important principle in the design of control structures for programming languages.

πŸ’‘Compound Statements

Compound statements consist of multiple single statements, usually enclosed by braces. The video script explains how they are used in selection statements to group statements together, affecting how control structures are formed and interpreted in languages like Perl, where all clauses must be compound.

πŸ’‘Indentation

Indentation is used in some programming languages, such as Python, to define the scope of code blocks, including selection statements. The script points out that Python uses indentation to eliminate ambiguity in nested selection statements, making the code more readable and structured.

πŸ’‘Switch Statements

Switch statements, also known as multiple way selection statements or case statements, allow the selection of one out of many execution paths based on the value of a control expression. The script provides an in-depth look at how switch statements are constructed and executed in languages like C, C++, Java, and JavaScript, and how they differ in terms of control flow and syntax.

πŸ’‘Case Expressions

Case expressions, as used in Ruby, are a form of multiple way selection that can return a result. The script explains two forms of case expressions in Ruby: one with only when conditions and another with a case value and when values, illustrating how these expressions can be used for decision-making and value assignment.

πŸ’‘Boolean Multiple-Way Selection Statements

Boolean multiple-way selection statements are an alternative to nested two-way selection statements and are used to handle more complex decision-making without deeply nested if-else structures. The script provides an example of how these can be implemented in Python using a series of elif statements, enhancing readability and maintainability.

Highlights

Introduction to statement level control structures in programming languages.

Discussion of control within expressions and among program units, with a focus on control flow among program statements.

Historical context of FORTRAN and the reliance on low-level control structures like go-to statements.

1960s research indicating that flowchart algorithms can be represented with two-way selection and pre-test loops, reducing the need for unconditional branches.

Definition and design issues of control structures, including the debate over multiple entry points and the use of go-to statements.

Exploration of selection statements, including two-way and multiple way selectors, and their significance in programming.

Design issues of two-way selection statements, such as the form and type of the control expression.

Different approaches to clause specification in modern programming languages, including single and compound statements.

The unique use of indentation in Python for differentiating clauses within selection statements.

Ambiguity in nested selection statements and how languages like Java disambiguate with static semantic rules.

Use of compound statements to eliminate ambiguity in nested selection statements, as seen in C, C++, and C#.

Perl's requirement for all clauses to be compound, even if they contain a single statement.

Ruby's approach to disambiguation using a special word to mark the end of a selection statement.

Python's use of indentation to eliminate ambiguity in nested selection statements.

Discussion on the use of selection statements as expressions in languages like ML, F#, and Lisp, allowing for conditional assignment.

Introduction to multiple way selection statements, also known as switch or case statements.

Design issues of multiple way selection statements, including the form and type of control expressions and the syntax for case values.

C, C++, Java, and JavaScript's use of switch statements for multiple way selection, including the use of case and default clauses.

C#'s semantic rule requiring unconditional branches at the end of each selectable segment to prevent implicit execution.

Ruby's case expressions that can return a result, offering an alternative to traditional switch statements.

Python's alternative approach to multiple selections using if statements for better readability.

Transcripts

play00:01

in the previous two lectures we covered

play00:03

chapter seven which deals with

play00:06

expressions and assignment statements

play00:09

in this lecture we will begin with the

play00:11

first part of our discussion on chapter

play00:13

eight

play00:14

which deals with statement level control

play00:17

structures

play00:20

this is an overview of the topics that

play00:22

we'll be discussing in today's lecture

play00:25

we'll start with an introduction to

play00:27

statement level control structures

play00:30

and then we'll move on to selection

play00:32

statements

play00:34

this is quite a substantial proportion

play00:36

of the chapters material and therefore

play00:40

we'll be spending quite a lot of time on

play00:42

it

play00:44

now within programming languages there

play00:47

are various levels of control that we

play00:50

can discuss

play00:52

so firstly we have control within

play00:54

expressions here things like the

play00:56

president's and associativity rules come

play00:59

into play and we discussed these in the

play01:02

previous chapter

play01:04

we also have control flow among program

play01:07

units essentially sub-programs or what

play01:10

you'll know as methods or functions and

play01:13

we'll discuss this in the next chapter

play01:16

chapter 9.

play01:19

in this chapter we'll be discussing

play01:21

control flow among program statements

play01:27

now as we saw in chapter 2 fortune 1 was

play01:30

specifically designed to work with the

play01:33

ibm 704 hardware and what this meant was

play01:37

that the control statements were based

play01:39

directly on the hardware and were

play01:41

therefore very low level

play01:44

what this meant was that the control

play01:47

structures that were supported were very

play01:49

specific to the hardware

play01:51

and didn't function

play01:53

in a similar fashion to what we used

play01:56

today

play01:57

what it also meant was that there was a

play01:59

heavy reliance on jumps or what you

play02:02

would understand as go-to statements

play02:06

so after the development of fortran

play02:09

there was a lot of research and

play02:10

arguments that took place in the 1960s

play02:13

as to what the best approach was to use

play02:17

for control statements

play02:19

and one important result of this was

play02:22

that all flowchart represented

play02:24

algorithms can be coded with only

play02:27

two-way selection statements and

play02:29

pre-test logical loops

play02:32

two-way selections are if else

play02:34

statements and pretest logical loops are

play02:37

while loops

play02:38

so what this thing means is that

play02:41

unconditional branching statements for

play02:44

example jumps or go to statements

play02:47

are unnecessary

play02:51

now a control structure is a control

play02:54

statement such as an if statement or a

play02:57

while statement and then also the

play02:59

statements whose execution the control

play03:02

structure controls

play03:05

now there's one design issue for all

play03:07

control structures and that is should

play03:11

the control structure in question have

play03:13

multiple entry points

play03:15

now it's only possible to achieve this

play03:18

through the use of go to statements and

play03:21

statement labels where a go to statement

play03:24

will jump execution to a particular

play03:26

point in the program that is labeled

play03:28

with the statement label

play03:31

now in general support for go to

play03:34

statements and therefore multiple entry

play03:37

points into control structures

play03:39

is considered to add little flexibility

play03:42

relative to the decreased readability

play03:45

that they will introduce

play03:50

now a selection statement provides a

play03:52

choice between two or more execution

play03:54

parts where this choice is exclusive in

play03:58

other words execution can only follow

play04:01

one of the selection paths out of a set

play04:05

now there are two general categories of

play04:07

selection statements namely two-way

play04:10

selectors and multiple way selectors and

play04:14

we'll be looking at each of these

play04:15

separately

play04:18

so let's start with two-way selection

play04:20

statements or two-way selectors over

play04:24

here we have the general form of a

play04:26

two-way selection statement

play04:28

we can see that we have a special word

play04:30

usually

play04:31

if which introduces the statement

play04:34

and following which we have a control

play04:37

expression and this control expression

play04:39

will be evaluated like a true false or

play04:43

boolean value it doesn't necessarily

play04:45

have to have a boolean type though as

play04:47

we'll see in a moment

play04:49

then we have a clause that is executed

play04:53

in the then portion if the control

play04:56

expression's value is interpreted as

play04:58

true and if the control expressions

play05:00

value is interpreted as a false then we

play05:03

execute the else clause

play05:06

so the three design issues associated

play05:08

with two-way selection statements

play05:11

firstly related to the control

play05:13

expression what is its form and type

play05:18

then related to the then and else

play05:21

clauses how are these clauses specified

play05:25

and then in the third place how should

play05:27

nested selectors be disambiguated and

play05:30

we'll look at each of these design

play05:32

issues in turn

play05:36

let's start by looking at the first of

play05:39

the three design issues

play05:41

that relate to the control expression

play05:44

both the form of the control expression

play05:46

and the type of the control expression

play05:49

so if we are looking at the form of the

play05:53

controlled expression we need to

play05:55

consider how the then clause is

play05:58

introduced and there are two approaches

play06:00

to this

play06:01

so firstly we can use a special word

play06:04

something like then

play06:06

to introduce the then clause

play06:09

notice that in this case there are no

play06:11

parentheses surrounding the condition

play06:15

the second approach is that we can

play06:17

surround the control expression with

play06:20

opening and closing parentheses

play06:24

now what's important to note here is

play06:27

that whichever approach we select for a

play06:30

programming language

play06:31

the condition needs to be delimited so

play06:35

in the first case the condition is

play06:37

delimited by the special words if and

play06:41

then

play06:43

in the second case the control

play06:44

expression is delimited by opening

play06:47

and closing

play06:48

parentheses

play06:50

so now if we move on to the type of the

play06:54

control expression

play06:55

we see that in c89 and c99 as well as

play07:00

python and c plus the control expression

play07:04

can be arithmetic in nature and in this

play07:07

case an integer condition will be

play07:09

interpreted as a boolean value so in

play07:12

other words a zero value will be

play07:14

interpreted as false and a non-zero

play07:17

value will be interpreted as true

play07:20

however in most other modern high level

play07:23

programming languages the control

play07:24

expression must be of a boolean

play07:28

type moving on to the second design

play07:32

issue related to

play07:34

two-way selectors

play07:36

we have the form that the clauses can

play07:39

take

play07:41

so then and else clauses in modern

play07:44

programming languages can either be a

play07:46

single statement

play07:48

or they can be compound statements where

play07:51

a compound statement consists of

play07:53

multiple single statements

play07:56

now in general in most situations a

play07:59

compound statement will be delimited in

play08:02

some way possibly with opening and

play08:04

closing braces

play08:05

or possibly special words at the

play08:08

beginning and end of the compound

play08:10

statements usually single statements are

play08:14

not delimited

play08:16

now perl is a little bit different in

play08:19

perl all clauses must be delimited by

play08:21

braces so in other words they have to be

play08:24

compound even if they consist of only

play08:27

one single statement

play08:30

in fortune 95 ada python and ruby

play08:33

clauses are considered to be statement

play08:36

sequences where the sequence can have

play08:38

one or more statements within it

play08:42

so a selection statement in these

play08:44

languages will end with a reserved word

play08:47

something like end or end if for example

play08:54

python once again uses a fairly

play08:57

different approach to other high-level

play08:59

programming languages in that it uses

play09:02

indentation to differentiate clauses

play09:05

so here we have an example of a two-way

play09:09

selector

play09:10

in python this only has a then clause

play09:14

and we can identify the then clause

play09:17

because it's this indented portion over

play09:20

here which is indented further

play09:22

than the if and the condition are

play09:26

so

play09:27

we can use then either spaces or tabs

play09:31

for this indentation however we can't

play09:34

mix both together and we must use the

play09:37

same approach throughout our program

play09:40

so what i would like you to do at this

play09:41

point is to pause the video

play09:44

and try to determine how this would

play09:47

affect the language evaluation criteria

play09:50

particularly in a negative way

play09:58

finally let's move on to the third

play10:00

design issue related to two-way

play10:03

selection statements

play10:05

so over here we have some example java

play10:08

code

play10:09

and this involves two if statements

play10:13

so we have our first if with a condition

play10:16

that tests where the sum is equivalent

play10:18

to zero

play10:20

then we have a second if with its

play10:22

condition that tests to see where the

play10:24

count is equivalent to zero

play10:27

then we have a then clause which

play10:30

contains an assignment where xero is

play10:32

assigned to the variable result and we

play10:34

have an else clause which performs also

play10:38

an assignment but this time of a value

play10:40

one to the variable result

play10:42

so the question is then which if does

play10:46

the else clause match to

play10:50

now there are various ways that this can

play10:53

be approached in order to disambiguate

play10:56

which if the else matches to with the

play10:59

red first if or the second if

play11:03

now the approach that java uses is that

play11:06

it has a static semantic rule which

play11:09

specifies that every else matches with

play11:12

the nearest if

play11:14

so in other words what this means then

play11:16

is that this else clause will actually

play11:19

match with the second if or the inner if

play11:24

rather than the outer one despite what

play11:27

the indentation appears to indicate to

play11:30

us

play11:31

so this can be fairly confusing and what

play11:34

i would like you to do at this point is

play11:36

once again pause the video and try to

play11:39

answer how this kind of ambiguity and

play11:42

the use of static semantic rules could

play11:46

affect the programming language

play11:47

evaluation criteria we've been using

play11:50

through this course

play11:56

now if we are using a static semantic

play11:59

rule in order to disambiguate nested

play12:02

selection statements then we can force

play12:05

alternative semantics through the use of

play12:08

compound statements and that's exactly

play12:10

what we've done in this example over

play12:13

here

play12:14

so what we have done is for the outer if

play12:17

statement we have created a

play12:20

compound statement as the then clause

play12:23

and that encapsulates the nested if

play12:27

statement with its then clause so we can

play12:30

see the beginning

play12:32

and the end of the compound statement

play12:35

which is delimited by means of braces

play12:39

so what this means then is that the else

play12:42

clause over here will now match the

play12:45

outer if statement and its condition

play12:50

the solution is also used in c c plus

play12:54

and the c sharp programming languages

play13:00

bill's approach to disambiguating nested

play13:02

selection statements relies on the fact

play13:06

that it requires all thin and else

play13:08

clauses to be compound

play13:11

so even if a clause contains only a

play13:13

single statement that single statement

play13:16

must be delimited as a compound

play13:19

statement

play13:20

and this then eliminates any ambiguity

play13:24

so here we see an example in perl

play13:27

of the same nested if

play13:30

statements that we saw in our java

play13:34

example previously

play13:35

we can see that this is very much like

play13:37

the second java example that we looked

play13:40

at where we matched the else clause to

play13:44

the then clause of the outer if

play13:46

statement once again we have a compound

play13:50

then clause

play13:51

for our outer if which the else then

play13:54

matches to

play13:56

the only difference here is that we can

play13:58

see that the single statements which are

play14:03

associated with the then clause of the

play14:05

nested if as well as the else clause of

play14:08

the out if are also compound in this

play14:12

example and we can see this because they

play14:14

are delimited by means of braces

play14:20

ruby's approach to disambiguating nested

play14:22

selectors relies on the fact that it

play14:24

uses a special word to mark the end of a

play14:28

selection statement and the special word

play14:31

in the case of ruby is end

play14:34

so this also then eliminates any

play14:36

possible ambiguity

play14:38

and we can see our previous example

play14:41

written in ruby over here so notice that

play14:45

we have special words that are used

play14:47

throughout

play14:48

for instance if we look at our outer

play14:51

two-way selection statement then we have

play14:53

the if special word

play14:55

then which introduces the then clause

play14:58

else which ends the then clause and

play15:01

begins the else clause and then finally

play15:04

end which denotes the end of the two-way

play15:07

selection statement

play15:08

so we know then that this else over here

play15:12

must match with the outer if because the

play15:15

inner if has already ended with the end

play15:19

special word over here this means that

play15:21

the else that follows can't then match

play15:24

with the nested if statement because

play15:27

that has already been ended and

play15:29

therefore must match with the outer if

play15:32

statement

play15:33

so what i would like you to do at this

play15:35

point is to pause the video and try to

play15:38

answer how ruby and to a lesser extent

play15:42

pearl's approach to disambiguating

play15:45

nested selection statements affects the

play15:48

programming language evaluation criteria

play15:51

we've been using through this course

play16:00

our final example related to how a

play16:03

programming language can disambiguate

play16:05

nested selection statements is in python

play16:10

so as we've seen previously python uses

play16:13

indentation in order to identify then

play16:16

and else clauses within a two-way

play16:19

selection statement and this indentation

play16:22

also eliminates any ambiguity

play16:25

the reason for this is that we can very

play16:28

easily identify which then clause and

play16:31

else matches to because they will be

play16:33

indented to the same level so we can see

play16:37

this in this example over here where we

play16:40

can very easily see that the else clause

play16:44

must match with the then of the outer if

play16:48

statement because these are indented to

play16:51

exactly the same level

play16:56

the last detail that we need to discuss

play16:58

related to two-way selection statements

play17:02

is the use of selectors as expressions

play17:06

so this is the case in ml f-sharp and

play17:10

lisp

play17:11

and in these languages the selection

play17:13

statement is actually an expression so

play17:15

what this means is that the selection

play17:18

statement can return a value

play17:21

so that means that the following code is

play17:24

legal in if shop here we can see that

play17:26

we're using a late in order to perform

play17:29

an assignment to a name

play17:31

y in this example and we are assigning

play17:34

to it

play17:35

our if expression over here which is a

play17:39

two-way selection statement that is used

play17:42

as an expression

play17:43

so what this means is that our condition

play17:45

is tested we check to see whether x is

play17:48

greater than zero

play17:50

if it is then our then clause simply

play17:53

contains x in this case so this means

play17:57

x's value will then be associated

play18:00

or bound to

play18:02

this

play18:03

name y

play18:05

however if x is not greater than zero in

play18:08

other words it's zero or it is less than

play18:11

zero

play18:12

then the else clause will be chosen and

play18:16

in this case that is 2 multiplied by x

play18:20

so in that case 2 multiplied by x will

play18:23

be computed and that value will be bound

play18:26

to the name y

play18:29

so if the if expression returns a value

play18:33

then there must be an else clause we are

play18:36

not allowed to leave out the else clause

play18:39

so what i would like you to do at this

play18:40

point is to pause the video and try to

play18:43

answer why the else clause is required

play18:46

in this case

play18:53

when i move on to the second kind of

play18:56

selection statement that exists namely

play18:58

the multiple way selection statement

play19:02

so multiple way selection statements

play19:03

allow for the selection of only one out

play19:06

of a larger set of either single

play19:10

statements or groups of statements and

play19:13

you'll know these as switch or case

play19:16

statements

play19:17

so there are five design issues related

play19:20

to multiple way selection statements

play19:22

firstly what is the form and type of the

play19:25

control expression and this is similar

play19:29

to the first design issue that we

play19:31

discussed in relation to two-way

play19:34

selection statements

play19:36

secondly how are the selectable segments

play19:39

specified and this relates to the syntax

play19:41

that is used for each of the individual

play19:44

cases

play19:46

then in the third place is execution

play19:48

through the structure restricted to

play19:50

include only one selectable segment

play19:54

or can execution include multiple

play19:57

selectable segments together

play19:59

one following the other

play20:02

then fourthly how are the case values

play20:05

specified and again this is a question

play20:07

of the syntax that will be used

play20:10

and then finally are unrepresented

play20:12

expression values handled in some way

play20:16

in order to handle an unrepresented

play20:18

expression value we would have to have

play20:20

some sort of default segment

play20:25

now we'll be looking at these design

play20:28

issues in relation to practical examples

play20:31

of how various programming languages

play20:34

deal with multiple ways selection

play20:36

statements

play20:37

so the first that we'll look at is used

play20:40

in c c plus plus java and javascript and

play20:43

you will be very familiar with this

play20:45

approach which we see in this example

play20:48

over here

play20:50

the multiple way selection is introduced

play20:52

by the special word switch and this is

play20:55

why we very often refer to these

play20:57

multiple way selections as switch

play21:00

statements

play21:01

following the switch special word we

play21:03

have the control expression which is

play21:05

delimited by means of parentheses and

play21:08

then we have the body of our switch

play21:10

which is delimited by an opening brace

play21:13

and a closing brace

play21:16

inside the switch we have a series of

play21:19

cases we can have any number of cases

play21:21

each one introduced by the special word

play21:24

case

play21:25

and we then have a constant expression

play21:28

followed by a colon and then a

play21:32

selectable segment the selectable

play21:35

segment can either be a single statement

play21:37

or a series of statements

play21:41

we then have following the series of

play21:44

cases an optional default clause which

play21:47

is introduced by the default special

play21:50

word also then followed by a colon and

play21:54

then a segment associated with that

play21:57

which of course can be a single

play22:00

statement or a series of statements

play22:02

following one another

play22:04

so the semantics then of how the switch

play22:07

statement would be evaluated is that the

play22:09

control expression is compared to the

play22:12

constants one by one from top to bottom

play22:16

the first

play22:18

case then that

play22:20

indicates a match between the control

play22:23

expression and its constant expression

play22:25

will be selected and then the

play22:27

corresponding selectable segment will be

play22:30

executed

play22:33

here we have the design decisions that

play22:36

were made for c's switch statement

play22:40

so firstly the control expression and

play22:42

the constant expressions can be either

play22:45

integers characters or enumerations

play22:49

notice that each of these can be

play22:51

interpreted as whole integer values

play22:55

secondly selectable segments can be

play22:58

either statement sequences or compound

play23:02

statements in other words blocks and of

play23:05

course this also includes single

play23:08

statements

play23:10

then thirdly control can flow through

play23:13

multiple segments

play23:15

so

play23:16

there's an explicit unconditional branch

play23:20

or a break that is required at the end

play23:22

of a selectable segment otherwise

play23:24

control will flow to the next segment

play23:27

and control can flow through multiple

play23:29

segments it can even flow all the way

play23:32

down to the default segment if there are

play23:35

no breaks that appear within the switch

play23:38

statement

play23:40

finally there is a default clause for

play23:43

unrepresented values

play23:45

and if this default clause is omitted

play23:49

but the control expression then matches

play23:51

no constant expressions then the entire

play23:54

selection statement will simply do

play23:56

nothing

play23:59

now c sharp has a fairly similar syntax

play24:03

to c when it comes to multiple way

play24:06

selection statements

play24:08

however it differs from c in two

play24:10

important ways

play24:12

so firstly c sharp has a semantic rule

play24:16

that disallows the implicit execution of

play24:19

more than one segment in other words it

play24:22

is not possible to accidentally have

play24:26

control run from one case on to the next

play24:30

case

play24:31

and c-sharp achieves this by requiring

play24:34

that each selectable segment must end

play24:36

with an unconditional branch where the

play24:38

unconditional branch is either a go-to

play24:42

or a break

play24:44

so the semantics here then for a break

play24:46

would be exactly the same as in c when a

play24:49

break is encountered then the case

play24:53

is terminated and execution moves to the

play24:56

end of the switch statement

play24:58

in the case of the go to the go to must

play25:01

be paired with a case label and in that

play25:03

case execution will then jump to the

play25:06

appropriate case and execution will

play25:09

continue from there

play25:11

there's only one situation in which a

play25:13

go-to or break is not required and that

play25:17

is when the selectable segment is empty

play25:20

in that case control will then

play25:22

automatically flow to the next case

play25:25

statement

play25:27

so what i would like you to do at this

play25:28

point is to pause the video and try to

play25:31

explain what effect this approach has on

play25:34

the reliability of c sharp

play25:42

secondly c sharp differs from c

play25:45

in that both the control expression and

play25:47

the case constants can be strings which

play25:50

of course as we saw on the previous

play25:52

slide is not possible in c

play25:56

again i'd like you to pause the video at

play25:58

this point and try to answer what effect

play26:01

this has on the programming language

play26:03

evaluation criteria

play26:10

ruby's multiple way selections are

play26:12

referred to as case expressions and the

play26:15

fact that they are expressions means

play26:17

that the case can return a result now

play26:20

there are two forms of these case

play26:22

expressions and the first uses only when

play26:26

conditions in other words there is no

play26:29

control expression present

play26:32

now this kind of case expression works

play26:35

like a sequence of everything else

play26:37

statements and we can see an example of

play26:40

this over here

play26:42

so here we have our case expression

play26:43

which is introduced for the special word

play26:45

case and ends with a special word end

play26:48

and we can see that there is no control

play26:51

expression present so we just have then

play26:54

within the case series of when

play26:57

and

play26:58

final else segment

play27:01

so what happens then when the case is

play27:04

encountered

play27:05

is that each of the conditions will be

play27:08

evaluated one at a time and whichever

play27:12

one matches first will then have its

play27:15

corresponding segment executed

play27:18

so if yeah mod 400 is equivalent to zero

play27:23

then the entire case will evaluate to

play27:25

true

play27:26

if year mod 100 is equivalent to zero

play27:30

then the entire case will evaluate to

play27:33

false

play27:34

and finally if we reach the else portion

play27:37

over here then we evaluate year mod 4

play27:41

equivalent to 0 and depending on whether

play27:44

this is true or false the entire case

play27:47

will evaluate to true or false

play27:50

and whichever

play27:52

mode of execution is followed the true

play27:55

or the false value that is produced will

play27:57

then be assigned to leap in this case

play28:01

which is a variable

play28:03

and this is because the entire case is

play28:06

dealt with as an expression and

play28:08

therefore can return a result which is

play28:11

used in the assignment

play28:15

the second form of ruby's case

play28:17

expressions uses a case value as a

play28:21

control expression and also has when

play28:23

values in this case expression works

play28:26

much more like a traditional switch

play28:28

statement that we've looked at

play28:30

previously

play28:31

so here we have an example of the use of

play28:34

this kind of case expression we can see

play28:37

once again that we are using the value

play28:40

of the case expression and assigning

play28:42

that to a variable named vowel

play28:46

here we have our case value which is a

play28:49

variable inval and then inside the case

play28:52

we have a series of wins followed by an

play28:55

else so the way that this would work is

play28:57

that the case value inval would be

play29:00

compared to each of the when values in

play29:02

turn and the first one that matches will

play29:05

then have its statement evaluated which

play29:09

becomes the value for the entire case

play29:11

and that will then be assigned to val

play29:14

so in other words if inval has a value

play29:17

of negative one

play29:19

then the entire case evaluates to the

play29:21

value of threshold minus one which will

play29:24

then be assigned to the valve variable

play29:26

if inval has a value of 0 then threshold

play29:31

will be assigned to val and if inval has

play29:34

a value of 1 then the value of threshold

play29:38

plus 1 will be assigned to val

play29:41

finally if we have a value for inval

play29:44

that doesn't match any of the win values

play29:47

then the else will be evaluated in which

play29:50

case we have an error code that is

play29:52

stored in this variable over here

play29:55

and that will then be assigned to val

play30:01

we'll now take a quick look at multiple

play30:04

selections using an if statement so

play30:08

these multiple selectors are boolean

play30:11

multiple-way selection statements and

play30:14

there are an alternative to nested

play30:17

two-way selection statements in other

play30:19

words nested if then else statements

play30:23

where these nested if-then-else

play30:25

statements may be poorly readable

play30:29

so here is an example of this kind of

play30:31

structure in python and you can see that

play30:34

it follows the same form as the first

play30:37

form of ruby's case expressions

play30:40

so we have then an if special word and

play30:44

that introduces our initial condition

play30:47

which then has a statement associated

play30:50

with it there may of course be multiple

play30:52

statements as well then we have the

play30:54

special word el if and this is

play30:57

equivalent to an else and an if

play31:00

following the else we then have a second

play31:04

condition associated with it and a

play31:06

second statement or set of statements

play31:08

that will be executed if that condition

play31:11

is met and we can have as many elevens

play31:14

as we want to as well as an else right

play31:18

at the end

play31:20

all right so that concludes our

play31:22

discussion on this chapter

play31:24

in the next lecture we'll be finishing

play31:26

off the discussion on this chapter and

play31:29

there we will be discussing iterative

play31:31

statements unconditional branching and

play31:35

guarded commands

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Control StructuresSelection StatementsProgramming ConceptsStatement Level ControlFlowchart AlgorithmsTwo-Way SelectorsNested If StatementsSwitch StatementsLanguage DesignCode ReadabilityProgramming Lecture