introduction to programming lecture note - 2 visual basic programming fundamentals

31
Introduction to Introduction to Programming Programming Lecture Note - 2 Lecture Note - 2 Visual Basic Programming Visual Basic Programming Fundamentals Fundamentals

Upload: abner-thompson

Post on 18-Jan-2016

239 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Introduction to Introduction to ProgrammingProgramming

Lecture Note - 2Lecture Note - 2Visual Basic Programming Visual Basic Programming

FundamentalsFundamentals

Page 2: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

CommentComment

Comment is a line in a program which is Comment is a line in a program which is ignored by the compiler (i.e. it is not ignored by the compiler (i.e. it is not translated by the compiler)translated by the compiler)

In VB, a comment starts with ‘ (Single In VB, a comment starts with ‘ (Single Quotation)Quotation)

Page 3: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

KeywordKeyword Every language has some reserved words Every language has some reserved words

that conveys some special meaning to the that conveys some special meaning to the compiler, such words are called keywordscompiler, such words are called keywords

E.g. dim, if, end, … are keywords in VBE.g. dim, if, end, … are keywords in VB

Page 4: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Storing information in variablesStoring information in variables

The first line of code is a Dim, or Dimension, The first line of code is a Dim, or Dimension, statement, used for dimensioning (or statement, used for dimensioning (or declaring) variables. declaring) variables.

ExampleExample::Dim Num As IntegerDim Num As Integer

Dim Name As StringDim Name As String

Dim Price As SingleDim Price As Single

Page 5: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Naming Convention for variablesNaming Convention for variables

The name must start with a letter, not a number or The name must start with a letter, not a number or other character.other character.

The remainder of the name can contain letters, The remainder of the name can contain letters, numbers, and/or underscore characters. No spaces, numbers, and/or underscore characters. No spaces, periods, or other punctuation characters are allowed.periods, or other punctuation characters are allowed.

The name must be unique within the variable's scope. The name must be unique within the variable's scope. (Scope refers to the context in which the variable is (Scope refers to the context in which the variable is defined)defined)

The name cannot be one of Visual Basic's reserved The name cannot be one of Visual Basic's reserved words.words.

Page 6: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

ExamplesExamples

Valid variable names:Valid variable names:a, test, a_test, a3798test89, absd_s3_ewa, test, a_test, a3798test89, absd_s3_ew

Invalid variable names:Invalid variable names:a.test, a test, a@test, 3589a, dim, decimala.test, a test, a@test, 3589a, dim, decimal

Page 7: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Changes to the Dim StatementChanges to the Dim Statement Multiple declarations of the same type are now easier, Multiple declarations of the same type are now easier,

as in the following example:as in the following example: Dim x, y As IntegerDim x, y As Integer

The ability to initialize a variable within the Dim The ability to initialize a variable within the Dim statement. statement.

Dim No_of_Cars As Integer = 10Dim No_of_Cars As Integer = 10 ** ** Initialization only works for one-variable Dim Initialization only works for one-variable Dim

statements.statements.

Page 8: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Understanding Data TypesUnderstanding Data Types Visual Basic .NET Visual Basic .NET

Data TypeData TypeValues StoredValues Stored ExampleExample

BooleanBoolean True/FalseTrue/False TrueTrue

CharChar Single CharacterSingle Character ““A”A”

DateDate Dates and TimesDates and Times 12/21/1970 02:00 PM12/21/1970 02:00 PM

DecimalDecimal Decimal or WholeDecimal or Whole 19.95D, 7.28119.95D, 7.281

DoubleDouble Decimal NumbersDecimal Numbers 1.23E-101.23E-10

IntegerInteger Whole NumbersWhole Numbers 67536753

LongLong Whole NumbersWhole Numbers 7342897632473428976324

SingleSingle Decimal NumbersDecimal Numbers .0000123.0000123

ShortShort Whole NumbersWhole Numbers 3212332123

StringString CharactersCharacters ““HELLO”HELLO”

Page 9: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Understanding Data TypesUnderstanding Data Types Data TypeData Type Size (B)Size (B) RangeRange

BooleanBoolean 22 True or FalseTrue or False

DateDate 88 January 1,100 to December 31,9999January 1,100 to December 31,9999

shortshort 22 -32,768 to 32,767-32,768 to 32,767

IntegerInteger 44 -2,147,483,648 to 2,147,483,647-2,147,483,648 to 2,147,483,647

LongLong 88 19 digits19 digits

SingleSingle 44 -ve no: -3.402823E38 to -1.401298E-45-ve no: -3.402823E38 to -1.401298E-45

+ve no:1.401298E-45 to 3.402823E38+ve no:1.401298E-45 to 3.402823E38

