Unit 3 Section 3-4 Blog
Notes
-When a coder chooses between two possible results, this is called selection.
-Iteration is the process of repeating a step until the requirement is met.
-Programs created using programming languages can execute algorithms that have been expressed in a variety of ways.
-Sequencing, selection, and iteration can be combined to create any algorithm.
-We humans use algorithms on a daily basis; an algorithm is a finite set of instructions that accomplishes a certain task.
-Implementing steps sequentially entails, for instance, carrying out the first action before moving on to the second, third, etc.
Vocab
sequencing- outline or set of steps that we do and follow in order that they are given
variable- you can store an actual value, the value of a variable in another variable, the result of an operation, or result of a procedural call
String: a sequence of characters
algorithm- finite set of instructions that accomplish a specific task, composed of sequencing, selection, and iteration.
selection- a section of code is run only if a condition is met.
iteration- repeating steps or instructions over and over again
numbers = [0,1,2,3,4,5,6,7,8,9,10]
evens = []
for i in numbers:
if (numbers[i] % 2 == 0):
evens.append(numbers[i])
print(evens)
Sequencing: all pf the steps
Iteration:They go through all the numbers in the step "for I in numbers:"
Selection: "if (numbers[i]% 2 == 0)" due to the fact that they sort each number to discover the even ones,
3.3 Video 2 Hacks
Practice Problems
- given the following code segment below:
a ⟵ 7
b ⟵ 1
c ⟵ 3
d ⟵ 4
a ⟵ b
b ⟵ c + d
d ⟵ b
a = 1, b = 7, c = 3, d = 7
- consider the following code segment:
hot ⟵ true
cold ⟵ false
cold ⟵ hot
hot ⟵ cold
what are the values of hot and cold after executing the code segment?
- the value of hot is true, the value of cold is true
- the value of hot is false, the value of cold is true
- the value of hot is true, the value of cold is false
- the value of hot is false, the value of cold is false
Answer: 1. the value of hot is true, the value of cold is true
3.4 Video 1 Hacks
String Homework
-
Test 1
firstName <- "Bob" lastName <- "Smith" var <- substring(firstName, 1, 1) name <- concat(lastName, var) email <- concat(name, "@gmail.com") DISPLAY(email)
-
What would the result be?
Hint: var = "B" name = "SmithB"
Answer: SmithB@gmail.com
-
Test 2
word1 <- "computer" word2 <- "textbooks" length1 <- len(word1)/2 length2 <- len(word2)/3 first <- substring(word1, 2, len1) second <- substring(word2, len2+3, len2) newWord <- concat(first, second) DISPLAY(newWord)
Answer:ompuook