Programming Concepts : 03 Flow Controllers

In the previous post, we discussed boolean algebra, comparison operators and combining them together to form a statement that results in true or false.

Today, we will be talking about how to use these statements to take decisions that affects the flow of the program and take different paths depending on the statement you defined to be the “control flow statement”.

Flow controllers can be categorized to unconditional branch, conditional branch,conditional struct, loop, subroutines, unconditional halt. We will discuss each one alone.

Unconditional branch : It’s about jumping to a particular statement with passing by the inbetween instructions. These branches are like (JMP in assembly, GOTO in VB). GOTO statements are usually used with labels. Labels are explicit names assigned to a fixed set of instruction within the source code. We use these labels later in things like  GOTO to refer to these set of instructions in the program memory.

Conditional branch : It’s the same as the unconditional, yet it doesn’t preform the jump until a desired statement is true. There are some predefined conditional branch statements like(JZ,JNZ in assembly).

Subroutines : Subroutines also known as (procedures, methods, functions), it’s mainly represents a piece of code written once and used many times in different places. These subroutines preforms a flow control as when the program call a subroutine, the program start to execute the instructions of subroutine and then return to the instruction after the call statement.

Conditional struct : We can say they are higher forms of conditional branches, that all HLL has a nearly similar syntax for them. They are separated to two  main structs (If,Switch).

IF statement - C#

IF statement – C#

If statement in the above example is very clear, each condition is related to a specific set of instructions, except “else” it represents any other cases that may happen.

Unlike the if,if-else statements, the switch statement can have a number of possible paths no only 2. The program chooses which path to go, based on the value of a particular variable like the following example that prints the name of the day depending on the value of variable day.

Switch statement example - C#

Switch statement example – C#

** default case is used to provide a path if no case was true.

** notice the “break;” statement after each case, if they weren’t there, the program would have executed the following case without checking for the condition.

LOOPs: In computer programming, a loop is a set of instructions that is continually repeated until a condition is reached or a force break is applied. Loops are divided into three main categories (count-controlled loops, condition-controlled loops, collection-controlled loops). These loops could be found in different constructions depending on the language you use, so I’ll talk about he most used (For, while, do while, for each).

For Loop (Count-controlled loops): It’s a popular construction for repeating a loop a certain number of times. The following example finds the sum of the numbers from 1 to 9 ( notice that the condition is i<10).

For loop example - C#

For loop example – C#

While Loop (Condition-controlled loops): It’s a famous construction for repeating a loop while a conditional statement is true, else the loop is broken. In the following example, the sum is increased by one as long as it’s less than or equal 10.

While example - C#

While example – C#

Do-While Loop (Condition-controlled loops): It’s the same as while loop, yet the loop is executed one time at least before checking the condition.

Do-While loop example - C#

Do-While loop example – C#

For each Loop (Collection-controlled loops): It’s a special loop construction that many languages provide, it allows the loop to be executed through elements of an array or members of collection.

foreach loop example - C#

foreach loop example – C#

Now, those where the most important things that each developer uses to write his own program. Despite they are very simple, they are very useful and you can make huge applications using them.

In the next post, we will be talking about one of the most important concepts that computer programming ever had. We will be talking about OOP(Object Oriented Programming).


Programming Concepts : 02 Boolean Algebra

Boolean algebra is the subarea of algebra in which the values of the variables are the truth values true and false, usually denoted 1 and 0 respectively.  – Wikipedia

An expression is a collection of logical operands and logical operators. In boolean algebra, operands are statements like (4<6 , ahmed is taller than Ali). Logical operators are logical AND,OR and NOT. These operators are written in different forms from a language to another).

Boolean Operators evaluation :

   A     B     A AND B     A OR B     NOT A

F     F          F                 F              T

F     T          F                 T              T

T     F          F                 T              F

T     T          T                 T              F

Comparison Operators :

Most of times, boolean expressions involve comparison operators that are evaluated to give a (T or F) value. These comparison operators are like (>,<,=,<=,>=,!=). ** ! means NOT

Expressions are used to ask yes or know question and have the answer as True or False. For an example let’s ask “Is Ahmed taller than Ali?”, assuming that Ahmed is 190 cm and Ali is 170 cm so the expression will return True.

Combining Comparison and Boolean Operators :

Assume that Ahmed wants to join a basketball team. To join this team Ahmed has to be at least 190 cm and less than 95 Kg. So these conditions will be coded into these expressions :

height >= 190 AND weight < 95 

So now each comparison expression must be evaluated to a (True or False) value, then we apply boolean operators to the resulting operands.

For more than two comparison expressions, expressions are evaluated from left to right, and parenthesis can change the order like :

height > 190 AND ( weight<100 OR speed>20)


– Multiple conditions separated by OR : at least one true operand to make the result true

– Multiple conditions separated by AND : all the operands must be true to make the result true


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.


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,..).