DoubleDouble 88 -ve no: -1.79769313486232E308 to -ve no: -1.79769313486232E308 to

-4.94065645841247E-324-4.94065645841247E-324

+ve no: 4.94065645841247E-324 to+ve no: 4.94065645841247E-324 to

1.79769313486232E3081.79769313486232E308

DecimalDecimal 1616 Up to 28 digits to the left or right of .Up to 28 digits to the left or right of .

Page 10: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Using Math OperationsUsing Math Operations

OperationOperation OperatorOperator

AdditionAddition ++

SubtractionSubtraction --

MultiplicationMultiplication **

DivisionDivision //

Integer divisionInteger division \\

ModulusModulus ModMod

ExponentiationExponentiation ^̂

Page 11: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Addition and SubtractionAddition and Subtraction

Using the Addition OperatorUsing the Addition Operator

result = number1 + number2[+ result = number1 + number2[+ number3]number3]

result = 1 + 2 + 3result = 1 + 2 + 3

Using the Subtraction OperatorUsing the Subtraction Operator

result = number1 - number2[- result = number1 - number2[- number3] number3]

result = 15 - 6 - 3result = 15 - 6 - 3

Page 12: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Multiplication and DivisionMultiplication and Division Using the Multiplication OperatorUsing the Multiplication Operator

result = number1* number2 [* number3] result = number1* number2 [* number3]

Using the Division OperatorsUsing the Division Operators

result = number1 / number2 [/ number3] result = number1 / number2 [/ number3]

result = 4/3 ‘will return 1.333333result = 4/3 ‘will return 1.333333

result = number1 \ number2 [\ number3] result = number1 \ number2 [\ number3]

result = 4\3 ‘will return 1result = 4\3 ‘will return 1

Page 13: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Modulus and ExponentiationModulus and Exponentiation

Using the Modulus OperatorUsing the Modulus Operator

result = number1 mod number2result = number1 mod number2

result = 20 mod 3 ‘will return 2result = 20 mod 3 ‘will return 2

Using the Exponentiation OperatorUsing the Exponentiation Operator

answer = number1 ^ exponentanswer = number1 ^ exponent

answer = 3 ^ 2 ‘will return 9answer = 3 ^ 2 ‘will return 9

Page 14: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Incrementing the Value of a VariableIncrementing the Value of a Variable

One frequent use of variables is a counter, One frequent use of variables is a counter, where the same variable can be updated with where the same variable can be updated with the result of an equation.the result of an equation.

intNumber = intNumber + 1

(Shortcut: intNumber += 1) intNumber = intNumber – 1 (Shortcut: intNumber -= 1)

Page 15: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Operator PrecedenceOperator Precedence

Exponentiation (^)Exponentiation (^) Negation (-)Negation (-) Multiplication and division (*, /)Multiplication and division (*, /) Integer division (\)Integer division (\) Modulus arithmetic (Mod)Modulus arithmetic (Mod) Addition and subtraction (+, -)Addition and subtraction (+, -)Important Note: You can override normal operator

precedence by using parentheses to group sub-expressions that you want to be evaluated first.

Page 16: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Controlling the Flow of Your ProgramControlling the Flow of Your Program Control statementsControl statements: : Without control statements, your program Without control statements, your program

would start at the first line of code and proceed would start at the first line of code and proceed line by line until the last line was reached, at line by line until the last line was reached, at which point the program would stop. which point the program would stop.

Two typesTwo types: : Decision statementDecision statement LoopLoop

Page 17: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Control statementsControl statements

Decision statement:Decision statement:

This statement is used to control the execution This statement is used to control the execution of parts of your program, based on conditions of parts of your program, based on conditions that exist at the time the statement is that exist at the time the statement is encountered. encountered.

Two basic types of decision statements areTwo basic types of decision statements are :- :-

IfIf statement and statement and Select CaseSelect Case statement statement

Page 18: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Control statementsControl statements

Loops:Loops: Loops are used to perform repetitive tasks in a Loops are used to perform repetitive tasks in a

program.program. Three main types of loops areThree main types of loops are :- :- Counter Loops (For loop)Counter Loops (For loop) Conditional Loops (Do loop)Conditional Loops (Do loop) Enumerator loopsEnumerator loops (For Each loop)(For Each loop)

Page 19: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

LoopsLoops

Counter, or For, loops perform a task a set Counter, or For, loops perform a task a set number of times. number of times.

Conditional, or Do, loops perform a task while Conditional, or Do, loops perform a task while a specified condition exists or until a specified a specified condition exists or until a specified condition exists. condition exists.

