This project has been funded with support from the European Commission. This publication reflects the views only of the author, and the Commission cannot be held responsible for any use which may be made of the information contained therein. PROJECT TITLE PROJECT ACRONYM DATE OF DELIVERY AUTHORS ALEXANDRU STRUNGĂ EDITORS INTELECTUAL OUTPUT AVAILABILITY OF DELIVERABLE MODULE 1: BASIC DIGITAL SKILLS UNIT 3: ALGORITHMS, LOGICAL BLOCK SCHEMAS, CONTROL STRUCTURES
21
Embed
MODULE 1: BASIC DIGITAL SKILLS UNIT 3: · PDF fileLogical schemes are graphical notations made up of blocks linked to each other by arrows. ... Flowcharting (or elaborating logical
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
This project has been funded with support from the European Commission. This publication reflects the
views only of the author, and the Commission cannot be held responsible for any use which may be made of
the information contained therein.
PROJECT TITLE
PROJECT ACRONYM
DATE OF DELIVERY
AUTHORS ALEXANDRU STRUNGĂ
EDITORS
INTELECTUAL OUTPUT
AVAILABILITY OF DELIVERABLE
MODULE 1: BASIC DIGITAL
SKILLS
UNIT 3: ALGORITHMS, LOGICAL
BLOCK SCHEMAS, CONTROL
STRUCTURES
Module/Unit: 1.3
https://www.silvercodeproject.eu/
Table of Contents
Entrance 2
Keywords 2
I. Module/unit overview 2
II. Learning content 2
Introduction (short description) 2
Learning Objective 3
1. Introduction to basic programming concepts 3
Variables 3
Constants 4
Keywords 4
Data Types 4
Operators 5
2. Introduction to algorithms 8
3. Logical block schemas 9
4. Testing a very simple algorithm in JavaScript 10
5. Control structures 11
Summary of Key Points (what did the participant learn) 17
Learning Objective: What you will learn in this module/unit? - To define variables, constants, keywords, operators; - To define an algorithm; - To give real-life examples of algorithms; - To understand what is a logical block schema; - To use a logical block schema with a mathematical expression; - To construct a logical block schema for a simple application; - To appreciate the importance of algorithms and control structures in coding.
1. Introduction to basic programming concepts
Variables Like many other programming languages, JavaScript has variables. Variables can be
thought of as named container or storage for data. We can put data into these containers and then refer to the data simply by naming the container. The name of a variable, that will always start with a letter, can contain: upper-case letters, lower case letters, numbers and “..” character (underscore).
The statement below creates (in other words: declares or defines) a variable with the name “message”:
var message;
The semicolon “;” is used in JavaScript and other programming languages for a statement or a command given to computer. The comma operator “,” evaluates each of its operands (from left to right) and returns the value of the last operand.
Now we can put some data into it by using the assignment operator =, which we will
discuss in more detail further in the unit:
var message;
message = 'Hello'; // stores the string
The string is now saved into the memory area associated with the variable. We can access it using the variable name:
var message;
message = 'Hello!';
alert(message); // shows the variable content
To be concise we can merge the variable declaration and assignment into a single line:
var message = 'Hello!'; // define the variable and assign the
value
alert(message); // Hello!
Now, let’s try to create a HTML document and check if the code is executed properly.
First, we need to write the following code in Notepad++. We will save this as an HTML file
and proceed to run it in Chrome.
<script>
var message = 'Hello!'; // define the variable and assign the
The notion of algorithm originates from the name of Arab mathematician and astrologist Al-Khwarizmi who lived in the 9th century A.D.
There are several modalities of understanding the notion of algorithm: (1) A thinking pattern used for resolving a problem in a finite number of steps; (2) A way of thinking – algorithmic thinking;
We can think of several ways of expressing algorithms: (a) Natural language
The algorithms for how to calculate arithmetic mean of two numbers is: 1) Start; 2) Read the first number; 3) Read the second number; 4) Calculate their sum; 5) Divide the result to 2; 6) Display the calculated result; 7) Stop.
(b) Pseudocode
Pseudocode is an informal high-level description of the operating principle of a computer program. The algorithm for how to calculate the largest number in a list of numbers of random order is:
Logical schemes are graphical notations made up of blocks linked to each other by arrows.
A fragment of the logical block schema for the module function is the following figure:
3. Logical block schemas A logical block schema graphically describes the steps of an algorithm and processing
executed on the data. There are several categories of data included in the logical block schemas:
(1) variables: memory zones that are changing values and are characterized by a name; The name can be a combination of lower case letters, upper case letters, numbers and “_” sign (underscore). Examples: aria, Aria, perimeter, volume, length etc.
(2) constants: memory zones with fixed value, without name (explicit constants) or memory zones with fixed value and name (symbolic constants). Examples: π (PI)
The basic operations of logical block schemas are the following:
In logical block schemas, we will meet many of the elements described above.
Flowcharting (or elaborating logical block schemas) has many advantages: (a) communication: flowcharts are better way of communicating the logic of a system to all concerned; (b) effective analysis: with the help of flowchart, problem can be analyzed in more effective way; (c) proper documentation: program flowcharts serve as a good program documentation, which is needed for various purposes; (d) efficient coding: the flowcharts act as a guide or blueprint during the systems analysis and program development phase; (e) proper debugging: the flowchart helps in debugging process; (f) efficient program maintenance: the maintenance of operating program becomes easy with the help of flowchart; it helps the programmer to put efforts more efficiently on that part.
4. Testing a very simple algorithm in JavaScript
An example of using a very simple algorithm (using logical block schema, pseudocode and JavaScript code), is to find out the sum of two numbers.
Here are the lines of code that we can test ourselves by running Chrome: <script>
var n1 = Number(prompt("Enter first number"));
var n2 = Number(prompt("Enter second number"));
var sum = n1+n2;
console.log(sum);
</script>
Let’s try to explain what’s happening step-by-step: 1. we declared two variables (n1 and n2) and we asked the user for a value for each one
of them; 2. we declared that the variable sum is the sum of n1 and n2; 3. the output is displayed (the sum of n1 and n2).
5. Control structures A control structure is a block of programming that analyzes variables and chooses a
direction in which to go based on given parameters. The term flow control details the direction the program takes (which way program control "flows"). Hence it is the basic decision-making process in computing; flow control determines how a computer will respond when given certain conditions and parameters.
The flow of control through any given function is implemented with three basic types of control structures:
1. Sequence: sequential execution of code statements (one line after another) -- like following a recipe
2. Selection: used for decisions, branching -- choosing between 2 or more alternative paths. In Javascript, for exemple, we can use different types of selection statements:
if else if switch
3. Looping (or repetition): used for looping, i.e. repeating a piece of code multiple times in a row. In Javascript, we can use loop statements such as:
while do/while for
The sequence is the simplest control structure, an abstract notation used to define other control structures (represented in the above figure). While not directly used in logical schemas, it usually include basic operations such as: one reading, two attributions, one decision.
Here is an example of JavaScript sequence! In first line of code we declare a new variable (averageGrade) that the user must input its value in the JavaScript prompt; in the third line of code, we add 1 to the value of averageGrade so the current value is 2; in the fourth line of
code we display the value of the variable. <script>
var averageGrade = Number(prompt("Guess the number from
Here is a JavaScript example for selection! In first line of code, we declare a new variable
(averageGrade) that the user must input in the JavaScript prompt; in the next lines of code we have two possible situations: if the averageGrade is equal to 8, the message “You won!” will
Pseudocode read a if a==8 write “You won!” end if else write “You lost!” end else
be displayed; (2) if the averageGrade is less or more than 8, the message “You lost” will be displayed.
A loop in coding is an instruction that repeats until a specified condition is reached. In a loop control structure, the loop asks a question. If the answer requires an action, it is executed. The same question is asked again and again until no further action is required. Each time the question is asked is called an iteration. According to the position of the sequence included in the loop, we can identify: (a) loops with initial test and (b) loops with final test. A third type of loop is the loop with counter. It has a more complex structure, and it is based on a counter that (1) has an initial value; (2) go through all the values of a continuous range (3) until a final value is reached and (4) every step is executed in the same sequence.
<script>
var averageGrade = 8;
for(var i=0; i<averageGrade; i++)
{
console.log(i);
}
</script>
Here is a JavaScript example for a loop or repetition! In first line of code, we declare a
new variable (averageGrade) that is equal to 8; in the next lines of code we have a loop that repeats if the condition are satisfied. The condition is that i must be less than the value of averageGrade which is 8. If so, the number will be displayed on the screen. In the end, 8 numbers will be displayed (0,1,2,3,4,5,6,7), after each loop.
Summary of Key Points (what did the participant learn)
The participant learned the definitions of basic programming concepts, algorithms, logical block schemas and control structures and how can be used in coding. The participant has also started to use logical block schemas in describing simple real-life tasks or basic applications.
III Conclusion
Defining, understanding and using algorithms is a key basic digital skill in coding. Only if the control structures are clearly defined and coherently expressed, a coding activity can be carried out successfully.
IV Further resources / Additional reading
Further resources To directly try the applications from chapter 3 we can test the code in a virtual
JavaScript compiler, which is available online here. What result do you get when
inserting the code individually, for each algorithm?
Additional reading
McPeak, J. & Wilton, P. (2015). Beginning Javascript. Indianapolis: John Wiley &
Sons.
Groner, L. (2016). Learning JavaScript Data Structures and Algorithms. Birmingham:
Packt Publishing Ltd.
Larsen, J.R. (2016). Get Programming with JavaScript. Shelter Island: Manning