ComProg Module - M8 Final
ComProg Module - M8 Final
VARIABLES
In computer programming, variables are containers of digital data. It is used to hold one or more values. Variables are
used to keep you (the programmer) from repeating some values in multiple places in your code. For example, if your
program is making a computation, it is important that you place the result of that computation onto a variable. In case that
you would need to get the result of the computation again, instead of redoing the computation, you would just get the
result from the variable.
How to use variables in Python?
When using a variable in VB, you need to declare it in your code. Just like your variables in math, it can be given with
any names.
To name a variable, it must begin with a letter of the alphabet. You can use a single character (e.g. x, y, or z) or words
(e.g. total, age, or flag).
After deciding for a variable name, you will have to determine its data type (check your Module 2 for data types).
To declare your variable in VB, this format is used: Dim [variable_name] As [data_type]
Example:
Dim age As Integer The first line uses “age” for its variable name, and Integer for its data
Dim f_name As String type, since we want the variable to hold a number value. Likewise, in the
Dim b_day As Date next lines, different data types are used according to the value that we want
Dim answer As Boolean our variables to hold.
ACTIVITY:
A. Complete the variable declarations below. Select your answers from the box.
B. Identify the data type of the following variables according to their given value:
CONTROL STRUCTURES
Control structure is used to direct the program's flow for it is involve in every decision that an application must do. There
are different types of control structures, let us start the discussion by explaining each of the types.
A. Sequential Structure
This logical structure pertains to a step-by-step execution of a program. Also known as straight-line structure, this type
of structure follows what is next in line for the flow of the program. For example, block 1 is processed first, and then
block 2, and so on. Remember the Addition calculator application shown in Module 3? Such programs are example of this
structure. Check the flowcart below so you can better understand its sequential structure:
B. Decision/Selection Structure
This logical structure is used if you want to analyze whether the given condition or a couple of conditions are true or false.
This structure provides two types of statements to choose from: If-Then and If-Then-Else statement. Before discussing
this structure, familiarize yourself first with the comparison operators, which are used to create and compare conditions.
COMPARISON OPERATORS
= Is equal to < Is less than
> Is greater than <= Is less than or equal to
>= Is greater than or equal to <> Is not equal to
If-Then
This statement enables you to execute a course of action when a condition is met. The sample pseudocode and
program code below is part of a program that displays the next form if the user inputs the subject VB.Net.
Format/Syntax: Sample pseudocode code:
Sample Output:
The user must type-in the string “VB.Net” to process.
Sample Output:
C. Repetition/Iteration Structure
Another term for this structure is looping structure. This enables statements or set of set statements to be repeated again
and again until the condition is met. We can see this concept in almost any medium to complex program, but simple
applications with counter or scoreboards can also benefit from this structure. There are three main types of loop: While,
For Loop, and Do-While.
Now let us practice this type of loop by creating a program that will output the sum of the first positive 9 numbers when a
button is clicked. Hint: The total sum of the said 9 positive numbers is 45 (1+2+3+4+5+6+7+8+9=45). Instead of writing
the entire mathematical operation to compute for the sum, we can use loop structures to shorten the code. This is
especially helpful when dealing with long set of numbers.
REMEMBER:
While Loop uses a condition. The procedure in the loop is executed as long as the condition is met and true.
For Loop requires values for start and end of the loop. The procedure in the loop is executed for a number of times
specified in the start and end of the loop.
Do-While Loop executes the procedure in the loop once and checks whether the condition is met and true so the
program could execute the procedure again.
Make sure that the loops you are creating will have a limit or will stop for a certain period (this is called definite
loop) like the examples that are shown in the three major loops.
The term for a loop that never finishes called infinite loop. However, this does not necessarily mean infinite because
your computer memory will exhaust and the program will surely stop. To avoid this, use conditions that will soon
evaluate to false or set a limit to the number of loops.
POST TEST
II. Tracing
Write down the output/message shown in the MessageBox from the codes below. If the loop will execute endlessly, write
infinite loop.
1. 2. 3.
Dim x As Integer = 9
Dim x As Integer = 5 Dim x As Integer = 7 Dim msg As String = “Ha!”
Do While x>=1 While x>=5 For i = 7 To x
x = x – 1 x = x + 1 msg = msg & msg
Loop End While Loop
MessageBox.Show(x) MessageBox.Show(x) MessageBox.Show(msg)
ANSWER SHEET
ACTIVITY 1:
A B
1. 6. 11.
2. 7. 12.
3. 8. 13.
4. 9. 14.
5. 10. 15.
POST TEST:
Fill in the blank Tracing
1. 1.
2.
3. 2.
4.
5. 3.