Basics Parts of (Imperative) Programming

·

2 min read

Data Types and Variables

All data in a computer is binary (1s and 0s). When we want to store words or letters we need to specify the types of data to allow a computer to know what the binary represents.

For numbers, we use integers for whole numbers such as 1 or -2. Floats for decimal numbers such as 1.2 or -2.1.

Note depending on precision or size requirements we may use variations such as long to store bigger integers doubles to more precise decimals or tinyint to take up less memory but have a smaller range.

Also, you can see that characters such as 'A' are numeric. This is because characters are encoded (typically Unicode) and can be used as an int. So the following 'A'-'A' will equal 0 or 'A'==65 (==is a is the left side the same as the right side) will return true.

For true or false we use booleans.

For letters, we use char (characters) and for words, we use strings which are just groups of letters where the order matters (arrays).

Variables are a way to assign value to names and use the variable's name and use operators such as below.

The above for example in Python3.

x=9 # set a variable called x with a value 9
print(x) # output x which is 9
# 9 
x=x+1 #set x to the value x+1
print(x) # output x which is 10
#10
x=2*x #set x to the value 2 times x
print(x) # output x which is 20
#20
print(x==20) #output the result of if x is equal to 20 which is true
#True

Sequence/Instructions

These are the basic steps or instructions to tell compute these are called statements. The order matters and they should be as simple as possible. Statements in the wrong order lead to incorrect actions.

Conditions/Selection

Conditions are true or false where depending on the value a set of instructions will be executed or skipped.

Iteration/Loops

Iterations are a set of instructions that are repeated until a condition is met. Such as a value becomes true or false or a set number of iterations is reached. Failing to set a correct condition will mean that the loop never ends until the program runs out of memory or exits.