Introduction to Computer Science
Introduction to Programming
Giving the Computer Instructions
At its heart, programming is about giving a computer a list of instructions to follow. Computers are powerful, but they are also very literal. They do exactly what you tell them, so your instructions must be incredibly precise. This precision is the key to making software work.
Think of it like writing a recipe. If a step says "add a pinch of salt," a human knows roughly what that means. A computer would need an exact measurement, like "add 2 grams of salt." Every programming language has its own set of rules for how to write these precise instructions. This set of rules is called syntax.
Syntax
noun
The set of rules that defines the combinations of symbols that are considered to be correctly structured statements or expressions in a programming language.
Just like English grammar has rules about where to put periods and commas, programming languages have syntax for everything from defining a variable to telling the computer to repeat an action. Getting the syntax right is the first step in writing code that works.
Storing Information
Programs need to work with information, or data. This data comes in different forms, like numbers, text, or true/false values. To keep things organized, programming languages use data types to classify the information. It's like having different types of containers for different things. You wouldn't store juice in a paper bag, and you wouldn't store a number as a piece of text if you wanted to do math with it.
Let's look at a few of the most basic data types you'll encounter everywhere.
| Data Type | What it Represents | Example(s) |
|---|---|---|
| Integer | Whole numbers | 7, -103, 0 |
| String | Text | "Hello, world!" |
| Boolean | True or false values | true, false |
| Array | An ordered list of items | [1, 2, 3], ["a", "b"] |
When we want to store a piece of data, we put it in a variable. A variable is just a named container. We can give it a name, like userAge or username, and then refer to it by that name later in the program. For example, we could have a variable userAge that holds the integer 25.
Controlling the Flow
A program doesn't always run its instructions straight from top to bottom. Often, it needs to make decisions or repeat actions. This is handled by control structures, which direct the flow of the program.
The most common decision-making tool is a conditional. It's a simple "if-then" statement: if a certain condition is true, then perform an action. You can also add an "else" part: if the condition is false, do a different action instead.
When you need to repeat an action, you use a loop. A loop will run the same block of code over and over again until a certain condition is met. For instance, you could use a loop to process every item in an array or to simply repeat an action a specific number of times, like printing "Hello" five times.
Putting It All Together
Let's see how these concepts combine in a simple program. We won't use a real programming language, just something called pseudocode, which is a plain-language description of the steps in an algorithm.
This program will check a user's age and print a different message depending on whether they are old enough to vote.
CREATE a variable called userAge
SET userAge to 19
// This is a conditional statement
IF userAge is greater than or equal to 18 THEN
PRINT "You are old enough to vote."
ELSE
PRINT "You are not old enough to vote yet."
END IF
See how it works? We store a piece of data (an integer) in a variable. Then, a conditional control structure checks that data and directs the program to print one of two strings. This simple combination of data types, variables, and control structures forms the foundation of almost all programming.
Now, let's test your understanding of these core concepts.
What is the "syntax" of a programming language?
In programming, a named container for storing a piece of data is called a __________.
With these building blocks, you can start to construct more complex instructions. Understanding syntax, data types, and control flow is the first major step on the road to programming.
