Programming Fundamentals
Overview
Teaching: 30 min
Exercises: 15 minQuestions
What is the difference between programming and coding?
What are algorithms and how are they developed?
What is pseudocode and how can it be used?
What is the most common forms of logic used in programming?
Objectives
Become familiar with the key concepts of programming, coding, algorithms, and pseudocode.
Be able to design algorithms to accomplish basic everyday tasks.
Become familiar with logical expressions commonly used in programming.
Introduction
Some of the practical skills required of programmers include the ability to create algorithms, model problems, process data, and manage projects. Many of these same skills are also necessary for anyone interested in the analysis of complex or large biological data sets.
It is very common for beginning programmers and scientists to learn how to use software or pieces of code without really understanding how it was designed. Knowing how a software program was designed is not typically necessary if you want to use it to complete some simple data analysis. But if you want to have more control over the tools you are using and the way your analysis is performed, then you need to understand how the piece of software is being run by the computer.
Programming vs Coding
Before we begin learning about how to write helpful programs for data analysis, it is important that we consider fundamental concepts and best practices in programming. While sometimes used interchangeably, programming and coding have different definitions.
So although programming and coding have different meanings, they are related. The goal of coding is to create the code that acts as a set of computer instructions for a part of a programming project. The goal of programming on the other hand, is to produce programs that are complete and ready to use software products.
Based on your personal experiences, let’s discuss our current understanding of these important concepts.
Discussion - Programming vs Coding
Come up with a single sentence to describe programming.
Solution
Programming is the process of creating instructions or set of related activities to achieve a task or goal.
Come up with a single sentence to describe coding.
Solution
Coding is the process of transforming the set of instructions for a process into a written language that a computer can interpret.
Pseudocode, Code, and Algorithms… Oh My!
Although the differences seem small, there are important distinctions that we can make between the concepts of pseudocode, code, and algorithms.
Everyone has some experienece with algorithms in their day-to-day life. For example, if you have ever cooked or done some task that requires you to follow instructions with a sequence of steps.
Discussion - Algorithms
Attempt to describe what algorithms are in a single sentence.
Solution
Algorithms are the set of step-by-step instructions that explain how to solve a given problem.
Algorithms need to be represented by some form of language in order to be understood and shared with others. The process of writing pseudocode can be tremendously helpful for figuring out how to start developing code to solve a problem, or implement an algorithm.
Discussion - Pseudocode
Attempt to describe what pseufocode is in a single sentence.
Solution
Pseudocode is the set of instructions for an algorithm written in a plain language.
As a first step before you begin developing an algorithm or writing any code, it is a good idea to write out the steps in a plain language. Let’s look at an example of pseudocode for a simple algorithm to make tea:
- Remove a teabag from the package
- Put the teabag in a cup
- Boil some water
- Add the hot water to the cup
- Allow the tea to steep for 5 minutes
- Remove the teabag
Challenge - Pseudocode
Write your own pseudocode for an algorithm to make buttered toast.
Solution
- Take a slice of bread from the package
- Place the bread in the toaster
- Allow the bread to toast for 5 minutes
- Remove the toasted bread from the toaster
- Put the toasted bread on a plate
- Open the container of butter
- Grab a knife by the handle
- Dip the kife blade into the butter
- Apply the butter to the toasted side of the bread
The primary advantage to using pseudocode in your programming process is that it improves the readability of your algorithms. By first writing algorithms for programs in a plain language, it allows you to break down a complex problem into smaller and more manageable pieces for coding. Furthermore, it gives you the chance to easily identify the most complex and potentially troublesome portions for code development.
Programming with Logic
A fundamental concept of computer programming, Boolean logic is the mathematical logic underlying Boolean algebra. In Boolean algebra expressions are evaluated to one of two values: TRUE or FALSE. Since an expression may only take on one of two values, Boolean logic is considered “two valued logic”.
Note that an expression is a combination of logical operands and operators. In Boolean logic the operands are statements that can be proven true or false, and the operators are the logical AND, OR and NOT.
Challenge - Boolean Expressions
1. What are some examples of simple boolean expressions?
Solution
- It is raining
- My cat is hungry
- The temperature is < 32 degrees Fahrenheit
- Today is NOT Wednesday
2. What are some examples of compound boolean expressions?
Hint
Use a combination of the following operators to add complexity to your expressions!
- Comparison operators (>, <, =, >=, <=, !=)
- Boolean operators (AND, OR, NOT)
Solution
- It is raining AND it is cloudy
- My cat is hungry OR my cat is cute
- The temperature is < 32 degrees Fahrenheit AND it is snowing
- Today is NOT Wednesday AND Today is Thursday
We can combine boolean expressions with control statements to specify how programs will complete a task. Control statments allow you to have flexible outcomes by selecting which pieces of codes are executed, or not.
Control Structures
The three primary types of control structures are:
- Sequential statmenetes are executed in the default ordering
- Iterative statements control the number of times a block of code is executed
- Conditional (or selection) statements control which blocks of code are executed, and which are not
The most common type of control structures are sequential statements. These are indicated by code statements written one after another, and are executed line by line. This means that the statements are performed in a top to bottom sequence according to how they are written.
The following is an example of a sequential statement with every-day actions.
Pseudocode
- Brush teeth
- Wash face
- Comb hair
- Smile in mirror
Challenge - Sequential Statmenetes
What is an example of pseudocode with a sequential statment using every-day actions or items?
Solution
This is an example of pseudocode with the seuqence of actions to tie a shoe.
- Tie a basic knot
- Make a loop with one of the laces
- Use your other hand to wrap the other lace around the loop
- Pull the shoelace through the hole to form another loop
- Hold both loops and pull them tight
Iterative statements allow you to execute the same piece of code a specified number of times, or until a condition is reached. The most common iterative statements are defined using either FOR or WHILE loops. Let’s start by looking at a flow diagram for a FOR loop, which dipicts the flow of information from inputs to outputs.
There are many every-day actions that are done repetatively over a range of time, for example.
Pseudocode
- For each day of the year
- Get up
- Brush teeth
- Wash face
- Comb hair
- Smile in mirror
Challenge - Iterative Statements 1
What is an example of pseudocode with a FOR loop iterative statment using every-day actions or items?
Hint: Iterative statements may contain, or be a part of sequential statements.
Solution
This is an example of pseudocode wth a FOR loop to brush your teeth.
- Add toothpaste to toothbrush
- For each tooth
- Brush the outer surface
- Brush the inner surface
- Brush the chewing surface
- Brush the tounge surface
- Rinse with water
WHILE loops are another type of iterative statement that can be used as a control structure in your code. This type of iterative statement will continue to execute a piece of code until a condition is reached.
We can also think of some every-day actions that are done repetatively until a certain point. For example, consider the process of braiding bair with a simple braid.
Pseudocode
- Divide the hair into three even sections
- While enough hair remains to weave
- Cross the left section over the middle section
- Cross the right section over the middle section
- Tie the ends of the sections together with a hair tie
Challenge - Iterative Statements 2
What is an example of pseudocode with a WHILE loop iterative statment using every-day actions or items?
Hint: Iterative statements may contain, or be a part of sequential statements or other iterative statements.
Solution
This is an example of pseudocode with a WHILE loop to play dodgeball.
- Gather 3 or more people per team
- Arrange 1 or more balls at the center of the court
- Set a timer for 10 minutes
- Rush to the balls in the center of the court when play begins
- While untagged players or time remains
- Try to dodge balls that the other team throws at you
- Throw balls at the other players to get them out
- Shake hands
The most common and simple form of conditional statements are of the IF… THEN form. These are statements that have two parts: hypothesis (if) and conclusion (then). The execution of the conclusion of the statement is conditional upon the state of the hypothesis, which must evaluate to TRUE.
Situations requiring conditional descisions come up all the time in life, for example:
- IF it rains, THEN the ground will be wet AND I will not water the plants
- IF I brush my teeth, THEN I will NOT get cavities
- IF my coffee = 100 degrees celsius AND I take a sip, THEN I will burn my tounge
Challenge - Conditional Statements 1
What are some examples of simple IF… THEN conditional statments using every-day actions or items?
Hint
Use a combination of the following operators to add complexity to your statements!
- Comparison operators (>, <, =, >=, <=, !=)
- Boolean operators (AND, OR, NOT)
Solution
Here are some examples of every-day conditional statements.
- IF you eat food, THEN you will NOT be hungry
- IF it is my birthday AND I want to cry, THEN I will cry
- IF my grade < 100 AND my grade > 90, THEN my grade is an A
The next type of conditional statement adds another level of complexity with the IF… THEN… ELSE format. By adding the ELSE condition to an IF… THEN statement we are able to have alternative conclusions to our hypothesis.
The following are examples of every-day moments that require decisions with multiple conditional outcomes.
- IF cell phone battery does NOT have power, THEN plug in ELSE turn on
- IF hair is dry, THEN put in braid ELSE dry with blowdryer
- IF pizza is hot, THEN eat ELSE heat
Challenge - Conditional Statements 2
What are some more examples of IF… THEN… ELSE conditional statments using every-day actions or items?
Hint
Use multiple ELSE conclusions and a combination of the following operators to add complexity to your statements!
- Comparison operators (>, <, =, >=, <=, !=)
- Boolean operators (AND, OR, NOT)
A more advanced type of conditional statement combines multiple IF… THEN… ELSE statements to make a compound statememnt with many alternative outcomes.
The following is an example of an every-day moment that requires compound decisions with many alternative outcomes.
Pseudocode
IF it is snowing outside
- AND IF my car is buried, THEN order food for delivery
- ELSE IF it is raining outside, THEN drive to pick up food
- ELSE walk to pick up food
Challenge - Conditional Statements 3
What are some more examples of compound IF… THEN… ELSE conditional statments using every-day actions or items?
Hint
Use a combination of multiple IF… THEN… ELSE statements and a combination of the following operators to add complexity to your statements!
- Comparison operators (>, <, =, >=, <=, !=)
- Boolean operators (AND, OR, NOT)
Key Points
Programming is the process of creating instructions or set of related activities to achieve a task or goal.
Coding is the process of transforming the set of instructions for a process into a written language that a computer can interpret.
Algorithms are the set of step-by-step instructions that explain how to solve a given problem.
Pseudocode is the set of instructions for an algorithm written in a plain language.
Boolean algebra uses mathematical expressions that are evaluated to one of two values: true or false.
Control statments allow you to have flexible outcomes by selecting which pieces of codes are executed, or not.