Programming structures
- No matter what programming language you use, the structures that are available are usually fairly similar.
- Variables
- Arithmetic Operators
- Comparison Operators
- Statements:
Variables
- A variable is essentially a storage container for information.
- Examples:
- age = 28
- price = 33.99
- last_name = "Washington"
- There are different kinds of variables based on the information that you want to store.
Numeric Variables
- Integers (whole numbers)
- Floats (decimal values)
Integers
Integer variables are used to store
positive or negative whole numbers.
int data=30
- Amazon Example: number of books you can order is an integer (also the number in 'cart').
- More than likely, Amazon stores this field as an integer because they’ll want to do calculations with it. If you order 2 copies of a book, the program multiplies the price by 2 to get the total cost.
- Another example is in eBay. When you select something to order, the quantity is an integer. See what happens when you put in the letter 'a' or a special character like '@'.
Floats
Float variables are used to store
positive or negative decimalvalues.
float price = 30.99;
- Price per book
- Total order amount
Strings
String variables are used to store text.
String name = "Bob Jones";
- Amazon Example: if you went into your profile, fields like name, address, state, and city are strings.
- A string needs to be surrounded by quotation marks.
Numbers as strings
String zipcode = "48070";
- You can store a number as a string variable.
- If you store a number as a string variable, you can't use it in any math calculations.
- For example, credit card numbers are integers but you would never use them in a mathematical calculation.
- Another example would be postal codes. Even though the USA uses only numbers, not all countries do. So if you make your zip code field an integer, no other country can use it except the USA.
Booleans
Boolean variables are used to store the value
TRUE or FALSE (yes or no).
boolean isPaid = true
- You define a condition, then use a Boolean to tell the computer whether that condition is true or false.
- Example:
- If the order is not paid, do not ship
- If the user is logged then display her contact list
Logical structures
- Now that you can store data, you probably want to do something with it.
- Logical structures provide the framework for using that data to "get stuff done".
- If you understand the concepts, you can learn how to represent them in most programming languages.
Arithmetic Operators
- + (addition)
- - (subtraction)
- * (multiplication)
- / (division)
- % (modulo)
Comparison Operators
- Operators are words or symbols that let you compare, combine, or evaluate something to produce an output.
-
- == (equal to)
- > (greater than)
- < (less than)
- >= (greater than or equal to)
- <= (less than or equal to)
- != (not equal)
- && (and)
- || (or)
If / Then / Else
If / Then / Else statements evaluate a condition
and take actions based on the result.
- If the condition is true, the computer does the action or actions that are listed after the IF statement (in some languages, the "Then" is implied).
- If the condition is false, the computer does the action or actions that are listed in the Else statement.
If/Then/Else Examples
- Another Amazon example...
- "IF" you're logged in, show your name and cart numbers. "ELSE", show a link to the login page.
- "IF" you're an Amazon Prime member, you get free shipping. "ELSE", you get to pay for shipping!
Let's Do Some If / Then / Else!
- For this excercise, we're using Ruby.
- Go to repl.it, which is an online terminal to practice writing code.
- Choose 'Ruby'.
Let's Do Some If / Then / Else!
Part 1 - If
cart_total = 8
if cart_total > 2
print "You get free shipping!"
end
- Change the numbers to see what happens.
- Change the operators (greater than, less than, etc.)
Let's Do Some If / Then / Else!
Part 2
ELSE is the companion to the IF statement. An IF/THEN/ELSE statement says "If the statement is true, run this block of code; if it's not true, run the code after the else statement."
cart_total = 8
if cart_total > 2
print "You get free shipping!"
else
print "You'll have to pay for shipping."
end
- Change the numbers to see what happens.
- Change the operators (greater than, less than, etc.)
- Change the string statements.
Comments
Allow you to leave notes in your that get ignored by the computer
- Comments explain things that are not obvious in your code
- Make your code more maintainable
Loops
A loop is a set of instructions that repeats
until a certain condition is reached.
Loops
- A loop is a set of instructions that repeats until a certain condition is reached.
- Two kinds of loops are For Loops and While Loops.
Why Use Loops?
- Loops are very powerful. One primary reasons to use loops is to reduce your lines of code.
- Depending on what you're doing, you could have hundreds of lines of code using
if/then/else
, or you could write a simple loop statement.
Let's Do Some Loops!
- For this excercise, we're using Python.
- Go to repl.it, which is an online terminal to practice writing code.
- Choose 'Python'.
Let's Do Some Loops!
papers_to_deliver = 65
while papers_to_deliver > 0:
print papers_to_deliver
papers_to_deliver = papers_to_deliver - 1
print 'Out of papers! Go home!';
- Change the numbers to see what happens.
- Change the operators (greater than, less than, etc.)
- Change the string statements.
WHILE Loop Examples
- Amazon or eBay: "WHILE" it's today, display the daily deals.
- Amazon cart: "WHILE" it's this book, display the book's image, title, price, etc.
Beware the Infinite Loop!
- An infinite loop is a loop that will never meet the condition to stop. It will keep going until it's used up all your computer's (or server's) memory (RAM). This is bad!
- Example for our loop:
- If the
papers_to_deliver = papers_to_deliver - 1
line was missing, it would keep looping because 65 is always greater than 0!
- If
papers_to_deliver = papers_to_deliver - 1
was changed to + 1
, it would keep adding 1 to 65 forever!
Project Implementation Example
When companies implement a new program,
they take various things into consideration:
- Possible languages to use:
- Python: computer program
- SQL: manage the data
- HTML/CSS: web development to share on the Internet
- Programming structures:
- Variables: place to store the information such as, colors, amounts, etc.
- Detailed steps to tell the computer what to do.
- Loops: Instructions that keeps repeating as neccessary.