0% found this document useful (0 votes)
8 views10 pages

Prog 1 Q2 Mod 2 WK 34

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

9

Programming 1
Grade 9 – Special Science Program
Quarter 2 Module 2 Week 3&4
Elements of Small Basic Program
Programming 1 – Grade 9 (Special Science Program)
Alternative Delivery Mode
Module Writer: Noli A. Esmeña
Quarter 2 – Module 2, Week 3&4: Elements of Small Basic Program
First Edition, 2020

Republic Act 8293, section 176 states that: No copyright shall subsist in any work
of the Government of the Philippines. However, prior approval of the government agency or
office wherein the work is created shall be necessary for the exploitation of such work for a
profit. Such agency or office may, among other things, impose as a condition the payment of
royalties.

Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand names,
trademarks, etc.) included in this module are owned by their respective copyright holders.
Every effort has been exerted to locate and seek permission to use these materials from their
respective copyright owners. The publisher and authors do not represent nor claim
ownership over them.

Published by the Department of Education


Secretary: Leonor Magtolis Briones
Undersecretary: Diosdado M. San Antonio

Printed in the Philippines by


Department of Education – Region I – Division of Pangasinan II
Office Address: Canarvacan, Binalonan, Pangasinan
E-mail Address: pangasinan2@deped.gov.ph

Reminders in using this module:

1. Use the module with care. Do not put unnecessary mark/s on any part of the
module. Use a separate sheet of paper in answering the exercises.
2. Don’t forget to answer What I Know before moving on to the other activities included
in the module.
3. Read the instruction carefully before doing each task.
4. Observe honesty and integrity in doing the tasks and checking your answers.
5. Finish the task at hand before proceeding to the next.
6. If you encounter any difficulty in answering the tasks in this module, do not hesitate
to consult your teacher or facilitator. Always bear in mind that you are not alone. We
hope that through this material, you will experience meaningful learning and gain a
deep understanding of the relevant competencies. You can do it!

What I Need to Know

This module will show you the elements of a small basic program which will help you
revisit your knowledge on what makes a real computer program.
The module consists of two (2) lessons, namely:
• Lesson 1 – Introducing Variables in Small Basic
• Lesson 2 – Operators and Control Structures of Small Basic
After going through this module, you are expected to:
1. define variables
2. distinguish the use of variables in program statements
3. determine the rules in naming variables
4. explain the meaning of the relational and logical operators
5. define control structure
6. distinguish between the types of control structure
7. determine the needed input and output for simple programming problems
8. use variables, numbers, characters, constants, operators, commas, and
semicolons correctly in programs

2
What I Know

Multiple Choice
Directions: Before you study this module, answer this to test your existing knowledge.
Write the capital letter of your chosen answer on a separate sheet of paper.

1. Which can hold or store information?


A. Variable B. Operators C. Structures
2. Which of the following is a role of variables in program statements?
A. Operator B. Object C. Storage
3. In the statement: name = TextWindow.Read(), which is used as a variable?
A. Name B. TextWindow C. Read
4. A variable CANNOT contain ___________.
A. spaces B. digits C. letters
5. Which is a qualified variable?
A. I love TNHS B. ILoveU C. Else
6. Which structure is used in creating loops in programming?
A. Sequential B. Selection C. Repetition
7. Which structure can be used for procedural-driven programming?
A. Sequential B. Selection C. Repetition
8. Which specify the flow of control in programs ?
A. Logic B. Structure C. Either A or B
9. Less than and greater than operators are classified as ______________.
A. arithmetic operators B. logical operators C. relational operators
10. Which structure can be used for If, Then and Else statements?
A. Sequential B. Selection C. Repetition
11. Which structure should be used to create a temperature converter program?
A. Sequential B. Selection C. Repetition
12. What input is/are needed in a simple mathematical converter program?
A. Formula B. Name of user C. Both A and B
13. What input should be asked from a user if you want to create a program that gives
the average of his/her quiz scores?
A. Quarter grade B. Quiz scores C. Performance scores
14. Which punctuation mark is needed to enter between an object and an operator?
A. Dot B. Comma C. Quotations
15. The statement: TextWindow.Writeline(Hello Philippines) is entered into an editor.
Can the small basic run this statement?
A. Yes, it will display the Hello Philippines message
B. No, quotation marks should have been used
C. No, the use of parenthesis is inappropriate

3
Lesson 1 Introducing Variables in Small Basic
You have learned about variables in the previous quarter, but how big is the role
that it is playing in a computer program? To answer this question, you will need to
experience how it is being used in an actual program, in this case, the small basic
program. Let’s begin!

What’s In

Activity 1. 3W Table
Directions: Copy the table below on your paper, then answer it with the required
data. On the first column, list everything you know about variables (do not browse
the internet). In the second column, list everything you want to know about
variables. The third column will be answered after studying this lesson.