Enumerator loops are used to perform an action Enumerator loops are used to perform an action on each item in a group of objects. on each item in a group of objects.

Page 20: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Understanding If StatementsUnderstanding If Statements

Syntax:Syntax: If If condition condition Then Then command command

Example:Example:

'Single-Line IF 'Single-Line IF If x > 5 then x = 0 If x > 5 then x = 0 'Multiple-Line IF 'Multiple-Line IF If x > 5 Then If x > 5 Then

x = 0 x = 0 End If End If

Page 21: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Understanding If StatementsUnderstanding If Statements

Using Multiple Commands within an If BlockUsing Multiple Commands within an If Block

ExampleExample::

If DepositAmt > 0 Then If DepositAmt > 0 Then

TotalPaid = TotalPaid + TotalPaid = TotalPaid + DepositAmtDepositAmt

DepositAmt = 0 DepositAmt = 0

End If End If

Page 22: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Understanding If StatementsUnderstanding If Statements

Working with the False ConditionWorking with the False Condition

Syntax:Syntax:

If If condition condition Then Then statements to process when condition is statements to process when condition is

True True

ElseElse

statements to process when condition is statements to process when condition is FalseFalse

End IfEnd If

Page 23: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Understanding If StatementsUnderstanding If Statements Working with Multiple If StatementsWorking with Multiple If Statements The The ElseIfElseIf statement enables you to specify another statement enables you to specify another

condition to evaluate when the first condition is condition to evaluate when the first condition is False. False.

Example:Example:If TestValue < 0 Then If TestValue < 0 Then lblResult.Text = "Negative" lblResult.Text = "Negative" ElseIf TestValue = 0 Then ElseIf TestValue = 0 Then lblResult.Text = "Zero"lblResult.Text = "Zero"Else Else lblResult.Text = "Positive" lblResult.Text = "Positive" End IfEnd If

Page 24: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Using Boolean Logic in If ConditionsUsing Boolean Logic in If Conditions

The logical operators used with True/False conditions The logical operators used with True/False conditions are as follows:are as follows:

And — Expressions on both sides of the And must And — Expressions on both sides of the And must

evaluate True for the result to be True. If one or more evaluate True for the result to be True. If one or more expressions in the And comparison is False, the result expressions in the And comparison is False, the result is False.is False.

Or — If either side of an Or comparison is True, or Or — If either side of an Or comparison is True, or both sides are True, then the result is True. The result both sides are True, then the result is True. The result of an Or is False only if both expressions are False.of an Or is False only if both expressions are False.

Page 25: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Using Boolean Logic in If ConditionsUsing Boolean Logic in If Conditions

Xor — This stands for exclusive or. This Xor — This stands for exclusive or. This operator is similar to Or but False if both sides operator is similar to Or but False if both sides are True. are True.

Not — Negates the result of an expression, Not Not — Negates the result of an expression, Not True is False and Not False is True.True is False and Not False is True.

Page 26: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Boolean OperatorsBoolean Operators

x y x And y

F F F

F T F

T F F

T T T

x y x Or y

F F F

F T T

T F T

T T T

x y x Xor y

F F F

F T T

T F T

T T F

x Not x

F T

T F

Page 27: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Using Boolean Logic in If ConditionsUsing Boolean Logic in If Conditions

IF username.text=“ahsan” And password.text = “123” ThenIF username.text=“ahsan” And password.text = “123” Then

lblMsg.text = “Welcome Ahsan”lblMsg.text = “Welcome Ahsan”

ElseElse

lblMsg.text = “Login Failed”lblMsg.text = “Login Failed”

End IfEnd If

Page 28: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Working with LoopsWorking with Loops

For LoopsFor Loops

Dim i As IntegerDim i As Integer

Dim Output As Integer = 0 Dim Output As Integer = 0

For i = 1 To 10 For i = 1 To 10

Output = Output + 1Output = Output + 1

Next i Next i

Page 29: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Working with LoopsWorking with Loops

Using Do While StatementsUsing Do While Statements

Dim Counter As Integer = 0 Dim Counter As Integer = 0

Do While Counter < 10Do While Counter < 10

Counter += 1 Counter += 1

LoopLoop

Page 30: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Working with LoopsWorking with Loops

Using Do While StatementsUsing Do While Statements

Dim Counter As Integer = 0 Dim Counter As Integer = 0

Do Do

Counter += 1 Counter += 1

Loop While Counter < 10Loop While Counter < 10

Page 31: Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals

Working with LoopsWorking with Loops

NoteNote

Do not put the While condition clause in both Do not put the While condition clause in both the Do and the Loop statements because doing the Do and the Loop statements because doing so causes an error when you try to run your so causes an error when you try to run your program.program.