Programming Fundamentals For Beginners
Programming for beginners starts with understanding how programming languages transform into programs. Have you ever wondered how computers turn a programming language into code? Keep reading to learn more!
What is code?
Code is a set of instructions that a computer follows to achieve a specific goal. For example, you can ask the computer to add two numbers together or send a message to a friend. Programming languages are meant for us to understand and machine code is meant for computers to understand. Programming languages define rules that consist of variables and functions. Computers use a compiler to translate programming language instructions into machine code. Machine code is what computers use to run these instructions.
When computers were first created, engineers had to enter in machine code for their programs. Can you imagine having to flip physical switches on the machine for every single instruction? That’s why programming languages allow us to express our ideas more effectively for computers to understand.
Imagine if you wanted to say hello to your friend but you had to instead say each letter of the actual word before they could understand your message!
Before
After
Data types
Computers store data in binary form, a sequence of ones and zeros. With each data type, the computer requires different amounts of ones and zeros, also known as bits. So each bit can have two values – 1 or 0. So with more bits you can represent more values.
Programming languages define the rules of these data types. These rules include types to represent text, numbers, and logic.
Now you may be wondering, how do we turn bits into letters and symbols? That’s why we have different tables that encode these unique sequences of bits to a symbol. If you wanted to display the letter “A”, it’s binary form would be 1000001 (7 bits). The computer will then look up the symbol from the ASCII Table.
With text, we use the universal ASCII Table to convert binary into symbols of natural languages such as English or Spanish. Languages such as Chinese need a larger table called Unicode to represent all of its symbols.
For numbers, computers must also use bits to represent numbers such as 1,024 or 1 million.
1,024 is 10000000000 in binary (11 bits)
1 million is 11110100001001000000 in binary (20 bits)
I’ll spare you all the math but if you want to go deeper you can review how different base forms work such as decimal, hexadecimal, and binary form. The key point is that everything is encoded as binary.
Logic uses a type known as boolean to represent that a variable is true or false. For those thinking ahead, you guessed it, it’s either 1 or 0.
Each language will have its own way of expressing these types for your program’s variables.
C++ example:
int
– whole numbers
long
– large numbers
float
– numbers with a fractional part
char – used for a single character
char[] – used for a list of characters or words
Loops
Since code is a set of instructions, the computer will follow them until it reaches the end of the program. So how do we keep our program from stopping? We use loops.
Programming languages have for loops and while loops.
Example:
for loop
for (i = 0; 0 to 10, i++) then print i
while loop
while i < 10 then print i decrease i by 1
Loops will use logic statements to determine when the loop should stop. Such statements could be to count from 0 to 10 or when a logic variable becomes false.
Beware of infinite loops! Infinite loops may cause problems unless you have a condition to end the loop.
Logic Conditions
Computers use logic conditions to make choices. For example, when you’re thinking about what kind of food to eat you follow your preferences. Your preferences get represented as different conditions such as the time of day or your mood. All programming languages have their own version of the if-then-else keywords to express these preferences.
Example:
if time is lunch then eatLunch() else eatSnack()
Functions
In programming languages, we can express different actions as functions. In a video game you would have to write every single action that can happen to your character – such as running across a field or attacking your opponent.
Functions can also receive variables, known as parameters, for it to take action upon. For Example, in a game characters have their own function for when attacked. The “gets attacked” function defines the rules for the amount of damage received, animation to render, sound to play, or if the character is invulnerable to certain attacks.
function attackPlayer(player, damage)
player.health = player.health - damage
playAttackedSound(player) end function
Programming for beginners is very exciting. Please let me know in the comments what else you’d love to learn!
Stay curious!
Continue on your journey and think like a programmer