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

Advertisements

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)

Notes:

– 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