0% found this document useful (0 votes)
60 views37 pages

Programming Language Fundamentals (Visual Basic) : Cyrenz D. Tan Ana Joy Portolito Bscpe-5

This document provides an overview of programming language fundamentals using Visual Basic. It discusses what programs and programming languages are. It then covers the history and components of Visual Basic, including its integrated development environment, toolbox, controls, and event-driven programming. The document also discusses variables and data types, constructors, control statements like if/else and loops, and different looping structures in Visual Basic like Do While, Do Until, and For Next loops.

Uploaded by

Jhoy Portolito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views37 pages

Programming Language Fundamentals (Visual Basic) : Cyrenz D. Tan Ana Joy Portolito Bscpe-5

This document provides an overview of programming language fundamentals using Visual Basic. It discusses what programs and programming languages are. It then covers the history and components of Visual Basic, including its integrated development environment, toolbox, controls, and event-driven programming. The document also discusses variables and data types, constructors, control statements like if/else and loops, and different looping structures in Visual Basic like Do While, Do Until, and For Next loops.

Uploaded by

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

Programming

Language
Fundamentals
(Visual Basic)
Cyrenz D. Tan
Ana Joy Portolito
BSCpE-5

Application Programs

Program - detailed set of instructions for a


computer to execute
Application programs (applications or
apps) - self-contained collection of
programs that perform a task for the end
user
Programming language - formal language
used to give instructions to computers

History of Programming
Languages
Machine

language

Procedure-oriented

languages
Object-oriented
Event-driven
Natural

languages

languages

languages

What is Visual Basic ?

Visual Basic (VB) is a programming environment


from Microsoft in which a programmer uses a
graphical user interface (GUI) to choose and
modify preselected sections of code written in
thebasic programming language.

it's sometimes used to prototype an application


that will later be written in a more difficult but
efficient language.

Common Programming
Languages

The Integrated Development


Environment of Visual Basic

Toolbox and Controls

Basic Controls

Label - displays text the user cannot change

TextBox - allows the user to enter text

Button performs an action when clicked

Form - A window that contains these controls

Others

RadioButton - A round button that is selected or deselected


with a mouse click

CheckBox A box that is checked or unchecked with a


mouse click

Properties Window

Event Driven Programming

Before event driven programs, typical


applications were executed by the computer
under control of the application.

With event driven programs, the operating


system detects user interactions (mouse
click, tab key, etc.), passes them to the
application, and a control responds by
running a subprogram.

Visual Basic Events

Nothing happens until a user clicks or presses a keythis is called an event.

An Event Procedure Walkthrough

Properties and Event Procedures of the Form

The Header of an Event Procedure

Event

An event is an action, such as the user


clicking on a button
Usually, nothing happens in a Visual Basic
program until the user does something and
generates an event.
What happens is determined by statements.
Code must be written to react to an event by
performing a function.

Controls and Associated


Events
Control

Event

Form

Load event occurs when form is


loaded into computer memory

Button

Click event occurs when user clicks


button with mouse

Textbox

Lostfocus event occurs when user


tabs out of box

Variables and Data Types

Variables are assigned values from a data type

VB data types include String, Integer, Double, and


Boolean

A Dim statement is used to declare a variable and


the data type from which its values are assigned

Syntax of Dim and


Assignment Statements

Dim

Dim <variable> as <data-type>

Dim avg as double

Dim nmbr as integer

Dim flag as boolean

Assignment

<variable> = <expression>

avg = (nmbr1 + nmbr2) / 2

flag = false

Constructors

A constructor is a method called automatically when an instance


of the class is created

Think of constructors as initialization routines

Useful for initializing member variables or performing other


startup operations

To create a constructor, simply create a Sub procedure named


New within the class

Characteristics of a
Constructor:

Always defined as a Sub. It therefore does not have a return value.

Regardless the name of the class, the constructor in VB.NET is


called New.

Can have any access modifier (Public, Protected, Friend, Private, or


default).

In most cases, a constructor has a Public access modifier.

A class can have multiple constructors.

Constructor Example:
Public Class Student
' Member variables
Private strLastName As String
Private strFirstName As String
Private strId As String
Private sngTestAvg As Single 'Holds
' Constructor
Public Sub New()
strFirstName = "(unknown)"
strLastName = "(unknown)"
strId = "(unknown)"
sngTestAvg = 0.0
End Sub
(The rest of this class is omitted.)
End Class

