Programming Essentials: Basic data types and control structures

If you have never programmed before, or your experience is limited to dynamic-typed languages such as R, Python or JavaScript, it is always nice to jump back into the basics for programming.

So what exactly do we mean by data types? It’s the way our compiler or interpreter will treat the data and the operations it will allow us to do with it. For example, the + and * operators work in an entirely different fashion with strings than how they work with numbers.

In traditional (static) languages such as C++, Java or Visual Basic, you will commonly see a type declaration when instancing any new variable, for example.

String x = "Hello World!";

Dynamic languages such as Python or Javascript will try to interpret a variable’s type when declaring it, there is no need to express the type.

So what variable types exist?

We will separate them between primitives and composites (i.e. types that derive from others).

Primitives are as follows:

  • Booleans are binary data. You can think of them as off-on, 0–1, Ground State-Excited State, or any other binary; however, in most languages these will be False or True.

Ennumerations can help us associate an integer value with any other abstract data type, and we can also associate any integer with a character (think of ASCII or Unicode), this allows us to think about composites:

  • Characters are any piece of text, including whitesace and symbols.

The most common control structures are if/else statements and loops. Since this is just a basic overview, I’ll plug in flowcharts and explain briefly.

Sequence (step by step action) vs selection (if/else) and iteration.

If/Else statements will set up two or more potential actions according to a condition (there is a more general but also more limited structure called switch).

Iterations will run a loop as long as a condition is met, the most common loops are for, for each and while.

  • A while loop will check a condition every time and break out of the loop until it is not met. While loops can result in infinite loops if not correctly coded.
for( int i = 0; i < 20; i++){
print(i)
}
  • The structure is for(initialization, condition, action to perform at the end of each iteration), and everything inside the block will be executed.

27 / Data Scientist / Professor