0% found this document useful (0 votes)
9 views46 pages

Unit 1 Introduction To VB - NET Part1

This document serves as an introduction to VB.NET, covering basic programming concepts such as data types, arithmetic operators, control structures, and problem-solving techniques. It explains the syntax and structure of Visual Basic, including variable declaration, naming conventions, and operator precedence. Additionally, it discusses control structures like sequence, selection, and repetition, along with examples and exercises to reinforce learning.

Uploaded by

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

Unit 1 Introduction To VB - NET Part1

This document serves as an introduction to VB.NET, covering basic programming concepts such as data types, arithmetic operators, control structures, and problem-solving techniques. It explains the syntax and structure of Visual Basic, including variable declaration, naming conventions, and operator precedence. Additionally, it discusses control structures like sequence, selection, and repetition, along with examples and exercises to reinforce learning.

Uploaded by

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

Unit 1

Introduction to VB.NET
Objectives
• In this chapter, you will:
– Learn the primitive data types in Visual Basic
– Become familiar with arithmetic operators
– Explore how to design algorithms to solve
problems
– Learn the components of basic control structures
– Study the syntax of basic sequence, selection, and
repetition structures in Visual Basic

2
Introduction
• Computer program
– Sequence of statements whose objective is to
accomplish a task
• Programming
– Process of planning and creating a program

