0% found this document useful (0 votes)
6 views56 pages

02 - CHAPITRE1 - Variables EN

The document provides an overview of algorithms, their structure, and the importance of variables in programming. It explains the definition and properties of algorithms, the declaration and manipulation of variables, and the different data types such as integers, reals, characters, and booleans. Additionally, it covers input-output functions and introduces the String class for handling character strings.

Uploaded by

Jamil Dk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views56 pages

02 - CHAPITRE1 - Variables EN

The document provides an overview of algorithms, their structure, and the importance of variables in programming. It explains the definition and properties of algorithms, the declaration and manipulation of variables, and the different data types such as integers, reals, characters, and booleans. Additionally, it covers input-output functions and introduces the String class for handling character strings.

Uploaded by

Jamil Dk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

CHAPTER 1

THE VARIABLES
You must know that
Writing a program is a complex
operation that requires many steps.
The most important thing is to
understand the end goal and stick to it.
For this, it is often preferable to break
down the desired treatment into a
succession of smaller and simpler
operations.
STRUCTURE OF AN ALGORITHM:

Algorithms are intended to make us think,


but not to run on a computer: for that, it will
be necessary to translate the algorithm into
a programming language.
STRUCTURE OF AN ALGORITHM:

Definitions
ALGORITHM
An algorithm is a series of elementary operations making it possible to obtain the
final result determined for a problem.

Property of an algorithm:
An algorithm, under similar execution conditions (with identical data) always
provides the same result.

Instruction block:
A block of instructions is a processing part of an algorithm, made up of
elementary operations located between Start and End or between braces.
STRUCTURE OF AN ALGORITHM:

The structure of an algorithm is as follows:

Algorithm algorithm -name // header part


Beginning // processing part
Instruction block;
END

Each line has a single instruction


The execution of the algorithm corresponds to the realization of all the instructions,
line after line, from the first to the last, in this order.
STRUCTURE OF AN ALGORITHM:

COMMENTS:
Comments are textual explanations in the algorithm by the
programmer following the two characters // .
They are not executed: they are invisible to the execution of the
algorithm.

These comments will be useful to programmers who want to


understand or modify the algorithm .
STRUCTURE OF AN ALGORITHM:

1 st
algorithm 2nd algorithm
Algorithm algo_hello Algorithm algo_hello2
Beginning Beginning
Write (“hello everyone”); Write (“hello ”) ;
END write(“everyone ”);
END

simple Hello everybody


r

Same
result
DATA

Declaration and use of variables


The values, in order to be able to be manipulated, are
stored in variables.
Variable:
A variable is a memory location that stores a value.
A variable is defined by:

a single name
a unique type of definition
a value assigned and modified during the course of the
algorithm
DATA

The syntax
The structure of an algorithm (declaring a variable
named index and of integer type ) is then the
following:
Algorithm algorithm -name // header part
Variables: index : integer // variable declaration part
Beginning // processing part
Instruction block;
END
DATA

The name – the type – the value


Name of a variable - identifier
of a variable:
Start with a lowercase
The name of a variable makes
it possible to uniquely identify
it during the algorithm.
To facilitate the reading of the
algorithms, it is advisable to
Rules Does not include spaces

respect rules for naming the If the variable name is made up of several words,
variables . each must begin with a capital letter and not
include hyphens.

You must also be careful to give the variables an explicit


name (proscribe i2, zz2,..)
DATA

The name – the type – the value


Type- domain of definition
The type (also called domain of definition) of the variable indicates the set of values that the
variable can take.
Variables can belong to several domains (integer, real, character, boolean, etc.).
Variables change value through the assignment operation.

Assignmen
t

Assignment is an operation that sets a new


value to a variable. The assignment symbol is
.
DATA

The name – the type – the value


Determination of variables
Consider the following problem: calculate and write the double of a given real number.
The structure of the algorithm is as follows:

Dual
Variables: number, result: real;
Beginning Variable declaration
number  7; number=? result=?
result  number x 2; number= 7 result= ?
write(result); number= 7 result= 14
END number= 7 result= 14
The variables no longer exist
The algorithm proceeds sequentially
DATA

The name – the type – the value


Mistakes to avoid

A variable is declared only once in an algorithm

A variable is declared at the beginning of the algorithm and not in the part reserved for
processing instructions.
Before you can use a variable, you must have declared it in the variable block.

Before you can use the value of a variable, a value must be assigned to it.
DATA

The name – the type – the value


Mistakes to avoid

Variable-error
Variables: number, result: real;
Beginning result  number x 2;
value  1; error : number has no
END value
error : value has not been set
Types

 Types can already be Predefined :


They are called the “ Predefined Types” of the language
Example: type Integer, Real,…

 ..Types defined (by you) :


