Difference between revisions of "W1201 Scope"

From Coder Merlin
Line 2: Line 2:


== Research ==
== Research ==
* [https://en.wikipedia.org/wiki/Scope_(computer_science) Scope (Wikipedia)]  
* Read [https://en.wikipedia.org/wiki/Scope_(computer_science) Scope (Wikipedia)]  
* [[Media:ComputerScienceOne.pdf|Computer Science I Textbook (by Bourke)]] Chapter 2.2.4. Scoping
* Read [[Media:ComputerScienceOne.pdf|Computer Science I Textbook (by Bourke)]] Chapter 2.2.4. Scoping


== Experiment ==
== Experiment ==

Revision as of 12:57, 25 February 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Scope[edit]

Research[edit]

Experiment[edit]

Create a directory within your "project" directory.

cd ~/projects
mkdir project-1201
cd project-1201

Edit a new file named "main.swift"

emacs main.swift

For this project you'll repeatedly edit the same file. It's probably easiest to test this file within emacs by typing: M-& (async-shell-command) and observing the results. For each question, be sure to understand the behavior before proceeding to the next question. It will be helpful to record your answers and reasoning for later review.

1

let x = 10
print(x)

What is output? (Ignore whitespace)

23
10
21
Compile or runtime error

2

let x = 10
print(x)
print(y)

What is output? (Ignore whitespace)

21 23
10 21
Compile or runtime error
10 10

3

let x = 10
print(x)
let x = 21
print(x)

What is output? (Ignore whitespace)

10 21
Compile or runtime error
10 10
21 23

4

let x = 10
print(x)
var x = 21
print(x)

What is output? (Ignore whitespace)

21 23
10 10
10 21
Compile or runtime error

5

let x = 10
print(x)
do {
    let x = 21
    print(x)
}}

10 21
10 10
Compile or runtime error
21 21

6

let x = 10
print(x)
do {
    let x = 21
    print(x)
}}
print(x)

10 21 10
10 10 10
Compile or runtime error
10 21 21

7

let x = 10
print(x)
repeat {
    let x = 21
    print(x)
}} while x == 21
print(x)

Compile or runtime error
10 21 10
Infinite loop
10 21 21

8

let x = 10
print(x)
while x == 21 {
    let x = 21
    print(x)
}}
print(x)

Infinite loop
10 10
Compile or runtime error
10 21 10

9

let x = 10
print(x)
for x in 11 ... 11 {
    print(x)
    let x = x + 10
    print(x)
}}
print(x)

10 11 21 10
Compile or runtime error
Infinite loop
10 10 20 10

10

let x = 10
print(x)
for x in 11 ... 11 {
    print(x)
    x = x + 10
    print(x)
}}
print(x)

Compile or runtime error
10 11 21 10
10 10 20 10
Infinite loop