0% found this document useful (0 votes)
10 views15 pages

Seance 2

Uploaded by

floppegg
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)
10 views15 pages

Seance 2

Uploaded by

floppegg
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/ 15

I - Basic Elements of an Algorithm

1- Variables
Variable : is a named storage location that holds a value.
• When a variable is created in a program, the programming
language reserves a portion of the computer's memory to store
the data associated with that variable.
1- Variables
Characteristics

• Name: A variable appears in programming under a variable name.

• Type: To distinguish the various possible contents, different types of variables are used

(integers, float, strings, Booleans, etc.).

• Value: Once the variable is defined, a value is assigned to it.

• An address: This is the location in the computer's memory where the value of the

variable is stored.
1- Variables
Name

• Meaningful: Example: “age”, “total_sales”, “user_name”

• Case Sensitivity : Some languages are case-sensitive, so variable names like “my_variable” and “My_Variable” would be

considered different variables.

• Length: Keep variable names of appropriate length for clarity. Example: “total_sales_amount” is clearer than “tsa”.

• CamelCase or underscores : Choose a consistent naming convention. Example: “HelloOver”, is clearer than “helloover”
1- Variables
Types

Simple Types (Integer, Float, Character)

• Integer : Used to manipulate positive or negative integers. For example: 5, -15, etc.

• Float : Used to manipulate numbers with a decimal point. For example: 3.14, -15.5, etc.

• Character: Used to manipulate alphabetic and numeric characters. For example: 'a', 'A', 'z', '?', '1', '2', etc.

• String: Used to manipulate strings of characters representing words or phrases. For example: "Hello, World".

• Boolean: Used for logical expressions. There are only two Boolean values: true and false.
1- Variables
Types
Corresponding Symbol or
Type Possible Operations Keyword
Addition +
Subtraction -
Multiplication *
Division /
Integer, real, float
Integer Division DIV
Modulus (Remainder) MOD
Exponentiation (Power) ^
Comparisons <, =, >, <=, >=, <>
character Comparisons <, =, >, <=, >=, <>
String Concatenation (addition) + (In Lua language : ..)
Boolean Logical Comparisons AND, OR, NOT
2 - Assignement
Assignment (FR: Affectation) : Physically, it allows storing a value in a variable.

Symbolized in algorithms by "←", it specifies the direction of the assignment.

Syntax: Variable ← Expression

Expression can be either:


• Constant
• Arithmetic expression (A+B, C/A…)
• Logical expression (Boolean True or False..)
2 - Assignement
Assignment (FR: Affectation) : allows assigning a value to a variable. Physically, it allows storing a value in a variable.

Symbolized in algorithms by "←", it specifies the direction of the assignment.

Syntax: Variable ← Expression

Example:
0 Algorithm Calculation
1 Variables A, B, C: integer
2 Start
3 A ← 10
4 B ← 30
5C←A*B
9 End
2 - Assignement
Assignment (FR: Affectation) : allows assigning a value to a variable. Physically, it allows storing a value in a variable.

Symbolized in algorithms by "←", it specifies the direction of the assignment.

Syntax: Variable ← Expression

Exercice :
Observe the evolution of different variables during the execution of the following instructions.

1. X←5
2. Y←7
3. Z←4
4. X←Y+Z
5. Z←2*X+Z
6. Y←Z/2
7. X←X+Y
2 - Assignement
Solution: Let's observe the evolution of the variables during the execution of
Exercice : each instruction:
1. X←5 1. X ← 5
2. Y←7 X=5
3. Z←4 Y←7
4. X←Y+Z X = 5, Y = 7, Z = ?
5. Z←2*X+Z 2. Z ← 4
6. Y←Z/2 X = 5, Y = 7, Z = 4
7. X←X+Y 3. X ← Y + Z
X = 11, Y = 7, Z = 4
4. Z ← 2 * X + Z
X = 11, Y = 7, Z = 26
5. Y ← Z / 2
X = 11, Y = 13, Z = 26
6. X ← X + Y
X = 24, Y = 13, Z = 26
At the end of the execution of all instructions, the values of the variables are:
X = 24, Y = 13, Z = 26
2 - Standard Inputs/Outputs :
Standard input statement: The input statement allows the user to enter data
from the keyboard to be used by the algorithm.
This statement assigns (assigns) a value entered via the keyboard to a variable.

Syntax: Read (identifier)


Example: Read (A) : allows the user to enter a value from the keyboard which
will be assigned to the variable A.

This statement allows the user to enter a value from the keyboard which will be
assigned to the variable A.
2 - Standard Inputs/Outputs :
Output statement: The output statement allows displaying information to the user on the
screen.
Syntax : Display (expression)
Example:
Display (A) : This statement allows displaying the value of variable A on the screen.

This statement allows displaying the value of variable A on the screen.


Exercise 1 : Calculate the Sum of Two Numbers.
Write a program that declares two integer variables num1 and num2 entered by the user,
assigns values to them (ask the user to type them), calculates their sum, and then prints the
result.

1.Input:
1. Ask the user to enter the first number (A).
2. Ask the user to enter the second number (B).
2.Processing:
1. Calculate the sum (S) by adding A and B.
3.Output:
1. Display the sum (S).

ALREADY DONE
Exercise 2 : Write a program that declares a float variable price and an integer variable quantity,
assigns values to them, calculates the total cost, and then prints the result.

ALREADY DONE
TO DO
Exercise 3 (easy) : Write a program(Algorithm + Code Lua) that declares a string variable name and an
integer variable age, assigns values to them, and then prints a message using concatenation(means
“string”..”string”) (e.g., "My name is [name] and I am [age] years old.").

Exercise 4 : Write a program that swaps the values of two integer variables a and b with and without using
a third variable.
Task : write two programs ((Algorithm + Code Lua), first one : using a third variable (temporary),
the seconde one : without using the third variable (this is just a trick in Lua, you will discover it)

You might also like