Do not exist in your language, you must create them.
Example: Human type; Bill;…
Types
Operations allowed on integers?

The standard operations{+,-,*}.


Div : i  9 div 2; ( i will be equal to 4)
Mod : i  9 mod 2 (i will be equal to 1)
abs(x) absolute value of x.
pred (x):x-1.
succ (x): x+1.
odd (x) true if x is odd, false otherwise.
sqr (x) the square of x.
Types
 The equality operator:
This is the operator found in all simple types that allows you to know if the
two operands are equal

It is represented by the character =


The result of an expression containing this operator is a boolean

 We also have the inequality operator : ≠


 And for types with an order the comparison operators

<, ≤ , >, ≥
Types
The real type is larger than the integers but also
has representation limits.

Multiplication program Arithmetic operator:


Var Radius, :integer; +,-,/,*
Var Perimeter: Actual;
const Pi:Real ; Relationship Operator:
Beginning =, ≠ <, ≤ , >, ≥
Radius  5;
Pi  3.147;
Perimeter 2*P i*Radius;
Write('Your perimeter is', Perimeter, 'cm');
END.
DATA

Types of variables
The real type and the integer type

The real type


– the integer the numeric type variables used in the
algorithm have as usual domains those
type provided by mathematics: real or integer.

Real-type
Variables: number1, number2, result: real;
var1 : integer;
Beginning
number1  1.2;
number2  15;
var1  2;
result  number1/number2 x var1
END
DATA

Types of variables
The real type and the integer
type
Conversion
Converting an integer to a real
Numerical-conversion algorithm
is natural: this operation does Variables: number1: integer;
not cause any loss of number2: real;
information; For example, the Beginning
integer 15 will become 15.0. number1  15;
Converting a real to an integer number2  number1; //number2 is 15.0
results in a loss of number1  number2 + 0.5 ; //impossible error
information: the decimal END
digits are lost. For example,
the real 15.75 will become 15.
DATA

Types of variables
The Character type

The
Character This is the domain consisting of alphabetic,
type numeric, and punctuation characters.

DO NOT CONFUSE
BETWEEN

The AND
character the whole
_
3
'3'
DATA

Types of variables
The Character type
The only elementary operations for character type elements are the comparison operations:


In fact, each character is associated with a unique whole numeric value (the ASCII code
establishes this correspondence: for example, the letter 'A' corresponds to the value 65).
To write an algorithm, we don't have to know the values of the ASCII table by heart. But
we will use three principles:
The integers corresponding to the characters 'A','B'…'Z' follow each other in this
order.
The integers corresponding to the characters ' a','b '…'z' follow each other in this
order.
The integers corresponding to the numeric characters '0' to '9' follow each other in
this order.
DATA

Types of variables
Conversion
So to convert a lowercase character to uppercase, just add the difference between
them: 'c'+ (' A'-'a ') equals 'C'
Character to integer type conversion: to convert the character '3' into an integer
value 3, simply calculate the difference between the two characters: '3'- '0', which
is equal to 3.
Conversion from integer to character type: to convert the integer 3 into a value of
the character '3', simply calculate the sum between the two characters: 3+'0',
which equals '3'.
DATA

Types of variables
Conversion
Integer-character-conversion
Variables: number1: integer;
char: character ;
Beginning
char  '3';
number  '3' – '0'; //number is 3
number  number + 2; // number is 5
char  '0' + number; // because is '5'
END
DATA

Types of variables
The Boolean type

The Boolean
The domain of booleans is the set formed of the
type only two values (true, false).

The admissible operations on the elements of this domain are carried out using all the
logical connectors, denoted: AND Fake TRUE
AND: for the “logical and” operation
OR: for the “logical or” Fake Fake Fake
NO: for the “logical no” TRUE Fake TRUE

OR operation Fake TRUE


Fake Fake TRUE
TRUE TRUE TRUE
Types
 Distributivity of 'and' and 'or'
 Reminders on Boolean logic...
operators
Possible values: True or False a or (b and c) = (a or b) and (a or c)
a and (b or c) = (a and b) or (a and c)
 Associativity of operators and and
Or  Involution
no no a = a
a and (b and c) = (a and b) and c

 Commutativity of operators and and


or
a and b = b and a
a or b = b or a
Types
Priority between Arithmetic operator
A  2; A =2;
BA+2; B=4;
BBA*3; B=-2;
C(BA)*3; C=-12;
DA-8 div B mod C; D=-2;
DD+A mod 9*3; D=12;
D D*(5 div 8)-2*3; D =-6;
DATA

