Programming Concepts : 01. Program Structure and Variables

A computer program is a set of statements that make the computer preforms an action or interact with user or the computer hardware and apply a specified task. All computer programs are mainly consisting of :

  1. Headers / The start of the program.
  2. Variables.
  3. Statements and flow controllers (Code Blocks).
Simple C# application

Simple C# application

 Variables :

Variables are simply places in memory that a program can use to store data and read it later. Variables are given names like ‘x’ , ‘salary’, so you can refer to them later using them instead of ugly memory pointers and locations. Each variable must be assigned to a specific data type, so to declare a variable we need to choose a data type , name and assign a value to it (Optional).

” There are two types of variables , we have static variables in which memory is reserved during compilation time.In contrast, dynamic variables addresses are determined when the program is run.”

Common data types:

Data type Description Example
Integers Finite subset of mathematical integers. int x = -1
Booleans Logical data type (true or false) bool flag=true;
Characters Represents a symbol of natural language char a = ‘a’
floating-point Represents numbers with integral and fractional parts. float x = 1.5
String sequence of characters string name = “Ahmed”

These are only the common data types, programming languages provides a large variety of data types like byte,double,long. There are more and more, just search and you will find them easily.

Variables aren’t the only data structure that programming languages provide, more complex data structures are available like arrays, lists, tuples and dictionaries. 

Arrays :

An array is a container object that holds a fixed number of values of a single type. – Oracle Java tutorials

arrays could be one dimensional arrays “Vectors” like ({1,2,3,4,5,6}) or they can be multidimensional arrays “Matrices” like ({{1,2},{3,4},{5,6}}. We use “indexes” to access an element in an array like (arr_name[1]).

Example :

string[] students = {“Ahmed”,”Karim”,”Ali”};

Console.WriteLine(students[0] + ” ” + students[2]); //prints out the line Ahmed Ali.

Arrays are usually zero indexed, that means that for an (n) vector we have (0,1,2,3, …. ,n-1) range of indexes.

Associative array (Dictionary) :

This type of data structure is just like arrays, yet the index here is not and integer ranges from(0 to n-1), yet this array are composed of pairs represented like this (Key, Value). We index a dictionary with keys and use them to retrieve the corresponding values.

Example:

Dictionary<string, string> facColors = new Dictionary<string, string>();

favColors.Add(“Ali”,”Red”); favColors.Add(“Ahmed”,”Blue”);

Console.WriteLine(favColors[“Ahmed”]); //prints out the word Blue

There are more advanced data structures, each programming language offer it’s own set of those data structures, but you can also implement any data structure you want in any language that doesn’t provide it by default. These data structures are like linked lists, queues, graphs, stacks , etc.

I won’t be talking about these as they differ from language to another, so i’ll leave this to your choice of language and researches.

The next post we will be talking about boolean algebra , comparison operators and conditional statements.

Programming Concepts : 00. Introduction

Programming Concepts”  is a series that aims to introduce to you some main programming concepts like programming languages types, program structures, standard code writing formats. I hope you enjoy your time here and increase your knowledge.

** No prior programming experience or knowledge is needed.

Due to different purposes and problems we write our application for, different programming languages appeared to satisfy our needs and offer us suitable features.

At first, we classify languages according to their level , 3 levels can be distinguished:

  • Machine Languages       :  set of instructions executed directly by a processor(lowest-level 1s and 0s).
  • Assembly Languages    :  low level, one-to-one correspondence between the language and machine instructions, dedicated to a specific processor architecture.
  • High-Level Languages(HLLs) : very near to natural language, it’s isolated from hardware details(e.g. computing, memory managements).

High-Level languages are the most used, as they simplify complex programming. Here we have a trade-off, we trade speed and optimization for simplicity. There are many types of HLLs, we classify them to different types according to different factors we introduce those to you, but you can search and find more.

According to execution (Compiled vs. Interpreted) :

  • Compiled Languages      : Your code is processed only one time and reduced to a set of instructions and saved as an executable  file (e.g. C).
  • Interpreted Languages(Scripting) : Your code is executed line by line, and it’s processed every time you run it. The code is saved in the save format as you write it  and needs an engine to compile it (e.g. Python).

Scripting languages are mainly used when you prefer fast development over fast execution. It’s needed for rapid prototyping, web development and when you need to process files , mine data and feed them to databases. Computers are now very fast and you will not notice a big difference in speed specially with “dynamic high-level general-purpose language” such as Python, Perl and TCL.

For larger projects, better maintenance and debugging , powerful GUIs, fast processing and execution , you should use languages like (C/C++,C#,Java,VB,..).