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