Basic Conditions
Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
Conditional Branching ● Allows the computer to “make decisions” ● Based on a condition ● Embedded into some other programming structure
Condition ● Expression ● Can be evaluated to true or false ● Often compares a variable to a value ● Sometimes compares two variables
Comparison Operators ● == Equals ● != Not Equal ● <= Less than or equal to ● >= Greater than or equal to ● < Less than ● > Greater than
If Statement ● Checks a condition ● Begins a block of code ● If condition is true, executes all code in block ● If condition is false, skips code block
If example ● If it’s raining: – Wear a raincoat ● End if
Challenge Algorithm New program hiWally by me New variable userName string starts with “” Ask user’s name, store in userName If userName is equal to “Wally” Output “You Bovine” End if End Program
Challenge ● Modify the Wally program so it says one thing if the user’s name is Wally, and a more benign greeting if it’s someone else ● Pause video and try on your own…
Using an Else Clause ● Else is a useful addition to If statements. ● Else allows you to set up another block of code to execute if the condition is false
If-Else Example ● If it’s raining: – Wear a raincoat ● Else – Wear a sweater ● End if
Challenge Algorithm New program wallyOrNot by me New variable userName string starts with “” Ask user’s name, store in userName If userName is equal to “Wally” Output “You Bovine” Else Output “Do I know you” + userName + “?” End if End Program
Challenge Algorithm
Code ● My code