Logic and Programming
Block-based Programming
What is Programming Language?
- A programming language is a set of rules used to
write instructions for the computer.
Python JavaScript C#
2
Block-based Programming
- Text-based coding is not easy.
- You need to remember the language rules and follow them
precisely.
- Block-based programming allows us to create code that is
both visual (simple) and (powerful).
- The developer just needs to connect visual “building blocks”
in a logical way.
3
Block-based Programming with Blockly
- Blockly is an example of Block-based programming.
- A project of Google, it is free and open-source
software
- It typically runs in a web browser.
- To try it:
https://blockly-
demo.appspot.com/static/demos/code/index.html
4
Variables
• A variable is a named location in memory that stores a value.
• The value being stored can change while the program is
running.
5
6
5
X
X
5
What are variables used for?
• Variables allow us to create programs based on
information that we know we will have, even if we don't
know what the value will be.
6
Programs: Sequence, Selection, and Loops
• All programs are made of the following:
A program
اﻟﺘﺴﻠﺴﻞ اﻟﺘﺤﺪﻳﺪ اﻟﺤﻠﻘﺎت اﻟﺘﻜﺮارﻳﺔ
Sequencing Selection ( )اﻟﺘﻜﺮار
Loops
(iteration)
7
1- Sequencing
• Here are some simple statements
Print 3
Print Hello
Print 3 + 5
Print (3*12) + 5
8
2- Selection
• Selection is a fancy way of saying "rule" or "condition."
• If it’s cold outside, wear a jacket.
If degree < 20
Wear a jacket
If degree > 30
Put a sunscreen
9
2- Selection
If degree < 20
Wear a jacket
10
3- Loops
- Loops allow us to repeat code. Whether it's a single
statement or a large block of code with several statements.
- Being able to repeat code in programs easily is extremely
helpful.
11
Loops Types
- repeat # times: repeats a set number of times
- repeat while/until: repeats while or until a condition
is true
- count with: repeats from a specified start to end by
a specific amount
12
repeat # times
13
repeat while/until
14
count with
15
Example 1
- Write a program to ask the user to enter 2 numbers
and display the largest number.
16
Example 2
- Write a program to print the numbers from 1 to 10.
17
Example 3
- Write a program to print the sum of the numbers
from 1 to 10.
18
Answer the following questions:
- What would the following block print?
14
19
Answer the following questions:
- Which of the following blocks would print 12?
20
Answer the following questions:
- Which values of a and b would get 'a' to print given
the block below?
a is 4, b is 3 a is 3, b is 4 a is 4, b is 4 a will never be printed
21
Answer the following questions:
- Which values of a and b would get 'b' to print given
the block below?
a is 4, b is 3 a is 3, b is 4 a is 4, b is 4 a will never be printed
22
Answer the following questions:
- Given the block below, how many times would 'a'
print?
0 4 5 6
23
Answer the following questions:
- Given the block below, how many times would 'a'
print?
0 3 4 5
24
Answer the following questions:
- What will be the output of the following program?
- The number is even
25