02 - CHAPITRE1 - Variables EN
02 - CHAPITRE1 - Variables EN
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:
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:
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.
1 st
algorithm 2nd algorithm
Algorithm algo_hello Algorithm algo_hello2
Beginning Beginning
Write (“hello everyone”); Write (“hello ”) ;
END write(“everyone ”);
END
Same
result
DATA
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
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.
Assignmen
t
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
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
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
The real type is larger than the integers but also
has representation limits.
Types of variables
The real type and the integer type
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
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;
BFalse; B=False;
BB and A or not (A); B=False;
C(5=4) or (2>3); C=False;
D(4<>4) or not(C); D=True;
E3;F5; 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
Example
READ AND
WRITTEN
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
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.
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
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;
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??