Professional Documents
Culture Documents
Notes
Std. : VIII Sub.: Computer Topic: Ch 8 Using Block statements in Apps
Block Programming
A block or a code block is the basic feature of structured programming. A block is simply a section of a
software code that contains one or more statements and declarations. A block can also be an algorithm or
script and may contain one or more blocks within itself.
Similar to programming in Scratch application, a block allows us to create control structures and improve
the code efficiency. Block-based coding is a term used to describe the process of building a programming
code by connecting multiple blocks of instructions together. In case of app development, block
programming allows us to combine multiple groups of different statements or conditions. This helps to
lower the number of Individual instances of variables and procedures that we need to specify.
Opening the Block Editor
In App Inventor, the Block Editor allows us to arrange the required blocks of code and program the app's
behaviour. Let us program the behaviour of the Start the Quiz button. When clicked, the button leads the
user to Screen 2 that is titled as the Question screen.
To open the Block Editor:
1. Access the MIT App Inventor dashboard. The dashboard appears in th Projects view. App Inventor
dashboard appears in the Projects View.
SGVP International School, Ahmedabad - INDIA 1 of 15
2. Under the My Projects section, click the required project. For example, click EQuiz. The project opens in
Design View displaying the Screen1 titled EQuiz.
3. To open the Block Editor, click the Blocks button. The Block editor for Screen1 appears.
The Block panel displays the blocks used to program general behaviour, as well as the blocks for specific
components added by us. We can drag and drop the required blocks into the Viewer to build relationships
among different statements and conditions.
2. From the Blocks panel, click Button1. The drawer displays the blocks associated with the
Button component.
( Button1 Drawer )
5. From the drawer, select the open another screen block and add it to the When Button1 click block.
7. From the drawer, select the " " block and add it to the When Button1 click event block.
(“ " Block )
(Specifying Screen 2)
SGVP International School, Ahmedabad - INDIA 5 of 15
8. In the " " block, enter the name of the screen that should appear, when we click the button. For
example, enter Screen 2.
9. Press Enter.
10. To test the app, run the emulator.
3. To set the initial value of Questionlndex to 1, from the Math drawer, drag the 0 block and change its
value to 1.
Set the Questions Stages
Once we have set the index variable for the question list, the app needs to increment this variable when
we click the Next button. As the variable changes, the new value indicates the corresponding question as
the current question. As a result, a value new question is displayed on the screen. To set the question
stages: 1. To set an event for the Next button, from the Next drawer, drag the when Next. Click block. 2.
To specify the first question in Question_Label, from the Variables drawer, drag the set to block and select
Question Index.
3. To increment the Question Index value by 1, perform the following actions
a. From the Math drawer, drag the + block.
b. To increase the old value by one, from the Variables drawer, drag the get block and select
global Question Index.
C. From the Math drawer, drag the 0 block and change its value to 1.
4. To display the next question in Question Label, perform the following actions.
a. From the Question_Label drawer, drag the set Question_Label.Text to block.
b. To select the first question from the question list, perform the following
actions. i. From the Lists drawer, drag the select list item block.
SGVP International School, Ahmedabad - INDIA 7 of 15
ii. To specify the question list, from the Variables drawer, drag the get block and select
global Question List.
iii. To specify the index variable for the question list, from the Variables drawer, drag the get
block and select global Question Index.
3. To set the action to go to the Result screen, perform the following steps.
a. From the Control drawer, drag the open another screen with start value block.
b. For screen Name, from the Text drawer, drag the " " block and enter Screen3(Result).
block.
4. To ensure that the start Value changes for every correct answer, perform the following
action. a. From the Variables drawer, drag the set to block and select global Score
Value. b. From the Math drawer, drag the + block.
c. To increase the old score value by one, from the Variables drawer, drag the get block and select
global Score Value.
d. From the Math drawer, drag the 0 block and change its value to 1
(Uploaded Images)
(Image List)
In the Block editor, let us now create a list of images for the app. The first image appears with the first
question as the Screen 2 is initialised. Other images appear as the user clicks the Next button. To link the
image list to the Image1 component
1. To change the picture, from the Image1 drawer, drag the set Image1.Picture to block. 2.
To specify the picture corresponding to the current question, perform the following actions.
a. From the Lists drawer, drag the select list item block. A.
b.To specify a file name from this list, from the Variables drawer, drag the get block and select
Picture List.
c.To specify the current question, from the Variables drawer, drag the get block and select
Questionlndex.
3. To test the app, run the emulator or AI Companion.
SGVP International School, Ahmedabad - INDIA 13 of 15
(Adding the Image List to when next is clicked event)
(Emulator Test)
7. To view the user interface as seen on the mobile screen, run the emulator.
Synopsis
● A block is simply a section of software code that contains one or more statements
and declarations.
● In App Inventor, the Block Editor allows us to arrange the required blocks ot Code and to
program the app's behaviour.
● In Block Editor, we can apply component-specific blocks, allowing us to switch between
separate screens.
● The Question screen is a single-user interface designed to serve as a template for all
questions. ● We can create a list of questions that the app can refer to. The app needs to keep
track of the current question being displayed.
● Once we have set the index variable for the question list, the app needs to increment this
variable when we click the Next button.
Q : D Answer in brief.
(1) List two advantages of using block programming.
(2) What is included in the Block panel?
(3) Explain the need for a global variable in case of questions.
(4) Why is a condition set for the last question?
(5) How is the score tracked?