Last post i’m talking about PROBLEM, in Algorithm, and in this post, i will talking about
- BRANCHING ( SELECTION )
- LOOPING ( REPETITION )
BRANCHING [ SELECTION ] IN ALGORITHM
Branching or Selection is usually used when we want to do something with some situation or condition,
when the situation or the condition is YES, we will do action A, but if the condition is NO, Action B wil be executed.
The work flow is same in pseudo-code, we just write
If Condition A == Expectation Then
DO THIS
Else
DO THIS
or
If Color of apple == Red
EAT THAT APPLE
Else
THROW AWAY THAT APPLE
that is an example for Pseudo-code, in some Programming language, we type
if ( Condition)
{
Something to do
}
else
{
Other thing to do
}
for example
if ( i == 1 )
{
printf(“The value of I is one!”);
}
else
{
printf(“The value of I is not one!”);
}
LOOPING [ REPETITION ] IN ALGORITHM
Looping or Repetition is type of algorithm to do the same thing for a given amount of time.
For example, we must write some word 10 times, so, in Algorithm we use Looping for Writing down that word 10 times.
in Pseudo-code the form of Looping are like this
DO
Write some word
FOR 10 TIMES
or
counter = 0
DO
Write some word
Increase counter by 1
WHILE counter < 10
in programming world, we have 3 types of Looping, they are
FOR (Condition) {Statement}
DO {Statement} WHILE (Condition)
WHILE (Condition) {Statement}
WHILE (){} and FOR () {} looping is the same style, so that is doesn’t matter you want to use WHILE (){} or FOR (){}
but, they are different with DO {} WHILE {}, the difference is at the way the statement is executed
WHILE(){} or FOR(){} looping is executing the statement AFTER checking their condition,
DO {} WHILE () looping is executing the statement FIRST and then checking their condition.
In Algorithm, or especially, in programming, we have operands and operators for some math operation
Operator is symbolic character that used to count some operands, is that Sum, divide, multiply, etc.
Some known operators are
- + [ Plus ]
- – [ Minus ]
- / [ Divide ] ( for dividing operation )
- * [ Multiply ] ( for multiplying operation )
- = [ Equals ]