3
Introduction (cont'd)
• Function
– Collection of statements; when executed,
accomplishes something
• Syntax
– Rules that specify which statements (instructions)
are legal
• Programming language
– A set of rules, symbols, and special words
– Visual Basic

4
Introduction (cont'd)
• Reserved words, keywords, or word symbols
– Words that are reserved by Visual Basic
– Usually in blue color in the IDE (Visual Studio)

5
Primitive Data Types in Visual
Basic
• Boolean • SByte
• Byte • Short
• Char • Single
• Date • String
• Decimal
• Uinteger
• Double
• Ulong
• Integer
• Long • UShort

6
Declaration of Variables
• All variables must be declared before they are
used in a program
• Declaring a variable
– Dim number1 As Integer
– Dim number2 As Integer
• Declaring multiple variables of the same type
– Dim number1, number2 As Integer

7
Naming Convention
• Camel case
– Variable / function name
– E.g., taxRate, salaryPayment
• Control naming convention
– Variable name = the meaning of control's value+
control's type
– E.g., number1Label, number2TextBox
• Although the variable/function name is not case
sensitive, it is important to follow a consistent
naming convention
8
Arithmetic Operators in Visual
Basic
• Addition: +
• Subtraction: -
• Multiplication: *
• Division (floating point): /
• Division (integer): \
• Modulus: Mod
• Exponentiation: ^

9
Division
• Division (floating point)
– x/y
– E.g., 7.l / 4 evaluates to 1.775

10
Division (cont'd)
• Division (integer)
–x\y
– x and y are integers
• 7\4 evaluates to 1, and 17\5 evaluates to 3
– x and y are not integers
• Numbers are first rounded to the nearest whole number
• E.g., 7.1 is rounded to 7, and 7.7 is rounded to 8
• Thus, 7.1\4 evaluates to 1, and 7.7\4 yields 2

11
Modulus & Exponentiation
• Modulus
– r Mod s
– 7 Mod 3 evaluates to 1 (since 7=3*2+1)
• Exponentiation
– 3^2 evaluates to 3*3=9
– 2^3 evaluates to 2*2*2=8
– 2^10 evaluates to 1024

12
Sign Operations
• Unary Minus
– -e
– E.g., -10, -3.14
• Unary Plus
– +g
– E.g., +100 (equivalent to 100)

13
Rules of Operator Precedence
priority
• ^ high
• +, - (sign operations)
• *, /
• \
• Mod
• +, - (addition and subtraction) low
• If there are several operators of the same priority,
then they are evaluated from left to right

14
Exercises
• What are the values of the following
expressions?
– 5.2/2
– 9 Mod 3
– 4\2
– 4.4\2
• What is the order of the following expression?
–X=2*5^2+3*5+7

15
Comparison Operators
• Equality operators
– = (equal)
– <> (not equal)
• Relational operators
– >
– <
– >=
– <=

16
Rules of Operator Precedence
priority
• ^ high
• +, - (sign operations)
• *, /
• \
• Mod
• +, - (addition and subtraction)
• =, <>, <, <=, >, >=
(equality and relational) low

17
Problem Solving Techniques
• Problem-solving process has three steps:
– Analyze problem and design an algorithm
– Implement the algorithm in code
– Maintain the program
• Algorithm is independent of languages
– Actions to be executed, and
– The order in which these actions are executed
• Pseudo code
18
Control Structures
• Problem with the GoTo statement
– GoTo statement can specify a control to any place
(line or destination) in a program
– Making the program unstructured and hard to
follow
• Research indicates that all programs can be
written by only 3 control structures
– With "GoTo elimination"

19
Categories of Control Structures
• Control Structures
– Sequence structure
– Selection structure
– Repetition structure

20
Sequence Structure
• Visual Basic statement
– total = total + grade
– counter=counter+1 Add grade to total
• UML activity diagram
– Flowchart
– Initial state action state 1 Add 1 to counter
… action state n
final state

21
Selection Structure
• If … Then
• If … Then … Else
• Select … Case
[grade>=60] display
"passed"

[grade<60]

22
Selection (1)
• If … Then
– If grade >= 60 Then
write(“Passed”)
End If

23
Selection (2)
• If … Then … Else
– If grade >= 60 Then
write(“Passed”)
Else
write (“Failed”)
End If

24
Nested Selection
If grade >= 90 Then
write(“A”)
Else
If grade >= 80 Then
write(“B”)
Else
If grade >= 70 Then
write(“C”)
Else
write(“F”)
End If
End If
End If
25
Alternative Version
If grade >=90 Then
write (“A”)
ElseIf grade >=80 Then
write(“B”)
ElseIf grade >= 70 Then
write(“C”)
Else
write(“D”)
End If

26
Repetition Structure
• Visual Basic provides 7 repetition statements
– Do While … Loop
– While … End While
– Do Until … Loop
– Do … Loop While
– Do … Loop Until
– For … Next
– For Each … Next

27
Example of Repetition (cont'd)
• While + loop-continuation condition
-------------------------------------------------------------------
Do While product <=100
product = product * 3 ' compute next power of 3
Loop
-----------------------------------------------------------
While product <=100
product = product * 3 ' compute next power of 3
End While

28
Example of Repetition (cont'd)
• Until + loop-termination condition
-------------------------------------------------------------------
Do Until product > 100
product = product * 3 ' compute next power of 3
Loop

29
If…Then…Else – General Form
If (condition) Then
statement(s) True
Condition
[ElseIf (condition) Then
statement(s)] False
[Else
Statement Statement
statement(s)]
End If

4- 30
If…Then…Else - Example

unitsDecimal =
Decimal.Parse(unitsTextBox.Text)
If unitsDecimal < 32D Then
freshmanRadioButton.Checked = True
Else
freshmanRadioButton.Checked = False
End If
4- 31
The Six Relational Operators
• Greater Than >
• Less Than <
• Equal To =
• Not Equal To<>
• Greater Than or Equal To >=
• Less Than or Equal to <=

© 2005 by The McGraw-Hill 4- 32


Companies, Inc. All rights
reserved.
ToUpper and ToLower Methods

• Use ToUpper and ToLower methods of


the String class to return the uppercase
or lowercase equivalent of a string,
respectively
If nameTextBox.Text.ToUpper( ) = "Basic"
Then
' Do something.
End If

4- 33
Compound Conditions
• Join conditions using logical
Condition 1
operators
OR T F
– Or If one or both conditions True,
entire condition is True T T T

Condition 2
– And Both conditions must be
F T F
True
for entire condition to be True Condition 1

– Not Reverses the condition, a AND T F


True condition will evaluate False T T F

Condition 2
and vice versa
F F F
4- 34
Compound Condition Examples

If maleRadioButton.Checked And _
Integer.Parse(ageTextBox.Text) < 21 Then
minorMaleCountInteger += 1
End If

If juniorRadioButton.Checked Or seniorRadioButton.Checked Then


upperClassmanInteger += 1
End If

4- 35
Combining And and Or Example

If saleDecimal > 1000.0 Or discountRadioButton.Checked _


And stateTextBox.Text.ToUpper( ) <> "CA" Then
' Code here to calculate the discount.
End If

4- 36
Nested If Statements
If tempInteger > 32 Then
If tempInteger > 80 Then
commentLabel.Text = "Hot"
Else
commentLabel.Text =
"Moderate"
End If
Else
commentLabel.Text = "Freezing"
End If 4- 37
Using If Statements with Radio
Buttons & Check Boxes
• Instead of coding the CheckedChanged
events, use If statements to see which
are selected
• Place the If statement in the Click event
Private
forSub testButton_Click(ByVal
a Button, such as an OK sender
or As
Apply
System.Object, _
button
By Val e As System.EventArgs) Handles testButton.Click
' Test the value of the check box.
If testCheckBox.Checked Then
messageLabel.Text = "Check box is checked"
End If
End Sub
© 2005 by The McGraw-Hill
Companies, Inc. All rights
4- 38

reserved.
Enhancing Message Boxes
• For longer, more complex messages,
store the message text in a String variable
and use that variable as an argument of
the Show method
• VB will wrap longer messages to a second
line
• Include ControlChars to control the line
length and position of the line break

4- 39
Message Box - Multiple Lines of
Output

ControlChars.NewLine
Used to force to next line

4- 40
Message String Example
Dim formattedTotalString As String
Dim formattedAvgString As String
Dim messageString As String
formattedTotalString = totalSalesDecimal.ToString("N")
formattedAvgString =
averageSalesDecimal.ToString("N")
messageString = "Total Sales: " &
formattedTotalString _
& ControlChars.NewLine & "Average Sale: " & _
formattedAvgString
MessageBox.Show(messageString, "Sales Summary",
MessageBoxButtons.OK)
© 2005 by The McGraw-Hill
Companies, Inc. All rights
4- 41

reserved.
Displaying Multiple Buttons
• Use MessageBoxButtons constants to
display more than one button in the
Message Box
• Message Box's Show method returns a
DialogResult object that can be checked
to see which button the user clicked
• Declare a variable to hold an instance of
the DialogResult type to capture the
outcome of the Show method
4- 42
Message Box - Multiple Buttons

MessageBoxButtons.YesNo

© 2005 by The McGraw-Hill 4- 43


Companies, Inc. All rights
reserved.
Declaring a Variable for the
Method Return
Dim whichButtonDialogResult As DialogResult

whichButtonDialogResult = MessageBox.Show _
("Clear the current order figures?", "Clear Order", _
MessageBoxButtons.YesNo,
MessageBoxIcon.Question)
If whichButtonDialogResult = DialogResult.Yes
Then
' Code to clear the order.
End If
4- 44
Input Validation
• Check to see if valid values were entered by
user before beginning calculations
• Check for a range of values
(reasonableness)
– If Integer.Parse(hoursTextBox.Text) > 10 Then
MessageBox.Show("Too many hours")
• Check for a required field (not blank)
– If nameTextBox.Text <> "" Then ...

4- 45
Performing Multiple Validations
• Use nested If statement to validate multiple
values on a form
– Examine example on Page 156
• Use Case structure to validate multiple values
– Simpler and clearer than nested If
– No limit to number of statements that follow a Case
statement
– When using a relational operator must use the word
Is
– Use the word To to indicate a range of constants
4- 46

You might also like