Programming Basics, Flowcharts, & Pseudocode

Programming Basics

Venturing into programming is stepping into a world of creativity and problem-solving. Programming is more than just writing code; it's about finding clever ways to solve real-world problems. Programming is a way to turn creative ideas into reality. It's like sketching out a plan before building something. In this world of coding, clear thinking and planning play an important role, much like drawing up blueprints before constructing something. As you begin exploring programming, think of it as a tool for teamwork, a language to share ideas, and a space for continuous learning and problem-solving!

Before we embark on this journey, there are some concepts you need to familiarize yourself with. We will examine each of these in greater detail throughout the unit, but it is helpful to know what is coming. The next few steps into programming will require you to have some understanding of these concepts.

Variables and Data Types

In programming, data types represent the kinds of values a variable can hold. Variables are virtual containers where you can store information. Common data types are integers, floating point numbers, strings, Booleans, lists, tuples, sets, dictionaries, and nonetypes. 

Integers are whole numbers. Floating point numbers are numbers that have a decimal point. Strings are a set of characters; think of the text on this page as strings. Booleans are binary values that can either be true or false. Lists, tuples, and sets are all similar in that they can hold multiple values inside one variable. Dictionaries are similar to lists, tuples, and sets. However, unlike the other data types, they store key-value pairs; for example, brand: GMC. And nonetypes represent the absence of a value.

Input and Output

As you may imagine, inputs obtain information from the user, whereas outputs return information to the user. For example, you may input two numbers into a program, then it computes the sum of the numbers and then displays the result for the user to see.

Conditional Statements

Conditional statements use if, else-if, and else to make decisions based on specified conditions. A simple example would be if you are 18, then you can vote. Else, you cannot vote.

Loops

There are two types of loops in programming: for and while loops. While similar, each has situations where one works better than the other. They are for iterative tasks or tasks that repeat an operation or operations multiple times. For loops are better suited to situations where you know beforehand how many times you must iterate through a sequence (a block of code). While loops are better for tasks where you do not have that information upfront. For example, a for-loop can count up or down from a number or print the values in a list, whereas a while-loop will keep looping through a sequence until the user types exit.

Functions

A function is a reusable block of code. Built-in functions are necessary in any program. Custom functions get created to prevent rewriting the same code multiple times. An example of a custom function may be finding the area of a rectangle. If you are creating a program that needs to repeatedly calculate the area of different rectangles, creating a function that does that would make the most sense. Each time you need to perform this calculation, call the function, and the value gets returned.

Lists and Arrays

Lists or arrays can hold multiple values in one single variable. In Python, there are lists, sets, and tuples. The difference between them is whether or not they can hold duplicates and whether they are ordered or unordered. At this point, the difference is insignificant. We will delve into that later in the unit. For now, imagine a list you make on paper. Maybe it is a list of assignments you need to complete, a list of items you need to buy to complete a project, or a list of friends you plan to invite to your birthday party. Lists in programming do the same thing. Certain types of lists can be sorted. Other list methods (built-in functions specific to an object) include slicing, joining, inserting, removing, reversing, etc.

Operators

In programming, an operator is a symbol or word that helps you do something with numbers or information. Operators are used to add numbers together, compare if they are equal, or check if one number is higher than another. Symbols like + for addition or == for checking equality let you perform different actions in your code. Operators are tools in programming that make it easier to work with data and make decisions. They allow you to do all kinds of things with the information you have in your computer programs.

