Overview
The purpose of programming is to translate logic into a computer language. In a sense we are trying to teach the computer to think for us. As you started this course with us and followed a few steps in the process to become a developer, this is probably already clear to you. The really hard question that remains unanswered is how do you teach a computer to do things? How do you teach a metal object to think? (We can enlighten you about one method that does NOT work! A short while ago, one of our computers kept crashing. It happened so often we finally gave it a good kick! Good and bad news: it never crashed again. Then again, it never ran again either. ;)
Logic is about making decisions based on input or other conditions. When a program "decides" something based on a question, we call it a "conditional." Conditionals are commonly used to ask questions in Flash and in programming in general. There are two types of conditionals: if () and switch (). In this hour we will focus on the following various types of if () conditionals:
if:
When there is only one decision to be made, without alternatives. This is the most simple form of condition testing in program logic. (If it's Friday night, it's time to go out.)
if-else:
When you have a choice in a decision, but only one. (If it's Friday night, go out; else be a deadbeat.) The next simple category of decision making
if/else if/else:
When you have several different decisions to make, but all at the same level of choices… it is nothing but an extended if-else construct where the number of conditions to be checked is more than one. (If it's Thursday night, start wishing it was Friday. If it's Friday night, go out. Both of those are on the same level: the logic hits the Thursday if, then flows to the Friday if.)
Nested if statements:
If there are more than one criteria for a decision (requiring 2 levels: easier than it sounds! If it's Friday night, if you're thirsty, go out. The thirsty if is nested inside the Friday one: unless it's Friday, you'll never wonder if you need a drink or not.) Generally used when we have more than one criteria that are independent.
Testing for negation:
Sometimes we can simplify the logical expression by testing for the occurrence of its complement: testing to check if it's NOT true. (What if we're boring souls, and we only ever go out on Fridays, and we are desperate so we check every single day – if it's Monday, stay sober. If it's Tuesday, stay sober.... just to see if it's Friday? The easier (and faster, slicker) way would be to check if it's NOT Friday!)
These statements are implemented in the program using special language constructs that we will explore in the next session.
Ready to Level Up Your Skills?
Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!
Enroll Now for Free!