Algorthm Hnd
Algorthm Hnd
Algorthm Hnd
Introduction to
algorithms
COURSE FACILITATOR :
TIOSSOCK NBONOUR BOREL
nbonouborel@gmail.com
1
Definition
An algorithm is a sequence of operations to be performed to achieve a result.
Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output. Algorithms are generally
created independent of underlying languages, i.e. an algorithm can be implemented
in more than one programming language.
Representation of an algorithm
There are two ways to easily represent an algorithm:
The programming flowchart, a graphical representation used to analyze a
problem. This representation of the algorithm has the advantage of being
visual, but is poorly suited to complex problems (layout, paper to be used). This
method of representation is gradually being abandoned in favor of a structured
language.
2
Example: graphical algorithm that calculates the sum of two numbers entered on the
keyboard.
Properties of Algorithm
Not all procedures can be called an algorithm. An algorithm should have the following
characteristics
3
Categories of algorithms
Search: Algorithm to search an item in a data structure.
4
How to write an algorithm
An algorithm has more or less the same general organization. There are three main
parts.
Name of algorithm
Processing part
START
STOP
The algorithm must have a well-defined structure. This structure must include :
-The header, which includes the name of the algorithm to identify it.
5
The elementary actions of an algorithm
a. Assign
Example. X 2
The Write instruction is used to display values on an output device, usually the screen.
The Read instruction allows the user to enter values via an input device (usually the
keyboard) for use by the algorithm. The value entered on the keyboard will be
assigned to a variable. Read is therefore an assignment instruction.
Read and Write instructions enable dialogue between the program and the user.
Example:
With the Read instruction, the message “Enter sales” appears on the screen and the
user enters the sales value on the keyboard. This value is assigned to the variable CA.
With the Write instruction, the message “Sales are: “ appears on the screen, followed
by the value of the variable CA entered by the user.
6
Arithmetic Operators in Programming:
Arithmetic operators in programming are fundamental components of programming
languages, enabling the manipulation of numeric values for various computational
tasks. Here's an elaboration on the key arithmetic operators:
Constants are symbolic names given to data where the value of the data cannot
change during the execution of the program. Many constants such as PI are built into
programming languages.
Variables
7
Variables are symbolic names given to data where the value of the data stored may
change during the execution of the program. In effect, a variable is a named area of
memory used to store data.
Data Type
1. If Statement:
Example:
end if
8
2- If-Else Statement:
Executes one block of code if a condition is true, and another block if it's false.
Example
3- If-Else-If Ladder:
Example
9
Switch Case: A Concise Decision-Making Structure
A switch case statement is a control flow statement that allows you to execute different code blocks
based on the value of a variable or expression. It's often used as an alternative to a series of if-else
statements, especially when you have multiple possible values to check.
Basic Structure:
Explanation:
10
Key Points:
The break statement is crucial to prevent "fall-through" behavior, where execution continues to
the next case if no break is encountered.
The default case is optional but recommended for handling unexpected input values.
switch cases can only compare exact matches, not ranges or inequalities.
In some languages, you can use fall-through behavior intentionally by omitting the break
statement.
When you have a variable or expression with a limited number of possible values.
When you want to avoid nested if-else statements, which can become complex and difficult to
read.
When you need to perform different actions based on specific values.
By understanding the switch case statement, you can write more concise and efficient code for
decision-making scenarios.
11
The For Loop in Algorithms
A for loop is a control flow statement in programming and algorithms that allows you to execute a
block of code repeatedly a specific number of times. It's particularly useful when you know beforehand
how many iterations you need to perform.
1. Initialization:
o This part is executed only once, at the beginning of the loop.
o It typically involves declaring and initializing a loop counter variable.
o Example: int i = 0;
2. Condition:
o This is a boolean expression that is checked before each iteration.
o If the condition is true, the loop body is executed.
o If the condition is false, the loop terminates.
o Example: i < 10
3. Increment/Decrement:
o This part is executed after each iteration of the loop body.
o It usually involves modifying the loop counter variable.
o Example: i++ (increment by 1), i-- (decrement by 1)
12
The While Loop
A while loop is another fundamental control flow statement in programming and algorithms. Unlike a
for loop, where the number of iterations is predetermined, a while loop continues to execute as long as a
given condition remains true.
13
Choosing the Right Loop:
Use a for loop when you know the exact number of iterations in advance.
Use a while loop when you don't know the exact number of iterations or when you want to
continue the loop until a certain condition is met.
By understanding both for and while loops, you can effectively control the flow of your algorithms and
write more efficient and flexible code.
A do-while loop is another control flow statement that is similar to a while loop, but with one key
difference: the condition is checked after the loop body is executed. This guarantees that the loop body
will execute at least once, regardless of the initial condition
14
Basic Structure of a Do-While Loop:
1. Loop Body:
o The code within the curly braces is executed at least once.
2. Condition:
o This is a boolean expression that is checked after the loop body.
o If the condition is true, the loop body is executed again.
o If the condition is false, the loop terminates.
Input validation: Ensuring that the user enters valid input before proceeding.
Menu-driven programs: Repeating a menu until the user chooses to exit.
Game loops: Continuously updating the game state and rendering the screen.
15
Choosing the Right Loop:
Use a while loop when you're not sure if the loop body needs to be executed at least once.
Use a do-while loop when you want to ensure that the loop body is executed at least once,
regardless of the initial condition. 1
16