Important operators we will be examining and using include arithmetic operators (+, -, *, /, %, //, **), comparison operators (==, !=, >, <, >=, <=), logical operators (and, or, not), and assignment operators (=, +=, -=, *=, /=, %=). There are others that we will not worry about at this level but the ones listed will be essential to write the type of programs we will use in class.

Some of these symbols are not what you may think. Briefly put, the arithmetic operators above are, add (+), subtract (-), multiply (), divide (/), modulus (%) which returns the remainder from dividing a number by another, floor division ( //) which rounds down to the nearest whole number, and exponentiation (**) which raises a number to a power or another number. 

The comparison operators above are used to check if two values are equal (==), not equal (!=), greater than (>), less than (<), greater than or equal to (>=), or less than or equal to (<=).   

The logical operators above are used to check multiple conditions in one statement. An example of the AND operator would be to check if it is warm outside AND sunny. The OR operator could be used to check if it is warm outside OR sunny. The NOT operator could be used to check if it is NOT warm outside. Each of these operators would result in a Boolean, either true or false. 

The assignment operators above are used to assign a value to a variable. The equal sign assigns the value of the right operand (a value) to the left operand (a variable name). The other assignment operators above perform a specified operation and update the variable's value.


Flowcharts

Creating a flowchart before writing a computer program is a fundamental practice for beginners in computer science. Flowcharts offer a visual roadmap for your program, helping you understand and plan the logical sequence of steps to solve a problem. It's like sketching out the blueprint before constructing a building. By breaking down complex problems into smaller, more manageable steps, flowcharts are a guide for designing the algorithm and writing the code. This visual representation not only assists in spotting errors and inefficiencies early on but also serves as documentation for the program's logic, making it easier to collaborate with others and maintain the code in the future. As you progress in your programming journey, flowcharts become a valuable tool for communicating, planning, and debugging, providing a structured approach to problem-solving and coding.

Above are some basic symbols used in making flowcharts. The oval starts and ends a program, the rectangle represents a process that a computer program would carry out, the parallelogram represents an input or an output, and the diamond represents a decision, specifically, a binary decision; one that could be true or false or yes or no. 

Flowcharts are written in plain language, not in actual code. Below is an example of a flowchart for a simple program that has the user enter an integer. Then, the program determines whether the number is even or not and displays the value [even or odd]. 

Programs often contain loops. To create a loop, use arrows to illustrate how the program will flow. In the example below, the program decrements (counts down) from 10 to 0, displaying the number after each decrement.

Practice


Pseudocode

Understanding pseudocode is valuable for beginners in computer science. Pseudocode is a middle step between thinking about a solution and writing actual code. It helps focus on solving problems without getting tangled up in the details of a specific programming language. It's like sketching out a plan before building something. Pseudocode is a universal way to describe how a program should work. It improves communication with teammates or stakeholders, even if they are not tech-savvy. Using pseudocode helps beginners organize their thoughts and plan how to put their programs together. It's also helpful for taking notes about how a program should work, making it easier to work with others and fix mistakes. As you learn more about programming, pseudocode becomes a crucial tool for talking about ideas, making plans, and correcting problems in a structured way, similar to how flowcharts help in the coding process.

While flowcharts are a visual representation of a program used to check logic and program flow, pseudocode is a written representation of code to work out a program before making it. In programming, like other industries, you would not want to build a finished product before getting approval from a boss or client to move forward. Flowcharts and pseudocode are tools for this task.

Important Concepts

Some concepts are necessary to familiarize yourself with before starting to write pseudocode. The next section will provide widely used words to apply these concepts to pseudocode. The key concepts are as follows:

Using These Concepts

When writing pseudo code, the goal is to make the code easier to understand by writing it in plain English instead of using a programming language. There are some common ways to use the important concepts mentioned in the last section when writing pseudocode. Notice they are written in all caps. This is common practice when writing pseudocode and will be the expectation in class.

Examples

Area of a Rectangle Calculator

START

GET length of rectangle

GET width of rectangle

SET area to length * width

Display area

END


Length of a Word Program

START

GET word

SET length to zero

FOR letter in word

INCREMENT length by 1

ENDFOR

DISPLAY length

END


What to Wear Program

START

GET temperature

IF temperature >= 80

DISPLAY wear shorts and a T-shirt

ELSE IF temperature >= 65

DISPLAY wear pants and a T-shirt

ELSE IF temperature >= 55

DISPLAY wear pants and long sleeves

ELSE

DISPLAY wear pants, long sleeves, and a coat

ENDIF

END


Countdown From 5

START

SET number to 5

WHILE number >= 0

DISPLAY number

DECREMENT number by 1

ENDWHILE

END


Practice