What I Know What I Want To Know What I Have Learned


About Variables

Enumerate what you know Enumerate what you want to Enumerate what you have
about variables here know about variables here learned about variables here

What’s New

Looking Back at Our First Program


Recall that our first program displays a text window that shows a message which is
“Hello World”. What if our program would ask the name of our user first then a
personal message such as “Hello Noli” would be displayed on the screen instead of
“Hello World” Is that possible? Yes, with the help of variables.

What is It

What are variables?


You can use a variable to store different kinds of information, such as text or a
number. You have learned in the previous quarter that constants are also variables.
A variable can contain different values at different points in time. Most variables can
contain only one value at a time. However, special variables, which are called arrays,
can contain more than one value.

Activity 2. “A Personal Hello” Program


Note: It will be better if you could immediately use a computer.

Wouldn’t it be nice if our program can say “Hello” with the user's name instead of
saying the generic “Hello World?” To do that we must first ask the user for his/her
name and then store it somewhere and then print out “Hello” with the user’s name.
Let’s see how we can do that.

4
Enter the following program in your small basic program editor:
TextWindow.Write("Enter your Name: ")
name = TextWindow.Read()
TextWindow.WriteLine("Hello " + name)

When you type and execute this program, you’ll see an output like in Figure 1.

Figure 1.1. Asking the User’s Name


And when you type in your name and hit ENTER, you’ll see the following output:

Figure 1.2. A Personal Hello


Now, if you run the program again, you’ll be asked the same question again. You can
type in a different name and the computer will say Hello with that name.
Let’s analyze the program. In the program you just ran, the line that might have
caught your attention is this:
name = TextWindow.Read()
Read() looks just like WriteLine(), but with no inputs. It is an operation and basically,
it tells the computer to wait for the user to type in something and hit the ENTER
key. Once the user hits the ENTER key, it takes what the user has typed and returns
it to the program. The interesting point is that whatever the user had typed is now
stored in a variable called name. A variable is defined as a place where you can store
values temporarily and use them later. In the line above, a name was used to store
the name of the user. The next line is also interesting:
TextWindow.WriteLine("Hello " + name)
This is the place where we use the value stored in our variable, name. We take the
value in name and append it to “Hello” and write it to the TextWindow. Now that you
have experienced using a variable, let us discuss the rules in naming it in small
basic.

Rules for naming Variables


Variables have names associated with them and that’s how you identify them. There
are certain simple rules and some really good guidelines for naming these variables.
They are:
1. The name should start with a letter and should not collide with any of the
keywords like if, for, then, etc.
2. A name can contain any combination of letters, digits, and underscores but
should not contain spaces.
3. It is useful to name variables meaningfully – since variables can be as long as
you want, use variable names to describe their intent.
https://tinyurl.com/yd2tj8wt
5
What’s More

Activity 3. CORRECT or INCORRECT


Directions: Write C if the statement is correct and I if it is not. Write your answers
on your paper.

1. A variable can hold or store data.


2. “School” is not an example of a variable.
3. A variable can hold data being given by a user.
4. A variable can hold data given by the programmer.
5. Keywords are qualified to become variables.
6. A variable should only be purely letters or pure numbers.
7. “I_am_4_u” is qualified as a variable.
8. A constant is not a variable.
9. A variable can contain different values at different points in time.
10. An array can contain more than one value.

What I Have Learned

Go back to the 3W table you have filled out at the beginning of this lesson and fill
out the third column with everything you have learned about variables.
What I Know What I Want To Know What I Have Learned
About Variables

Enumerate what you know Enumerate what you want to Enumerate what you have
about variables here know about variables here learned about variables here

6
Lesson 2 Operators & Control Structures of Small Basic

Variables are not just the essential elements of a program. Sometimes, operators and
structures are needed to solve the programming problem. In this lesson, you will be
introduced to how they are used in a real small basic program. Let’s start!

What’s In

Activity 4. Identification
Some of the essential terms that will be used in this lesson are inside the box below.
Those terms were tackled in the previous quarter. Choose the correct answer from
the box. Write your answer on your paper. This will be checked but not recorded.
Answer honestly without the help of the internet.
Relational Arithmetic Logical Sequential Selection Repetition
Operators Operators Operators Structure Structure Structure

1. It enables the programmer to assign different events for different situations.


2. Some of its examples are addition, subtraction, and multiplication.
3. Some of its examples are greater than and less than mathematical symbols.
4. They are used when a program needs to repeatedly process one or more
instructions until some condition is met.
5. It refers to the linear execution of codes within a program.
6. Some of its examples are AND and OR.

What’s New

Operators are commonly used in Mathematics and Science. They are used to
indicate an operation between different symbols such as digits or letters. The
operators dictate what operation will be applied. In programming, these operators
are also used to apply operations on inputs either from the user or from the
programmer itself. Aside from operators, structures are also very important because
they dictate the flow of the program. Let’s begin learning more about them.

