The Iterative Process and
Password Characters
Introduction to FOR and WHILE Loops
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Learning Outcomes
Upon completion of this lesson, students will be able to:
●
●
●
●
●
●
●
●
●
●
CRD-2.B - identify how a segment of program code functions.
CRD-2.C - identify inputs in a program.
CRD - 2.D - identify outputs in a program.
CRD - 2.J - Identify inputs and corresponding expected outputs or behaviors
that can be used to check the correctness of an algorithm or program.
AAP - 1.A - represent a value with a variable.
AAP - 1.B - Determine the value of a variable as a result of an assignment.
AAP - 1.C - represent a string using a variable.
AAP - 2.E - For relationships between two variables, expressions, or values: a.
Write expressions using relational operators. b. Evaluate expressions that use
relational operators.
AAP - 2.H - write and evaluate conditional statements.
AAP - 2.K - For iteration:a. Represent using iteration statements.b. Determine
the result or side-effect of iteration statements.
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Password Character Validation
• Have you ever had to use a password to log in to a
website?
• Most secure websites requires some minimum length and
variation in your characters; such as, at least one number,
one lowercase character and one uppercase character.
• Some even may require using special characters such as,
!@#$%^&*_+= for example.
• How does a website determine if you have the minimum
length and the correct type of characters in your
password and then decide to allow you access to
information on their website?
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Topics to Review
First of all, we need to review the following prerequisite
topics.
• Booleans
• Strings
• User input
• Variables
• Conditionals
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Review of Booleans and Variables
• A Boolean expression is a logical statement that is either
TRUE or FALSE
• Boolean expressions can compare data of any type as long
as both parts of the expression have the same basic data
type.
• You can test data to see if it is equal to, greater than, or
less than other data.
5 < 7 evaluates to TRUE
10 < 8 evaluates to FALSE
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Review of Booleans and Variables (cont. 1)
• Let the variable value ← 5 (Note: In AP Pseudocode ←
means = in an assignment of a variable. = is used as a
relational operator to determine to equality of two items)
What does value > 6 evaluate to?
• A variable can be reassigned and retains its most recent
value
varA ← 5
varA ← 10
varA > 8 evaluates to TRUE or FALSE?
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Review of Strings and User Input
• Strings are any combination of characters (including
numbers, spaces and special characters)
“Hello World” consists of 11 characters - don’t forget
about the space!
“Hi 67 123” consists of 9 characters
“M&Ms are the Be$t!” consists of 18 characters
“H$% Th&8*” consists of 9 characters
• User input refers to any time a program needs the user to
interact to continue
users can initiate a process, input a string or number,
make a choice, etc.
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Using Booleans and Strings
• Two strings are not the same unless the order and case of
all characters is IDENTICAL
• Does “Hello World” = “hello world” ?
• We would say that “Hello World” = “hello world”
evaluates to FALSE, why is this FALSE?
-H is not equal to h and neither is W equal to w.
• Does “howareyou?” = “how are you?” ?
• This evaluates to FALSE as well
• Does “equal strings?” = “equal strings?” ?
• Yes, of course this evaluates to TRUE because the strings
are IDENTICAL
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Review of Conditionals
• What happens when the following code is executed?
IF (6 < 10) {
DISPLAY (“This is correct”)
}
“This is correct” will be displayed
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Review of Conditionals (cont. 1)
• What happens when the following code is executed?
var1 ← “word”
var2 ← “words”
IF (var1 = var2) {
DISPLAY (“These words are the same”)
}
Nothing will be displayed
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Review of Conditionals (cont. 2)
• What happens when the following code is executed?
IF (“string” = “String”) {
DISPLAY (“These are the same”)
}
ELSE {
DISPLAY (“These words are different”)
}
These words are different would be displayed
• Note, the else does NOT require an additional Boolean
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Review of Conditionals (cont. 3)
What happens when the following code is executed?
IF (6 < 4) {
DISPLAY (“This is correct”)
}
ELSE-IF (6 < 8) {
DISPLAY (“Actually, this one is correct”)
}
Actually, this one is correct - will be displayed
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Review of Conditionals (cont. 4)
What is the output of the following code segment?
num ← 6
IF (num < 8) {
DISPLAY (“This is a small number”)
IF (num > 5) {
DISPLAY (“But it’s not too small”)
}
}
This is a small number AND But it’s not too small - will be
displayed
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the FOR Loop
• Programmers use something called a LOOP to repeat a set
of commands inside the loop
• One type of Loop is called a FOR loop, which will repeat
the command inside the loop a set amount of times.
• The AP Reference sheet uses the REPEAT n TIMES as one
version of the FOR loop and FOR EACH item IN thing as
another version of the FOR loop
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the FOR Loop (cont. 1)
• The REPEAT n TIMES Loop
• The following example will repeat the num ← num + 2
command 5 times:
num ← 6
REPEAT 5 TIMES {
num ← num + 2
}
DISPLAY (num)
• After the loop runs 16 will be displayed because 2 will be
added on to the initial value of 6 five times.
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the FOR Loop (cont. 2)
• What would be displayed after the following lines of code
are executed?
x←5
REPEAT 4 TIMES {
x←x*2
}
DISPLAY (x)
• The number 80 would be displayed because the initial
value of 5 is multiplied by 2 four times.
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the FOR Loop (cont. 3)
The FOR EACH item IN list version of the for loop
• This for loop is used to iterate through a list, but
many programming languages can use a FOR loop
to access elements of a string in the same
manner.
• For this lesson FOR EACH element IN string will
iterate once for each character in the string,
including letters, numbers, spaces and special
characters.
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the FOR Loop (cont. 4)
• For example,
word ← “Hello”
FOR EACH letter IN word {
DISPLAY (letter)
}
• The loop will iterate 5 times because there are 5
characters in word
• The first time through the loop letter = “H”, then “e”, “l”,
“l” and finally “o”
• The DISPLAY command on the AP CSP reference sheet
displays the value of an expression followed by a space. So
the code segment would display H e l l o
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the FOR Loop (cont. 5)
What would the following code segment output?
word ← “Mississippi”
FOR EACH letter IN word {
IF (letter = “s”) {
DISPLAY (“YES”)
}
}
• Yes Yes Yes Yes will be displayed.
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the FOR Loop (cont. 6)
What would the following code segment output?
word ← “Mississippi”
count ← 0
FOR EACH letter IN word {
IF (letter = “i”) {
count ← count + 1
}
}
DISPLAY (count)
• The number 4 will be displayed because there are four “i”
characters in word
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the FOR Loop (cont. 7)
What would the following code segment output?
word ← “Tennessee”
index ← 0
FOR EACH letter IN word {
index ← index + 1
IF (letter = “e”) {
DISPLAY (index)
}
}
• 2 5 8 9 will be displayed. (Note: the AP CSP reference
sheet counts indices starting at 1 NOT 0.)
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the FOR Loop (cont. 8)
What would the following code segment output?
word ← “Tennessee”
newWord ← “”
FOR EACH letter IN word {
IF (letter = “e”) {
newWord ← newWord + “E”
}
ELSE {
newWord ← newWord + letter
}
DISPLAY (newWord)
• “TEnnEssEE” will be displayed
• Complete Code Segment and Output Activity
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the WHILE Loop
• Another type of loop programmers use is called a WHILE
loop.
• This loop executes the statements in the loop until a
particular condition evaluates to FALSE.
• AP CSP uses a version of the while loop called
REPEAT UNTIL (condition)
• This loop will iterate until the condition evaluates to TRUE
• Many programming languages use a WHILE loop
described on the next slide that iterates until the
condition is FALSE
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the WHILE Loop (cont. 1)
• For example:
count ← 0
REPEAT UNTIL (count > 3) {
count ← count + 1
}
DISPLAY (count)
• This code segment will output 4, because once count is
greater than three the loop will terminate.
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the WHILE Loop (cont. 2)
• As mentioned previously, many languages use a WHILE
loop, that will execute until a condition is FALSE.
• For example:
count ← 0
while (count < 4) {
count ← count + 1
}
DISPLAY (count)
• This code segment will output 4.
• The loop will iterate 4 times. The first time count = 0, then
1, then 2 and 3 (but then 1 is added to 3), so 4 is displayed
after the loop terminates.
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the WHILE Loop (cont. 3)
• Sometimes, programmers need to “measure” a
word or list to determine how many times a loop
will iterate.
• The AP CSP Reference sheet defines LENGTH(list)
to count the number of elements in a list.
• For this lesson, we will use
LENGTHofSTRING(word) to count the number of
characters in a string, including letters, numbers,
spaces and special characters.
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the WHILE Loop (cont. 4)
• What would the following code segment output?
word ← “ABCDE”
count ← 0
REPEAT UNTIL (count = LENGTHofSTRING(word) ) {
count ← count + 1
DISPLAY (word[count])
}
• The code segment would output A B C D E
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the WHILE Loop (cont. 5)
• What would the following code segment output?
word ← “Hello World.”
count ← 0
REPEAT UNTIL (count > 5 ) {
DISPLAY (word)
}
• The code segment would output “Hello World.” an infinite
number of times (this is an example of an infinite loop - a
loop that will never terminate).
• Notice that count is not incremented, so count will always
equal 0 and thus will never be greater than 5.
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Iteration the WHILE Loop (cont. 6)
•
•
•
What would the following code segment accomplish?
answer ← “Yes”
total ← 0
REPEAT UNTIL (answer = “No” ) {
DISPLAY (“Enter a number.”)
num ← INPUT()
total ← total + num
DISPLAY (“Enter another number?”)
answer ← INPUT()
}
DISPLAY (total)
This will allow the user to enter as many numbers as they would
like and then display the sum of all the numbers entered.
Complete Code Segments and Out Activity Two
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Password Creation
• So, back to passwords.
• When you want to create a username/password for an
online service what happens?
• You first must select a username
• Some applications suggest one for you that is
available
• Many, however, require a certain format for a
username, such as first initial followed by last
name.
• Then you must create a password that meets certain
criteria like; minimum length, including a lowercase
letter, uppercase letter, number and sometimes a
special character.
• Why do you think they require these different
length/characters for your passwords?
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Password Creation (cont. 1)
• All of these requirements help to make your password
more secure.
• To be “more secure” means to take a long time to guess.
• Let’s say you had a password that was one character in
length and was a letter.
• How many guesses would it take to guess it?
• 52 guesses (at most) - 26 lowercase and 26 uppercase
• Now what if it was 2 characters in length?
• 52*52 guesses (at most)
• 3 characters?
• 52*52*52 guesses
• Having a length requirement helps increase the number of
guesses quickly, but using numbers and special characters
does as well.
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Password Creation (cont. 2)
• Why is it important to increase the number of guesses?
• “Number of guesses” really just means how many
attempts someone (or something - a computer) would
have to use before they would know your password.
• A computer can process things very quickly, so even 1
million attempts would take almost no time.
• Go to "How Secure Is My Password" to see how long a
computer would take to crack your passwords.
• Complete Password Programming Activity
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
The Iterative Process and Ethics
What do you know about music
sampling?
• Watch “The Ethics Of Sampling”.
(TeachRock.com)
Do you know a song that contains
sampling?
• Watch "Sampling as a Natural
Human Practice" (TeachRock.com)
Discussion Questions
• How does Wu-tang Clan’s
version differ from Knights?
Is it original?
• Who are the stakeholders?
• Where is accountability
assigned?
This document is licensed with a Creative Commons Attribution 4.0 International License ©2017
www.C5colleges.org This material is based upon work supported by the National Science Foundation under
Grant No.1548315
.
Catalyzing Computing and Cybersecurity in Community Colleges
www.ncyte.net
This material is based upon work supported
by the National Science Foundation under
Grant No.1548315.
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )