fundamentals of visual basic
DESCRIPTION
Fundamentals of visual basic. Comments. 1. are inserted to document programs and to improve readability. 2. help other people to read and understand your program code. 3. do not cause the computer to perform any action when a program is running as the compiler ignore it. - PowerPoint PPT PresentationTRANSCRIPT
CommentsComments1. are inserted to document programs and to
improve readability.
2. help other people to read and understand your program code.
3. do not cause the computer to perform any action when a program is running as the compiler ignore it.
4. can begin with either ‘ or Rem (short for Remark) and is a single line-comment that terminated at the end of the current line.
5. colored green.
Comments..contComments..contExample:Example:
‘ ‘ Code for the single selectionCode for the single selection
Rem Rem Code for the single selectionCode for the single selection
KeywordsKeywords
1. reserved words that cannot be used for anything other than for the features they represents. (e.g. Private, Sub, End, Print, etc.)
2. colored blue.
3. Visual Basic sets to uppercase the first letter of keywords, so typing dim would be changed to Dim.
VariableVariable1. location in the computer memory where a value can be stored for use by a program.2. A variable name is any valid identifier.3. A variable name should not be any of the Visual Basic keywords and must begin with a letter.5. It must not contain a space or any of the special characters. Underscore (_) is the only special character accepted.6. The maximum length of a variable name is 255 characters containing only letters, numbers, and underscores.
ConstantConstant- Has a fixed value and may not be changed by the program as it run.
Example:Dim Var_name As TypeDim Input1 As Integer
Data TypesData Types
1. describe the information that a variable may store or hold.2. describe how many bytes of memory are required to represent a type.3. the number of bytes determined the range of values that can be stored.4. Byte sizes and range values are fixed.5. assigning any value outside a data type’s range is a run-time error.
OperatorsOperators
- are symbols that trigger computations, comparisons, and decisions.
- Are combinations of operators and operands
ExpressionsExpressions
OperatorsOperators
- are symbols that trigger computations, comparisons, and decisions.
- Are combinations of operators and operands
ExpressionsExpressions
PROGRAM CONTROL PROGRAM CONTROL STRUCTURESSTRUCTURES
PROGRAM CONTROL PROGRAM CONTROL STRUCTURES STRUCTURES
Refers to the order of program execution whether statements are executed sequentially, repeatedly, or conditionally or selectively
Ten (10) Program Control Structures
1. Sequence Control Structure
- Statements are executed one after the other in the order in which they are written (called as Sequential Execution)
- Considered as the simplest form of a program control structure where the statements are executed from top to bottom.
Sequence Control Structure..cont
Example:
Dim x As Integer, y As Integer, sum As Integer
X=3
Y=4
Sum = x+y
Label1.caption = “The sum is:” & sum
2. Selection Structure
2.1. Single Selection
- performs or selects an action if a condition is True and skips the action if the condition is False.
Syntax:
If <condition> Then
Statements
End if
2.2 Double Selection
- performs an action if the condition is True and performs a different action if the condition is False.Syntax:
If<condition>Then
Statement1
Else
Statement2
End if
3. Multiple SelectionSyntax: (A)
If<condition1> Then
Statement1
ElseIf<condition2>
Statement2
ElseIf<condition3>
Statement3
ElseIf<condition4>
Statement4
End If
Select Case Multiple StructureSyntax(B):
Select Case <var_name>
Case Is <range_of_values>
Statement
Case <value>
Statement
Case<value1> To <value2>
Statement
Case <value1>,<value2>,<value3> To <value4>Statement
Case Else
Statement
End Select
Example:Dim nGrade As Integer
nGrade = Val(Text1.Text)
Select Case ngrade
Case Is >= 90
Text2.Text = “A”
Case 80 To 89
Text2.Text = “B”
Case 70 To 79
Text2.Text = “C”
Case 60 To 69
Text2.Text = “D”
Case Is < 60
Text2.Text = “F”
End Select
Repetition Structure
Looping Statements
- Is a program instruction that repeats some statement or sequence of statements in a specified number of times.
- Allows a set of instructions to be performed all over and over again until a certain condition is reached, met, proven or tested as false or true.
- Allows us to execute one or more lines of code repetitively.
6 Types of Repetition Control Structures
1. While/Wend
2. Do
2.1Do While/Loop
2.2 Do/ While Loop
2.3 Do Until/Loop
2.4 Do/ Until Loop
3. For / Next
1. While/WendSyntax:
Index
While [condition]
Statement/s
Wend
Parts
Index – a user defined numeric variable that the loop uses as a counter .
Condition- Optional. Boolean expression. If condition is Nothing, Visual Basic treats it as False.
Statement- Optional. One or more statements that are repeated while, or until, condition is True.
Step - is an optional keyword indicating the loop should step .
Example:
Cnt = 1
While cnt<11
Print cnt;
Cnt = cnt + 1
Wend
2. Do 2.1Do While/Loop
Syntax:
Index
Do While [Condition]
Statement/s[Step: Increment or Decrement]
Loop
Example:
cnt = 1
Do While cnt <11
Print cnt
cnt = cnt +1
Loop
2.2 Do/ While Loop
Syntax:
Index
Do
Statement
[Step: Increment or Decrement]
Loop While [Condition]
Example:
cnt = 1
Do
Print cnt;
cnt = cnt +1
Loop While cnt<1
2.3 Do Until/Loop Syntax:
Index
Do Until [condition]
Statement
[Step: Increment or Decrement]
Loop
Example:
cnt = 1
Do Until cnt = 11
Print cnt;
cnt = cnt +1
Loop
2.4 Do/ Until Loop Syntax:
Index
Do
Statement
[Step: Increment or Decrement]
Loop Until [Condition]
Example:
cnt=1
Do
Print cnt;
cnt = cnt + 1
Loop Until cnt = 11
3. For / NextParts:
The For is the keyword you use to start the loop.Index1
- is a user defined numeric variable that the loop uses as a counter .start
-is the number from which the loop starts from .
To
- is the keyword which separates the start and end numbers .
end
- is the number at which the loop stops.
Step(Keyword)
- is an optional keyword indicating the loop should step .
step
- is the size of increment / decrement the step should have (this can be a negative number)Next
- is the keyword that completes the loop .index2
- is used to identify which index is updated by the Next keyword .
Syntax:
For index1 = start To end [Step step]
[ Statements ]Next [index2]
Sample:
For cnt=1 To 10
Print cnt;
Next cnt
End of the slidesEnd of the slides
“Your value is not who you are and what you have, but how other people have become
because of you”
-
End of the slidesEnd of the slides
“Heaven is not for people who are better than the rest, but for
people who sincerely try to become better than who
they are..”
- John 3:3