Types of variables
The Boolean type - Example
Boolean-type algorithm
Variables: boolean1, boolean2: boolean;
Beginning
boolean1  5<6; //boolean1 evaluates to True
boolean2  NOT boolean1; // boolean2 evaluates to False
boolean2  (5<7) OR (3>8); //boolean2 evaluates to True
boolean1  true; // boolean1 evaluates to True
END
Priority between Logical operator
A  True; A = True;
BFalse; B=False;
BB and A or not (A); B=False;
C(5=4) or (2>3); C=False;
D(4<>4) or not(C); D=True;
E3;F5; E=3;F=5;
G(F+E div 2=2) or not(C) G=True;
and D
DATA

Mistakes to avoid
During an assignment, the value of the right part must be of the type of the
variable whose value is modified.

error-type
Variables: char: character ;
Beginning
because  1.56; //error: because is not a real
because  5<8;
// error: because is not a boolean
END
INPUT-OUTPUT FUNCTIONS

The programs frequently use instructions allowing the


display on the screen and the entry of values on the
keyboard by the user.
We are going to provide ourselves with two analogous
operations allowing us to simulate:
Displaying a sentence with the write();
Entering a value by the user with the read() instruction.
INPUT-OUTPUT FUNCTIONS

The read function


The user data entry instruction is:
get( variableName )

The execution of this instruction consists of:


1. Ask the user to enter a value on the input device
2. Modify the value of the variable passed between parentheses
INPUT-OUTPUT FUNCTIONS

The read function


The on-screen display instruction (output device) of an
expression is:
write(expression)
This statement simply performs the display of the
expression passed between parentheses.
INPUT-OUTPUT FUNCTIONS

Example
READ AND
WRITTEN

Alg orithme example-read-write


Variables: nb: real;
Beginning
read( nb) //the user enters the number on the keyboard
write(' 'the value of nb''); //a sentence is displayed on the screen
write (nb); // a value is displayed on the screen
write (''the value of nb is: '', nb) ; //a sentence followed by the value //are displayed on the screen
END
INPUT-OUTPUT FUNCTIONS

Exercise
Write an algorithm that asks the user to enter three real
numbers on the keyboard and displays the sum of these
three numbers on the screen ;
Sum-three-real
Variables: nb1, nb2, nb3, sum: real;
Solution

Beginning
read(nb1);
read(nb2);
read(nb3);
sum  nb1+nb2+nb3;
write(sum);
END
TYPES : A TOOLBOX

Character strings
Introducing the String class
A character string is composed of alphanumeric characters forming a
word or a sentence.
It is impossible to manipulate character strings with the usual
operations defined for reals or integers: the String class therefore
provides us with specific operations.
TYPES : A TOOLBOX

Character strings
Introducing the String class
The string class user interface (the set of operations defined to
manipulate them) is described as follows :
String () allows to create a string in memory
Write(): empty allows to write the string on the screen
Read ():empty allows the user to enter the content of the string
Length(): integer provides the number of characters in the string
iethChar (integer ): character provides the character which is at the position passed as
parameter (the first is at position 0)
modifyIth (integer, character ): empty replaces the character located at the position
given in parameter (the first character is at position 0)
concatenate (String): empty modifies the string by juxtaposing the string passed in
memory
TYPES : A TOOLBOX

Character strings
Using a string
Let's explain how to use a string in an algorithm through declaration
and usage examples. Manipulating a string requires two steps:
1. You have to create the chain. Indeed, the simple declaration in
the variable block is not enough to create it.
2. We can then use the chain thanks to the methods of the user
interface.
TYPES : A TOOLBOX

Character strings
Using a string
Statement Algorithm creation -of-the-chain
A String type variable is declared as variables: last name, first name, brother:
follows : String
Variables : variableName : String; Beginning
Then, we actually build the object name  new String (''Tlemsani '');
(also called instance) in the body of //the name variable is initialized and contains ''Tlemsani''
the program with the new operation:
variableName  new String(); brother  new String(name);
Strings must be initialized before //the brother variable also contains ''Tlemsani''
using them. For example, the
lastname, firstname and brother first name  new string();
variables are initialized by the // this string is initialized, but empty
following three methods: END
TYPES : A TOOLBOX

Character strings
Using a chain
Statement
Each new instruction triggers in the memory the creation of a reserved area
schematized by a box containing the string created. Let us represent the state of the
memory at the end of the execution of the previous algorithm.
Creation-of-the-chain algorithm
Variables Instances

name “Tlemsani”

brother “Tlemsani”

first name «»
TYPES : A TOOLBOX

Character strings
Using a chain
Use
The method works in association with a specific box (specified during the call) of the
memory schema .

use-of-the-string algorithm
variables : lastname , firstname: String;
lg: integer;
char: character ;
Beginning
name  new String();
name.read (); //the user enters what he wants
lg  name.length (); // lg contains the length of the word name
because  name.iemeChar (3); // because contains the 4th letter of the word name
// specify that the first is at index 0
name.modifierIth (5,'t'); // the 6th letter changes
name.write (); // we write the word
END
TYPES : A TOOLBOX

