Introduction to IF ELSE ELSEIF and Passwords Learning Outcomes Upon completion of this lesson students will be able to CRD2B identify how a segment of program code functions CRD2C identify inputs in a program ID: 830413
Download The PPT/PDF document "Input Validation and Conditionals" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Input Validation and Conditionals
Introduction to IF, ELSE, ELSE-IF and Passwords
Slide2Learning 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.
AAP - 1.A - represent a value with a variable.
AAP - 1.C - represent a string using a variable.
AAP - 2.H - write and evaluate conditional statements.
Slide3Input Validation
Have you ever had to use a password to log in to a website?
Discuss the process of logging in to a website.
How does a website determine if you have the correct password and
decide
to allow you access to information on their website?
First of all, we need to review the following prerequisite topics.
Booleans
Strings
User input
Variables
Slide4Review 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
Slide5Review of Booleans and Variables (cont. 1)
Let the variable value ← 5 (Note: In AP Pseudocode
←
means = in an assignment of a variable)
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?
Slide6Review 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
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.
Slide7Using 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
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
Slide8Conditionals (The if statement cont. 1)
So, How does a website verify you have the correct password?
A website uses something called a “Conditional”
A conditional is a statement that executes a command when a Boolean is TRUE
IF is the first conditional we will discuss
The use of an IF conditional requires a Boolean and a subsequent command.
For instance;
if (Boolean is TRUE), then (Do something)
Slide9Conditionals (The if statement cont. 2)
What happens when the following code is executed?
IF (6 < 10) {
DISPLAY “This is correct”
}
“This is correct” will be displayed
What happens when the following code is executed?
var1 ← 12
IF (var1 < 10) {
DISPLAY “This is correct”
}
Nothing will be displayed
Conditionals (The if statement cont. 3)
The conditionals also work with Booleans involving strings
What happens when the following code is executed?
IF (“string” = “String”) {
DISPLAY “These words are the same”
}
Nothing will be displayed
Slide11Conditionals (The if statement cont. 4)
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
Slide12Conditionals (The if statement cont. 5)
Suppose now that the user inputs a string.
var1 ← INPUT(“Enter a word”)
IF (var1 = “correct”) {
DISPLAY “You have entered the correct word!”
}
The outcome of this program segment depends on what the user enters and cannot be determined unless user input is defined.
This is the basic idea behind a website allowing access after entering the CORRECT password.
Slide13Conditionals (the else statement)
Sometimes a programmer may want to execute one command if a Boolean is TRUE and another command is the Boolean is FALSE
We use the ELSE statement to accomplish this.
Slide14Conditionals (the else statement cont. 1)
For example;
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
Slide15Conditionals (the else-if statement)
One more possibility that a programmer may use is the else-if statement
The else-if statement is used when a first Boolean condition is FALSE and a second Boolean condition is TRUE
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
Slide16Conditionals (the else-if statement cont. 1)
But be careful, when the if Boolean is TRUE, the else-if Boolean is never checked
IF (5 < 7) {
DISPLAY “This is a true statement”
}
ELSE-IF (5 > 3) {
DISPLAY “This one is true too!”
}
ONLY This is a true statement will be displayed and NOT This one is true too!
Slide17Conditionals (embedded if statement)
Embedded ifs and else-ifs
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 its not too small”
}
}
This is a small number AND But its not too small - will be displayed
Slide18Conditionals (embedded if statement cont. 1)
What is the output of the following code segment?
num ← 10
IF (num < 8) {
DISPLAY “This is a small number”
IF (num > 5) {
DISPLAY “But its not too small”
}
}
Nothing will be displayed because num is not less than 8, so the
boolean
num > 5 is not considered since it is inside the first if statement.
Complete Code Segments and Output Activity
Slide19Password Protection
So, back to passwords.
When you want to log in to a website or application what happens?
You first must type in your username and then the password that corresponds to it.
The website or application uses an if conditional to check whether your password is IDENTICAL to the password they have saved for your username.
If they are identical, you are directed to your unique user experience and gain access to your information
But what happens if you type in the wrong password?
Slide20Password Protection (cont. 1)
When a user types a password incorrectly, usually, a message is displayed such as
“That password does not match the password we have on file for your username”
Then the user can re-type their password in and the new password is checked
Slide21Password Protection (cont. 2)
Why is it important to allow a user to enter their password again?
User may have forgotten
Users fingers may have accidentally hit the wrong key
User may have inadvertently left the caps lock on
Many other mistakes are possible
How many times should a user be allowed to enter their password incorrectly?
Slide22Password Protection (cont. 3)
Why would a website or application want to limit the number of times a user can incorrectly enter their password?
An unauthorized user may be attempting to break into an account by guessing a password
An authorized user may feel their account is less secure if there is no limit
Complete Strings and Variable Programming Activity
Slide23Code and Ethical Considerations
Watch
“
The
Impact of Code in Society (Joel
Spolsky
)”
by
Devoxx
FR
. May 9,
2016.
Discussion Questions
During which phase of cyber technology is the video addressing?
Who are the stakeholders?
Where is accountability assigned?
Slide24