0% found this document useful (0 votes)
53 views6 pages

ComProg Module - M8 Final

The document discusses variables and control structures in Visual Basic programming. It defines variables as containers that hold digital data and describes how to declare variables in VB by specifying a name and data type. It also explains three types of control structures: sequential, which follows a linear order; decision/selection, which directs the program flow based on conditions; and repetition/iteration, which repeats statements using loops like while and for loops. An example calculates the sum of the first nine numbers using a while loop.

Uploaded by

LEO JR GREFALDO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views6 pages

ComProg Module - M8 Final

The document discusses variables and control structures in Visual Basic programming. It defines variables as containers that hold digital data and describes how to declare variables in VB by specifying a name and data type. It also explains three types of control structures: sequential, which follows a linear order; decision/selection, which directs the program flow based on conditions; and repetition/iteration, which repeats statements using loops like while and for loops. An example calculates the sum of the first nine numbers using a while loop.

Uploaded by

LEO JR GREFALDO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

2

Module COMPUTER PROGRAMMING.NET


8 Variables and Control Structures
From the previous module, we discussed the Visual Basic environment, flow charts, data types, and Visual Basic common
controls and their usage. In this lesson, we will discuss the following:
 How variables are declared and used in the visual basic programming; and
 Control structures and how to use them.

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.

tScore String 1. Dim (____) As Date


address gAverage 2. (____) gAverage As Double
Double reg_date 3. Dim temperature As (____) = -16.4
Dim Boolean 4. Dim (____) As Integer
As Date 5. Dim is_Correct As (____) = TRUE

B. Identify the data type of the following variables according to their given value:

6. average = 97.71 11. ctext = “Very good!”


7. message = “hello” 12. isVisible = TRUE
8. verified = FALSE 13. airTemperature = -16
9. x = 32 14. cAnswer = “a”
10. msary = 3/27/2022 15. cTime = 12:24:54 AM

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.

COMPUTER PROGRAMMING .NET | MODULE 4


2

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:

Start As you can see,


Get 1st and 2nd Add 1st and 2nd
Print Sum End
before the program number number
can output the
answer, the 1st and 2nd number must be entered first. After inputting all the necessary values, the program can now start
computing the sum and procedure an output. Therefore, the program’s flow is a step-by-step execution of the procedures
that is synonymous to 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:

If [condition] Then If subject is VB.Net Then


[statement/s] Display Form2
End If End If

Sample VB.Net code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button.Click


If TextBox1.Text = “VB.Net” Then
Form2.show
End If
End Sub

Sample Output:
The user must type-in the string “VB.Net” to process.

When the Next button


is clicked,
it will show the next
form named Form2.

2|PageComputer Programming .Net  Module 4


2
If-Then-Else
This statement enables you to execute another course of action when a condition is true or false. The sample
pseudocode and program code below are the next part of the specified program wherein the user must type-in a grade and
the system will determine it is “PASSED” or “FAILED”. Take note that the passing value is 60 and above.

Format/Syntax: Sample pseudocode code:

If [condition] Then If grade is greater than or equal to passing Then


[statement/s] Print “Congratulations, You Passed”
Else Else
[statement/s] Print “Sorry, You Failed”
End If End If

3|PageComputer Programming .Net  Module 4


2
Sample VB.Net code:
Dim passing As Double = 60

If TextBox1.Text >= CDbl(passing) Then


MessageBox.Show(“Congratulations, You Passed”)
Else
MessageBox.Show(“Sorry, You Failed”)
End If

Sample Output:

sample output if a Sample output if a


passing grade is failing grade is
typed-in. typed-in

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.

1. Using While Loop


This subtype enables the loop of procedures and statements to be executed while a certain condition is met and
true.

Format/Syntax: Sample VB.Net code:

While [condition] Dim i As Integer = 1


[statement/s] Dim sum As Integer
End While
While i <= 9
Sum = sum + i
i = i + 1
End While
The code i <= 9 serves as the condition for the above MessageBox.Show(“Total sum is “ & sum)
While Loop statement, which means that the procedure
will continue to execute as long as this condition is true.
Next, we jump to the procedure sum = sum + i statement, which is the formula for getting the corrent sum. The next
statement i = i + 1 is for altering and giving the new value for i, so that the next time the program evaluates it, i
always contain the next number.

4|PageComputer Programming .Net  Module 4


2

2. Using For Loop


Among the three major types of loops, For Loop is the most commonly used. This type is utilized when you know
both the starting point and the end point of the loop.

Format/Syntax: For counter = start[value] To end[value]


[statement/s]
Next

Sample VB.Net code:


As you will notice, only the variable i is
Dim i As Integer
Dim sum As Integer declared and initiated with the integer value of 1
For i = 1 To 9 and set to and ending value of 9, meaning the
Sum = sum + i value of i increases by 1 then executes the
Next procedure in each loop until its value is equal to
MessageBox.Show(“Total sum is “ & sum) 9.

2. Using Do-While Loop


This loop is almost similar to a While loop except that it executes the procedure first then checks if the condition
is met. If the condition is met, then loop will continue. This means that the procedure can execute at least once.
Format/Syntax: Sample VB.Net code:

Do While [condition] Dim i As Integer = 1


[statement/s] Dim sum As Integer
End While Do While i <= 9
Sum = sum + i
i = i + 1
Loop
MessageBox.Show(“Total
The main difference between the above code from its While loop code counterpart is the keyword Loop. This means that
the iteration or repetition is done after executing the whole procedure.

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

I. Fill in the Blanks


Read each statement below carefully and fill in the blank with the correct answer on the answer rsheet provided.

1. (__________) are used to direct the program's flow.


2. Sequential structure is also known as (__________).

5|PageComputer Programming .Net  Module 4


2
3. (__________) statement enables you to execute à course of action when a condition is met.
4. Repetition/Iteration structure is also called (__________).
5. The term for a loop that never finishes is (__________).

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)

NAME: ________________________________________________________ SCORE: ________________


GRADE STRAND: _______________________________________________ DATE: ________________

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.

6|PageComputer Programming .Net  Module 4

You might also like