'Holds last name


'Holds first name
'Holds ID number
test avg

Control Statements

Control Statements are used to control the flow


of program's execution.

VB supports selection statement such as

if... Then

if...Then ...Else

Nested If...Then...Else

Select...Case

Loop structures such as

Do While...Loop
While...Wend
For...Next

Control Statement

If...Then selection structure

The If...Then selection structure performs an indicated action only when the
condition is True; otherwise the action is skipped.
Syntax of theIf...Thenselection

If <condition> Then
statement
End If
e.g.: If average>75 Then
txtGrade.Text = "A"
End If

Continuation

If...Then...Else selection structure

TheIf...Then...Elseselection structure allows the programmer to specify that a different


action is to be performed when the condition is True than when the condition is False.
Syntax of theIf...Then...Elseselection

If <condition > Then


statements
Else
statements
End If
e.g.: If average>50 Then
txtGrade.Text = "Pass"
Else
txtGrade.Text = "Fail"
End If

Continuation..

Nested If...Then...Else selection structure

NestedIf...Then...Elseselection structures test for multiple


cases by placingIf...Then...Elseselection structures
insideIf...Then...Elsestructures.

Continuation.
Syntax of the NestedIf...Then...Elseselection
structure
Method 2
Method 1
If < condition 1 > Then
If < condition 1 > Then
statements
statements
Else
ElseIf < condition 2 > Then
If < condition 2 > Then
statements
statements
ElseIf < condition 3 > Then
Else
If < condition 3 > Then
statements
statements
Else
Else
Statements
Statements
End If

End If
End If
EndIf

Continuation.

e.g.: Assume you have to find the grade using


nested if and display in a text box
If average > 75 Then
txtGrade.Text = 1.5"
ElseIf average > 65 Then
txtGrade.Text = 2"
ElseIf average > 55 Then
txtGrade.text = 2.5"
ElseIf average > 45 Then
txtGrade.Text = 3"
Else
txtGrade.Text = 5.0"
End If

Continuation.

Select...Case selection structure


Select...Casestructure is an alternative toIf...Then...ElseIffor
selectively executing a single block of statements from among
multiple block of statements.Select...case is more convenient
to use than theIf...Else...End If.

Syntax of theSelect...Caseselection structure


Select Case Index
Case 0
Statements
Case 1
Statements
End Select

Continuation.

Dim average as Integer


average = txtAverage.Text
Select Case average
Case 100 To 75
txtGrade.Text =1.5"
Case 74 To 65
txtGrade.Text =2.0"

Case 64 To 55
txtGrade.Text =2.5"
Case 54 To 45
txtGrade.Text =3.0"
Case 44 To 0
txtGrade.Text =5.0"
Case Else
MsgBox "Invalid average marks"
End Select

Continuation.

Loop

Structures

A repetition structure allows the programmer to that an action


is to be repeated until given condition is true.

Continuation.

Visual Basic repetition or looping statements

Do While

Repeat/Loop while true

Often called pretest loop

Do Until

Repeat/Loop until true

Often called posttest loop

For...Next

Repeat/Loop a specified number of times

Continuation.
Do

While... Loop Statement

a control flow statementthat executes a block of code at least


once, and then repeatedly executes the block, or not,
depending on a givencondition at the end of the block.

Syntax:

Do While [Condition]
[Statements]
Loop

Continuation.
Example:
Private Sub Command1_Click()
Dim a As Integer
a=1
Do While a < 100
a=a*2
MsgBox ("Product is::" & a)
Loop
End Sub

Do Until loop

repeat until test expression is true

When false, the previous steps are repeated until the


expression evaluates to true

When true, the loop ends and


not
executed again

Syntax:
Do Until Condition
statement(s)
Loop expression

the statement(s) are

Continuation.

For Next LoopStatement

Executes a set of statements repeatedly in a loop for


the given initial, final value range with the specified
step by step increment or decrement value.

Syntax:
For Counter = StartValue To EndValue[Step Value]
statement[s]
Next [Counter]

Continuation.

While Wend Statement

a looping statement where a condition is checked first, if it is true a


set of statements are executed. The condition can also become false
atleast once while this statement is used.

Syntax:

While condition
[statements]
Wend

Continuation.
In general, there are two categories of
loops:

Pretest Loops

the condition gets evaluated at the beginning of the loop cycle.

Posttest Loop

the loop condition gets evaluated at the end of the loop cycle

You might also like