What is It

Activity 5. Simple Temperature Converter Program


Open Microsoft small basic on your computer then follow the steps that follow. First,
we’ll get the temperature in Fahrenheit from the user and store it in a variable.
There’s a special operation that lets us read numbers from the user and that is
TextWindow.ReadNumber. Type the following in your editor.
TextWindow.Write("Enter temperature in Fahrenheit: ")
Fahrenheit = TextWindow.ReadNumber()
This will let a user enter the value of our variable which is Fahrenheit. Now, that the
user can assign a value to our variable, we have to tell the computer what to do with
the value assigned by the user by adding the formula of conversion in your editor.
Celsius = 5 * (Fahrenheit - 32) / 9
7
The parentheses tell the computer to calculate the Fahrenheit – 32 part first and
then process the rest. Now, all we have to do is print the result out to the user.
Putting it all together, we get this program:
TextWindow.Write("Enter temperature in Fahrenheit: ")
Fahrenheit = TextWindow.ReadNumber()
Celsius = 5 * (Fahrenheit - 32) / 9
TextWindow.WriteLine("Temperature in Celsius is " + Celsius)
And after running the program, the text window should display as what can be seen
in Figure 2.1.

Figure 2.1. Text Window Asking for the Value of Fahrenheit


Try typing a value then hit Enter. The correct Celsius value should be immediately
displayed like in Figure 2.2.

Figure 2.2. Temperature Conversion


https://tinyurl.com/yd2tj8wt

Congratulations! You have now successfully used operators in small basic. On what
part?
Arithmetic, Comparison, and Logical Operators
By typing the conversion formula, you have used multiplication (*), division (/), and
subtraction (-). The other operator not used, is the addition (+). These operators are
called arithmetic operators. You can also perform other mathematical operations by
using the “Math” object and its set of operations such as remainder and square root.
The other operators are relational or comparison operators and logical operators
which will be used in the next module. Comparison operators can be used in
conditions for If, While, and For statements. Logical (Boolean) operators can be used
in conditions for If and While statements.
Operator Symbol
= (equal to)*
< (less than)
> (greater than)
Comparison Operators
<= (less than or equal to)
>= (greater than or equal to)
<> (not equal to)
And
Logical Operators
Or
*Only equal (=) operator can compare two texts or two numbers. Other operators are for
numbers only.
https://tinyurl.com/yb4bjypt
Control Structures
8
Control Structures specify the flow of control in programs. They are also called
logic. It analyzes and chooses in which direction a program flows based on certain
parameters or conditions (https://tinyurl.com/yda72zhy).
There are three basic types of control structures used in programming.
1. Sequential Structure. Sequence control refers to the linear execution of codes
within a program. In sequence control, the statements are executed one by
one in consecutive order.
2. Selection Structure. Selection control enables the programmer to assign
different events for different situations. Before it takes the next step, it asks a
question. An example is the “If…Then…Else” statement.
3. Repetition Structure/Loops. Repetition structure or loops are used when a
program needs to repeatedly process one or more instructions until some
condition is met at which time the loop ends.

Figure 3. Illustration of control structures


https://tinyurl.com/y7geo2od

In our simple temperature converter program, the sequential structure is used


because its flow is linear as illustrated in Figure 4.

Program asks the user to input a Fahrenheit value

Program converts the Fahrenheit value

Program displays the correct Celsius value

Figure 4. The sequential flow of the temperature converter program

You will be applying the selection and repetition structure on the next module.

Input and Output: A Crucial Part of Programming


As a programmer, you need to know the expected output of your program. If your
output expectation or goal is vague then you’ll never know if your program solved the
programming problem. In our temperature converter program, we know that an
input is required from a user and that the output should be the converted
temperature which is the Celsius value.
Remember that inputs can be provided by the user or by the programmer itself. An
easy way to remember the relationship between input and output: “a correct input
produces a correct output”

9
What’s More

Activity 5. TRUE or FALSE


Directions: Write TRUE if the statement is correct and FALSE if it is not. Write your
answers on your paper.
1. Relational operators are also called active operators.
2. Relational operators can be used in selection structures.
3. Logical operators are also called Boolean operators.
4. Logical operators can be used in If, Then, and Else statements.
5. Control Structures specify the flow of control in programs.
6. There are three main types of control structures.
7. If, Then and Else statements are examples of sequential structure.
8. Loops in programming have no end.
9. Selection structures are decision-driven.
10. A correct output produces a correct input.

What I Have Learned

Activity 6. Thinking of a Strategy


Directions: It is mentioned that programmers should know the expected output can
plan for the correct input. Think of some possible strategies that a programmer can
do to have a clear idea of the expected output. List them on your paper.

10

You might also like