Character strings
Using a chain
Mistakes to avoid:
Do not forget to create the chain before applying an operation to it
Specify on which chain the operation is applied (do not forget the dot “.”
in front of a method (instance).
Always put parentheses
Use suitable parameters for iemeCar and modifyIeme operations
To read and write strings, use the read and write methods and not the
read and write I/ O functions .
TYPES : A TOOLBOX

Character strings
Using a chain
Mistakes to avoid:
Here is an algorithm with five method usage errors:
Algorithm use-string-with-errors
variables: name : String;
lg: integer;
char: character ;
Beginning
name  new String();
lg  firstname.length (); //the string firstname was not created
lg  length(); // the length of what?
lg  name.length ; // the parentheses are missing
because  name.iemeChar (); // you must specify the number of the letter
read(name); // you must use the operation: name.read ()
write(name); // you must use the operation: name.write ();
END
MEMORY SCHEMES

It is essential to know the state of the variables during the


execution of an algorithm thanks to a memory diagram .

The memory schema of an algorithm


represents all the variables and their
values at a specific stage in its execution.
MEMORY SCHEMES

A memory diagram delimits three distinct parts:


1. The name of the algorithm
2. The variable part where all defined variables will be
represented by:
• A value (or by “?” if the variable does not yet have a value) for
primitive type variables;
• An arrow (pointing to a box drawn in the right part of the diagram) for
String object type variables.
3. The part of the instances where each box will have
been created by using the new operator: there are as
many boxes to represent as there are new in the
algorithm.
MEMORY SCHEMES

Some common mistakes to avoid:


Forgetting to represent a variable;
Giving a bad value to a variable;
Represent strings without an associated box ;
Forgetting to specify the step (during the course of the
algorithm) represented by the memory diagram .
THE ARRAY TYPE

The array type is used to store values of the same type


using a single variable .
The array type

An array structures a set of values of the same


type accessible by their position.

Dimension, type and index of an array:


Statement

The maximum number of array elements, which


is specified in the definition, is called its
dimension. The type of its elements is called the
type of the array. To access the elements of an
array, an index indicates the rank of the element.
THE ARRAY TYPE

An array is defined in two stages. The array named tab is


declared as follows, along with the base type of its
elements. This is a variable declaration:
Variable : tab: domain array[];
Then the statement in the body of the program which
actually implements the array, i.e. which reserves space in
memory, is constructed with a new operator acting in the
execution environment of the program:
tab  new domain[10) ;
// the array tab has a dimension of 10 elements
THE ARRAY TYPE

The following example represents a tab array of 10


integers
tab= : 22 -7 56 12 0 23 -4 1 57 35
clues [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Using a single array variable tab, it is possible to manipulate


several different values. For example: tab[0) is seen as an
independent variable with the value 22 .
THE ARRAY TYPE

Using a table
One-dimensional array
Handling the elements of the tab array is described in the
following example: a single variable is used to store 4 whole
notes.

Table use algorithm


Variables: notes: array[] of integers;
Beginning
notes  new integer[4];
grades[0]  12;
grades[1]  14;
grades[2]  10;
notes[3]  18;
END
Memory scheme
THE ARRAY TYPE

Using a table
Two-dimensional
If we want to write a program that works with a 3 by 3 checkerboard containing
integers, we will introduce a checkerboard instance in the form of a 3 by 3
array. Let's write the algorithm modifying three elements of the array.

Checkerboard algorithm
Variables: checkerboard: array[][] of
integers;
Beginning
checkerboard  new integer[3][3];
checkerboard[0][0)  0;
checkerboard[1][1)  1;
checkerboard[0 ][1)  1;
END
Memory scheme
EXCHANGE TWO VARIABLES

Knowing how to exchange the content of two variables is a


fairly simple technique, which you need to master .

The principle is identical in algorithmic: it is necessary to


introduce a temporary variable (of the same type as the
two others) which will store a value .
EXCHANGE TWO VARIABLES

You obtained,
during the Algorithm swap -two-integers
Variables: value1,value2,temporary: integer ;
execution of an Beginning
algorithm, two value1  155;
integer variables value2 3;

value1 and value2. Temporary  value1;


You want to Value1  value2;
exchange their Value2 temporary;
END
respective
contents:
EXCHANGE TWO ITEMS

The same principle applies to exchange two strings:

Algorithm exchange-two-channels
Swap-two-strings Variables Instances
Variables: value1,value2: string 3
value2 “Tlemsani”
temporary: integer ;
Beginning
value1  new String(''Tlemsani ''); value1 “ Merad ”
value2  new String('' Merad '');
1
temporary
Temporary  value1; 2
Value1  value2;
Value2 temporary;
END Memory scheme
ISSUES??

You might also like