UNIT 2 Decision Control & Loop Control Finalc
UNIT 2 Decision Control & Loop Control Finalc
1
2
Data type:
Describes that what type of data a variable can hold.
Syntax:
Dim variablename as datatype
3
Some of the data types used in .NET
4
Module Module1
Sub Main()
Dim str As String
str = "HELLO WORLD"
Console. WriteLine (str)
Console. ReadLine ()
End Sub
End Module
5
There are following operators are used in VB. NET
Sr. Name
No
1 Arithmetic Operator
2 Logical Operator
3 Relational Operator
4 Assignment Operator
5 Bitshift Operator
6
1. Arithmetic Operators
7
Module Module1
Sub Main()
Dim a, b, c As Integer
a = 20
b=2
Console. WriteLine("a:" & a)
Console. WriteLine("b:" & b)
c=a+b
Console. WriteLine ("ADDITION IS :" & c)
c=a-b
Console. WriteLine("SUBTRACTION IS :" & c)
c=a*b
Console. WriteLine ("MULTIPLICATION IS :" & c)
c=a/b
Console. WriteLine ("DIVISION IS :" & c)
c = a Mod b
Console. WriteLine("REMAINDER IS :" & c)
c=a^b
Console. WriteLine ("RESULT IS :" & c)
Console. ReadLine ()
End Sub
8
End Module
9
2. Logical Operators
10
Module Module1
Sub Main()
Dim i As Boolean
i = Not 45 > 25
Console. WriteLine("i is :“&i)
i = Not 20 > 45
Console. WriteLine("i is:“&i)
Console. ReadLine()
End Sub
End Module
11
3. Relational Operators
Sr. No Operator Symbol Description
4 Greater than >= Find out the greater value and equal value
equal to by comparison.
5. Less Than <= Find out the smallest value and equal value
equal to by comparison
12
Module Module1
Sub Main()
Dim a, b As Integer
a = 10
b=5
Console. WriteLine("a:" & a)
Console. WriteLine("b:" & b)
If (a = b) Then
Console. WriteLine("a is equal to b")
Else
Console. WriteLine("a is not equal to b")
End If
If (a > b) Then
Console. WriteLine("a is greater than b")
Else
Console. WriteLine("a is not greater than b")
End If
Console. ReadLine()
End Sub
End Module
13
14
4. Assignment Operator
Used to assign value to variable.
Symbol used ‘=’
Assignment can be used with different arithmetic operators
like
Sr. No Operator Description
1 = Used to assign value to variable
2 += Addition is followed by Assignment
3 -= Subtraction is followed by Assignment
4 *= Multiplication is followed by Assignment
5 /= Division is followed by Assignment
6 ^= Exponential is followed by Assignment
15
Module Module1
Sub Main()
Dim i As Integer
i = 10
Console. WriteLine("i is:" & i)
i += 5
Console. WriteLine("i+=5 gives:" & i)
i -= 2
Console. WriteLine("i-=2 gives:" & i)
i *= 3
Console. WriteLine("i*=3 gives:" & i)
i /= 4
Console. WriteLine("i/=4 gives:" & i)
i ^= 2
Console. WriteLine("i^=2 gives:" & i)
Console. ReadLine()
End Sub
End Module
16
17
5. Bitshift Operator
Performs the shift operations on binary values.
That is it works on bits and perform bit by bit operations.
18
19
There are 2 types of control structure.
1. Decision Making control Statement
1. If statement
2. If..else statement
3. Select case statement
2. Looping control statements
1. For Next loop
2. While loop
3. Do..while loop
4. For each loop
20
1.If statement
If conditional allow to execute an expression if a condition is
TRUE & if the condition is FALSE ,then the code after the end
if statement will be executed.
Syntax:
If (condition) then
Statements
End if
21
Module Module1
Sub Main()
Dim age As Integer = 20
Console. WriteLine("The given Age is:" & age)
If (age < 18) Then
Console. WriteLine("Sorry.....Not Eligible for Voting.......")
End If
Console. WriteLine("Please...Vote...")
Console. ReadLine()
End Sub
End Module
22
23
2. If..else statement
If conditional allows us to execute an expression if a condition
is TRUE & execute an else statement if it is FALSE
Syntax:
If condition then
Statements
Else if condition then
Statements
Else
statements
End if
24
Module Module1
Sub Main()
Dim i As Integer
Dim str As String
Console. WriteLine("Enter your Score...")
i = Console. ReadLine()
If (i < 40) Then
str = "Fail"
ElseIf (i < 55) Then
str = "Pass"
ElseIf (i < 75) Then
str = "First Class"
Else
str = "Distinction“
End If
Console. WriteLine(str)
Console. ReadLine()
End Sub
End Module 25
26
3. Select case statement
Executes one of the several groups of statement depending on
the value of an expression.
Syntax:
27
Module Module1
Sub Main()
Dim num As Integer
Console. WriteLine("Enter Number:")
num = Console. ReadLine()
Select Case num
Case 1
Console. WriteLine("ONE")
Case 2
Console. WriteLine("TWO")
Case 3
Console. WriteLine("THREE")
Case 4
Console. WriteLine("FOUR")
Case 5
Console. WriteLine("FIVE")
Case Else
Console. WriteLine("OOoops Wrong choice...")
End Select
Console. ReadLine()
End Sub
End Module
28
29
Allows a procedure to be repeated many times as long as
condition is not full filled.
Used to perform repetitive work
There are following looping statement in VB.NET
1. For Next loop
2. While loop
3. Do..while loop
4. For each loop
30
1. For Next loop
▪ Executes a statement for multiple number of times.
▪ Counter variable called as LoopIndex
▪ LoopIndex: counts number of loop iterations as loop
executes
▪ Syntax:
31
Module Module1
Sub Main()
Dim i As Integer
For i = 1 To 10
Console. WriteLine(i)
Next
Console. ReadLine()
End Sub
End Module
32
33
2. While Loop
▪ Loop keep repeating till the condition is TRUE.
▪ Syntax
Variable Initialization
While (condition)
statements
Increment/Decrement
End while
34
Module Module1
Sub Main()
Dim i As Integer
i=1
While (i <= 10)
Console. WriteLine(i)
i=i+1
End While
Console. ReadLine()
End Sub
End Module
35
36
3.Do .. While Loop
▪ Executes the statements while condition is TRUE.
▪ With Exit Do statement you can terminates Do loop at any
time.
▪ Syntax
Variable Initialization
Do
statements
Increment/Decrement
Exit Do
Loop While condition
37
Module Module1
Sub Main()
Dim i As Integer
i=1
Do
Console. WriteLine(i)
i=i+1
End Module
38
39
4. For each Loop
▪ Used to Loop over elements in an array.
▪ It automatically loops over all the elements in the array.
▪ Syntax
40
Module Module1
Sub Main()
Dim i, a(3) As Integer
Console.writeline(“Array Elements are........”)
a(0) = 10
a(1) = 40
a(2) = 60
a(3) = 100
For Each i In a
Console. WriteLine (i)
Next i
Console. ReadLine ()
End Sub
End Module
41
42
43
There are various from controls available in VB.NET
Toolbox.
Controls can be drawn on form to enable the user interaction
with application.
Examples of controls are Textbox, Button, Label, Radio
Button ,Combo Box etc.
Every control has properties, methods(procedure built into a
control that tells the control how to do things) & event
(Action that occurs in your program)
Allows us work with controls at design time or at run time.
44
Different form controls are:
Sr.No Control Sr.No Control Sr.No Control
45
Different form controls are:
Sr.No Control
16 Tooltip
17 Tree view
18 Image box
19 Image List
20 Status Bar
46
1. Button
Used to initiate function, compare values and many more
actions.
Button can be clicked by Mouse or if the button has focus with
enter key or space bar.
47
Public Class Form1
Private Sub Form1_Load(sender As Object, e As Event Args)
Handles MyBase. Load
Dim b As New Button
b. Text = "Click Me..."
Me. Controls. Add(b)
End Sub
End Class
48
Public Class Form1
Private Sub Form1_Load(sender As Object, e As Event Args)
Handles MyBase. Load
Dim b As New Button
b. Text = "Click Me...”
b. Location = New Point(100, 100)
Me. Controls. Add(b)
End Sub
End Class
49
At design time to create Button:
1.Drag and drop button from toolbox on form
2.Then select /click on button to set properties
in property window::Text
3.For coding double click the control
50
2. Textbox
Used display information
By default it can accept 2048 character.
51
Public Class Form1
Private Sub Form1_Load(sender As Object, e As Event Args) Handles
MyBase. Load
Dim t As New TextBox
t. Text = "HELLO"
t. Location = New Point(100, 100)
Me. Controls. Add(t)
End Sub
End Class
52
At design time to create Textbox:
1.Drag and drop Textbox from toolbox on form
2.Then select /click on textbox to set properties
in property window::Text
3.For coding double click the control
53
3. Label
Used display text that user can not edit directly.
i. e. used display message to user.
54
At design time to create Label:
1.Drag and drop Label from toolbox on form
2.Then select /click on Label to set properties
in property window::Text
3.For coding double click the control
55
4. Radio Button
Used to select one item within group of items.
When user selects one option within group then other options clear
automatically.
Options are YES/NO,TRUE/FALSE,MALE/FEMALE
Default event: CheckedChange event
Set Property of Radio Button: name , Text
56
To create Radio Button:
1.Drag and drop Radio Button from toolbox on form
2.Then select /click on Radio Button to set properties in property window::Text
3.For coding double click the control
57
5. Checkbox
Used to select one or multiple items within group of items.
Allow the user to choose from combination of options
Checkbox s clicked to select and clicked again to deselect
option.
When selected Tick Mark appears which shows selection.
Options are YES/NO,TRUE/FALSE,MALE/FEMALE etc..
Default event: CheckedChange event
Set Property of Checkbox: name , Text
58
To create Check Box:
1.Drag and drop Check Box from toolbox on form
2.Then select /click on Check Box to set properties in property window::Text
3.For coding double click the control
59
6. Listbox
Used to display a list of items from which user can select one
or multiple items from the list.
Default event: SelectedIndexChanged Event
Set Property of List box : name , items, SelectionMode(to
select multiple options otherwise no need to set.)
60
To create List Box:
1.Drag and drop List Box from toolbox on form
2.Then select /click on List Box to set properties in property window::items
3.For coding double click the control
61
7. Combobox
Used to display data in drop-down list
From the drop-down list user can select one item from list.
62
To create Combo Box:
1.Drag and drop Combo Box from toolbox on form
2.Then select /click on Combo Box to set properties in property window::Text
3.For coding double click the control
63
8. Picturebox
Used to display image on the form.
64
To create picture Box:
1.Drag and drop picture Box from toolbox on form
2.Then select /click on picture Box to set properties in property window::image
3.For coding double click the control
65
9. Panel
Panel control is Container Control.
Used to host group of similar child controls
66
To create Panel:
1.Drag and drop Panel from toolbox on form
2.Then select /click on Panel to set properties in property window : Name , Back color
3.For coding double click the control
67
10. Tab Control
Tab control is just like divider in notebook.
Manages Tab pages where each may host different child
controls.
As multiple tables are included so switching between tab pages
can be allowed.
68
To Design Tab Control:
1.Drag and drop Tab Control from toolbox on form
2.Then select /click on Tab Control to set properties in property window :Text
3.For coding double click the control
69
10. Timer
Used when user wants to perform some task or action
continuously at regular interval of time.
Methods used:
1.start():used to start timer
2.stop():used to stop timer
70
To Design Timer Control:
1.Drag and drop Timer Control from toolbox on form
2.Then select /click on Timer Control to set properties in property window : Interval
3.For coding double click the control
Code Window
Public Class Form1
Private Sub Button1_Click(sender As Object, e As Event Args) Handles Button1.Click
Timer1.Enabled = True
Me. BackColor = Color. Cyan
End Sub
End Class
71
Form Design
Output Window
72
11. Month Calendar
Allows user to select date from calendar
Select date visually from calendar displayed on form.
73
To Design Month Calendar Control:
1.Drag and drop Month Calendar Control from toolbox on form
2.Then select /click on Month Calendar Control to set properties in property window
3.For coding double click the control
Code Window
Label1.Text = MonthCalendar1.SelectionStart.ToLongDateString
End Sub
74
Form Design
Output Window
75
12. Date Time picker
Allows user to select date & time by displaying calendar on
form.
It display only Day and date .so requires less space.
76
To Design Date Time Picker Control:
1.Drag and drop Date Time Picker Control from toolbox on form
2.Then select /click on Date Time Picker Control to set properties in property window
3.For coding double click the control
Code Window
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As
Event Args) Handles DateTimePicker1.ValueChanged
Label1.Text = DateTimePicker1.Value.ToShortDateString
End Sub
77
Form Design
Output Window
78
13. Error provider
Used to set validation errors
i.e. When input is invalid for any control on the form , it allows
you to set error message for that control.
79
To Design Errorprovider Control:
1.Drag and drop Errorprovider Control from toolbox on form
2.Then select /click on Errorprovider Control to set properties in property window
3.For coding double click the control
Code Window
Private Sub Button1_Click(sender As Object, e As Event Args) Handles
Button1.Click
If TextBox1.Text = "" Then
ErrorProvider1.SetError(TextBox1, "Please..Add some text..")
Else
ErrorProvider1.SetError(TextBox1, "")
End If
End Sub
80
Form Design
Output Window
81
14. Progress Bar
Shows the progress of operation.
82
To Design Progress Bar Control:
1.Drag and drop Progress Bar Control from toolbox on form
2.Then select /click on Progress Bar Control to set properties in property window
3.For coding double click the control
Code Window
ProgressBar1.PerformStep()
End Sub
83
Form Design
Output Window
84
15. Image List
Contains collection of images.
85
16. Status strip
Contains several panels which informs the status of application
May appears on top , bottom of application.
86
To Design Status Bar Control:
1.Drag and drop status Bar Control from toolbox on form
2.Then select /click on status Bar Control to set properties in property window
3.For coding double click the control
Code Window
ToolStripStatusLabel1.Visible = True
End Sub
87
Form Design
Output Window
88
17. ToolTip
Will display small window with text when mouse is over a
control giving a hint about that control
89
To Design ToolTip Control:
1.Drag and drop ToolTip Control from toolbox on form
2.Then select /click on ToolTip Control to set properties in property window
3.For coding double click the control
Code Window
90
Form Design
Output Window
91
18. Message Box
It’s a dialog box used to display information to user
Msgbox () always return value..
Message box also used with show() method.
OK MsgBoxResult.Ok 1
Cancel MsgBoxResult.Cancel 2
Abort MsgBoxResult.Abort 3
Retry MsgBoxResult.Retry 4
Ignore MsgBoxResult.Ignore 5
Yes MsgBoxResult.Yes 6
NO MsgBoxResult.No 7
92
93
Private Sub ok_Click(sender As Object, e As EventArgs) Handles ok.Click
MsgBox("Its okk...", MsgBoxStyle.Critical, "No Error...")
MessageBox.Show("good...", "no error...", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
End Sub
End Sub
94
Private Sub ignorbt_Click(sender As Object, e As Event Args) Handles ignorbt.Click
MsgBox("Please Ignore..", MsgBoxStyle. AbortRetryIgnore, "Ignored......")
MessageBox.Show("Pleas Ignore...", "Ignored....", MessageBoxButtons.
AbortRetryIgnore, MessageBoxIcon. Information)
End Sub
End Sub
95
19. Input Box
It’s a dialog box used to accept values from user
96
97
98