Chapter 0 Algorithm
Chapter 0 Algorithm
Rania YANGUI
y [email protected]
Chapter 1: Introduction to the
algorithm
1.2
Plan
1. Introduction
2. Structure of an algorithm
3. Example
4. Concept of variable
5. Variable Types
6. Expression and operator
7. Concept of constant
8. Types of instructions
9. Test on instruction assignment
10. Conditional and iterative instructions
1.3
Introduction
● An algorithm is a finite and unambiguous sequence of
operations Or of instructions to solve a problem .
● Comes from the name of the mathematician Al- Khawarizmi ,
the father of algebra .
● Examples algorithms :
- A cooking recipe ( ingredients → prepared dish )
- The research in a dictionary (word → definition )
- The whole division ( two integers → their quotient)
1.4
Introduction
● Program :
○ A program is an assembly and a chain of instructions
elementary written in a programming language , and
executed by a computer in order to process the data of a
problem and return one or several results .
● Programming language :
○ A programming languageprovides a set of keywords and
syntax rules that allow you to create statements that form
programs and that can run , without any problems, on a
machine.
1.5
Introduction
1.6
Introduction
1.7
STRUCTURE OF AN ALGORITHM
1.8
Example
1.9
Concept of variable
● Example of variable
■ M, Max: float
■ Weight , Average : int
■ Achievement : Boulean
1.
Variable Types
1.
Expression & operator
1.
Expression & operator
Operators alphanumeric
● &: concatenation
Example :
A ← "Hello"
B ← "everyone"
1.
Notion of constant
■ VAT = 0.17
■ N = 50
■ Admitted = True
■ pi = 3.14
■ …
1.
Types of instructions
1.
Types of instructions
1.
Types of instructions
○ The instruction assignment
■ This instruction allows to affect a value to a variable, after
evaluation of a logical or arithmetic expression.
■ It's necessary note that the result of evaluating the expression
must have the same type as the variable that will receive
■ It's necessary note that each variable takes the last value
affected
1.
Test on the assignment statement
● Objective : self- assessment of understanding of the effect of
assignment statements on the state of variables.
Exercise 1
What are the values of the variables a,b and c written by the
following algorithm?
1.
Test on the assignment statement
1.
Test on the assignment statement
Exercise 2
What is the objective of this sequence instructions ?
1.
Conditional Statements
● These instructions are used to make the choice between execution
or not blocks of instructions following the evaluation of a condition.
There are three types :
1. The instruction simple
This type of instructions allows you to choose between
execution or not of a single block of instructions
1.
Conditional Statements
1.'instruction co
2. The instruction alternative
This type of instructions allows you to choose between two blocks
of instructions to execute .
1.
Conditional Statements
1.
Conditional Statements
Example:
Read (x)
Depending on the case (x)
x = 1 : Write ('Very Low')
x = 2 : Write ('Weak)
x = 3 : Write ('Average)
x = 4 : Write ('Good')
x = 5 : Write ('Excellent')
Otherwise
Write ('Value out of accepted range')
End If
1.
Test on the use of conditional statements
1.
Iterative instructions
● Iterative instructions Or repetitive , called Also loops , allow to execute several
times the same block of instructions .
● There are three types :
1. 'For'
In this type of instructions iterative we know the number in advance of
iterations . In In fact , we must specify , in the header of the 'For' instruction,
the value initial and final value and possibly the step
Example :
For i ranging from 1 to 10
do
Writing ('Master BD &
DS')
End for
1.
Iterative instructions
2. The ' While '
Example :
i←1
While (i <= 10) do
Write ('Master BDDS')
i ← i *2
End As long as
1.
Iterative instructions
3. The ' Repeat '
- As with the ' While ' statement , the ' Repeat ' statement does not
allow you to know necessarily in advance the number of iterations .
- We continue the repetition of the execution of the block of
instructions until a condition is met .
1.
Iterative instructions
Example :
i←1
Repeat
Writing ('Master BD&DS')
i←i+1
Up to ( i > 10)
Note : The ' While ' statement executes zero Or several times , at the
time when , the instruction ' Repeat ' is executed a Or several times .
1.