Slides: http://gdila.org/programming
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some “rules”
But how do you go from an idea to a computer program? And what is a computer program?
Turns out, you’re not the only person to wonder this.
Paul Ford wrote a 38,000-word essay called “What is code” that was published on June 11, 2015 in Bloomsburg Business Week, and it captured the attention of many.
At its core, a computer program is a way of getting something done.
One of the ways to accomplish “getting something done” is a list of instructions that tells the computer exactly what to do and allows interaction with human beings.
Those instructions have to be written in a “language” that the computer can understand.
Computers don’t understand things the same way humans do. Words and symbols that make sense to human eyes need to be translated down to binary for the computer to understand.
Binary is a system of expressing everything using only 1s and 0s.
For example, in ASCII (one system of encoding in binary), the lower case letter ‘a’ is represented as ‘1100001’.
From human-readable languages to binary:
Image credit: blog.malwarebytes.org/intelligence/2012/09/so-you-want-to-be-a-malware-analyst/
One can draw an analogy from a computer to the human body, and from coders to scientists!
So which human-readable programming language do you choose to use?
It depends on the task you’re trying to accomplish!
Different languages are designed to do different things.
For example:
I need a volunteer…
Problem Exists Between Keyboard And Chair
In other words, user error.
Remember, when you’re writing a program and the computer is executing it, the computer is only doing what you told it to do. If it’s not working the way you expect, look for an error in your program.
Just like English has the concepts of vocabulary and grammar, so do computer languages.
In computer programming, the syntax of a computer language is what defines if the combination of letters and symbols used are considered a valid construction in that language.
When you’re first playing around and learning the syntax of a new computer language, one of the most common first exercises is called "Hello, World!"
.
The "Hello, World!"
program is one that outputs the text "Hello, World!"
on the screen for the user.
This task is usually relatively simple to accomplish in most programming languages, so it’s a good warm-up exercise.
There are many, many, many examples in many, many, many languages.
We’re going to use a website called repl.it to try out "Hello, World!"
in a language or two.
Choose a language and paste in the hello world code from the Hello World collection.
Click the Run button to see the program run.
Once you have a project and a language, you’ll want to program your project in that language.
Let’s talk about some of the basic foundational building blocks of all programming languages.
Keep in mind that each language may have a different syntax for expressing these concepts, but the underlying concepts will be the same through them all.
age = 28
color = blue
You use a unique account number to identify yourself with your bank.
'
or "
) to specify the start and end of the string. (Remember how "Hello, World!"
had quotes?)TRUE
or FALSE
(yes or no)."true"
(yes). As a result, your account information displays, your cart is populated, etc.Any unanswered questions about variables or types of variables?
You may be asking yourself how exactly the computer “stores” these variables. As anyone who’s shopped for a computer knows, there are a lot of terms when it comes to computer storage and memory. Let’s briefly touch on them.
RAM or Random Access Memory is where the computer does its “thinking”.
This is where programs temporarily store data like variables while executing programs. Data in RAM is frequently overwritten and is destroyed when you turn off your computer.
ROM or Read-Only Memory is for the most part not editable.
It’s used by the computer to store important pieces of data when switched on, like data about the operating system.
Storage a more permanent type of memory. The computer user can utilize it to keep data around even after the computer is turned off.
Examples include the hard drive on the computer, a thumb drive, or an external hard drive.
Any unanswered questions about memory and storage?
Now that we have variables storing data for us, what can we do with them?
We use operators to compare, combine, or evaluate combinations of variables in order to produce some desired output. We could then store this output in another variable, if we wanted!
Some examples of operators:
Operator | Meaning |
---|---|
= |
equal to |
!= |
not equal to |
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
&& * |
and |
|| * |
or |
* These operators have different characters in different languages.
Any unanswered questions about operators?
If this, then that…
true
, Then the computer will do the actions you specify in the if
clause. (In some languages, the word then
is implied.)false
, the computer will do the actions you specify in the else
clause.Another Amazon example…
IF
you’re logged in, show your name and shopping cart contents. ELSE
show a link to the login page.IF
you’re an Amazon Prime member, you get free shipping. ELSE
you have to pay for shipping!Let’s practice conditional statements in Ruby.
cart_total = 8
if cart_total > 2
print "You get free shipping!"
end
else
statement after the if
statement.cart_total = 8
if cart_total > 2
print "You get free shipping!"
else
print "You'll have to pay for shipping."
end
Any unanswered questions about conditional statements?
A loop is a list of instructions that repeats until a certain condition is met.
One reason to use a loop is to reduce the number of lines of code you need to write to accomplish a task.
I need 5 volunteers…
Important: Beware the infinte loop!
There are two kinds of loops:
while
loopsfor
loopsA while
loop allows code to be executed repeatedly based on a given condition.
Often based on the value of a variable.
Usually your code will need to change the variable within the body of the loop.
Example: While
it’s raining, bring an umbrella.
You’re expecting and important email. As long as you haven’t received it yet, you keep refreshing your inbox.
number_of_bottles = 99
while number_of_bottles > 0:
print number_of_bottles
print ' bottles of beer on the wall'
number_of_bottles = number_of_bottles - 1
print 'no more bottles of beer!'
Be careful not to create an infinite loop!
If you do, you may need to force-quit your browser!
For
loops are similar to while loops. They just have a different syntax.
In a for
loop, you set up the conditional and increment or decrement your variable in the same line.
Example: For
each book in the list, read it!
You decide to do a pushup challenge every day for 100 days.
for
loop this time.for number_of_bottles in range(99, 0, -1):
print number_of_bottles
print ' bottles of beer on the wall'
print 'no more bottles of beer!'
Any unanswered questions about loops?
One final thing to keep in mind is that it’s good practice to leave comments in your code. Comments are not executed by the program. They’re just there for you to read.
Comments should explain why your code is doing what it’s doing.
The syntax for comments varies from language to language, but here are some common ways to create comments:
# The following code prints something
print 'hello, world!';
// And this code prints something too!
print 'hello, world!'
/* And this bit of code also prints something */
print 'hello, world!'
Any unanswered questions about comments?
Thank you for supporting Girl Develop It!
We strive to make every class better than the last. To help us, please complete this survey. It’s anonymous and your comments will only be seen by the organizers.