Download presentation
Presentation is loading. Please wait.
Published byMaximilian Lamb Modified over 8 years ago
1
Basic Conditions
2
Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
3
Conditional Branching ● Allows the computer to “make decisions” ● Based on a condition ● Embedded into some other programming structure
4
Condition ● Expression ● Can be evaluated to true or false ● Often compares a variable to a value ● Sometimes compares two variables
5
Comparison Operators ● == Equals ● != Not Equal ● <= Less than or equal to ● >= Greater than or equal to ● < Less than ● > Greater than
6
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
7
If example ● If it’s raining: – Wear a raincoat ● End if
8
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
9
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…
10
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
11
If-Else Example ● If it’s raining: – Wear a raincoat ● Else – Wear a sweater ● End if
12
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
14
Challenge Algorithm
16
Code ● My code
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.