Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

UNIT 2 Decision Control & Loop Control Finalc

fg

Uploaded by

sijisag802
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

UNIT 2 Decision Control & Loop Control Finalc

fg

Uploaded by

sijisag802
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

Decision control & loop control

1
2
 Data type:
Describes that what type of data a variable can hold.

 Syntax:
Dim variablename as datatype

 Ex. Dim a As Integer

3
Some of the data types used in .NET

Sr. Data Type Description


No
1 Integer Used to represent integer values
2 String Represents a string
3 Char Represent a single character
4 Double Represents double precision floating point
5 Boolean TRUE and FALSE
6 Date Represents Date

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

Sr. No Operator Symbol Description

1 Addition + Perform addition of numeric elements


2 Subtraction - Perform subtraction of numeric elements
3 Multiplication * Performs Multiplication of numeric
elements
4 Division / Used to perform Division
5 Mod Mod Used to find out remainder of division
6 Exponential ^ Used to find number raised to value.

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

Sr. No Operator Symbol Description

1 Logical AND And If both conditions TRUE then only result


TRUE
2 Logical NOT Not Reverse of TRUE
3 Logical OR Or If either conditions TRUE then only result
TRUE

4 Logical XOR Xor TRUE if either one condition is TRUE but


not both is TRUE.

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

1 Equal to < >,= Checks values of two operands are equal or


not
2 Greater Than > Find out the greater value by comparison.

3 Less Than < Find out the smallest value by comparison

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.

Sr. No Operator Name Description

1 << Binary Here left operand value is moved to


Left Shift left by the number of bits specified
by right operand
2 >> Binary Here left operand value is moved to
Right Shift right by the number of bits specified
by right operand

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:

Select case expression


Case expression1:
statement;
Case expression2:
statement;
Case else:
statement;
End select

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:

for index=Start To End(step increment)


Statements
Next

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

Loop While i <= 10


Console. ReadLine()
End Sub

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

For Each element In group/array name


Statements
Exit for
Statement
Next [element]

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

1 Button 6 List Box 11 Panel

2 Textbox 7 Combo Box 12 Month Calendar

3 Label 8 Picture Box 13 Date time picker

4 Radio Button 9 Tab Control 14 Error provider

5 Check Box 10 Timer 15 Progress Bar

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.

 Default event: Click event


 Set Properties of button : name , Text

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.

 Default event: TextChanged event


 Set Properties of Textbox: name, text

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.

 Default event: Click event


 Set Property of Label: Text

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

Form Design Output Window

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

Form Design Output Window

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

Form Design Output Window

61
7. Combobox
 Used to display data in drop-down list
 From the drop-down list user can select one item from list.

 Default event: SelectedIndexChanged Event


 Set Property of Combobox Button: name, items

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

Form Design Output Window

63
8. Picturebox
 Used to display image on the form.

 Set Property of Picturebox: name, image

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

Form Design Output Window

65
9. Panel
 Panel control is Container Control.
 Used to host group of similar child controls

 Set Property of Panel: name , Back color

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

Form Design Output Window

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.

 Set Property of Tab Control : name,text

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

Form Design Output Window

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

 Set Property of timer: interval(1000 Millisecond ),Enabled

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.

 Set Property of Month Calendar: name, selection range

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

Private Sub MonthCalendar1_DateChanged(sender As Object, e As Date


Range Event Args) Handles MonthCalendar1.DateChanged

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.

 Set Property of Date Time picker : name , value

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.

 Set Property of Error provider : Seterror() method

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.

 Set Property of Progress Bar :value(current position of


progress bar ),min , max , step

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

Private Sub Button1_Click(sender As Object, e As Event Args)


Handles Button1.Click

ProgressBar1.PerformStep()

End Sub

83
Form Design
Output Window

84
15. Image List
 Contains collection of images.

 Set Property of Image box : images

85
16. Status strip
 Contains several panels which informs the status of application
 May appears on top , bottom of application.

 Set Property of Status strip: text

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

Private Sub Form1_Load(sender As Object, e As Event Args)


Handles MyBase. Load

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

 Set Property of ToolTip: SetToolTip() method

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

Private Sub Form1_Load(sender As Object, e As Event Args) Handles


MyBase. Load
If TextBox1.Text = "" Then
ToolTip1.SetToolTip(TextBox1, "CAPITAL LETTERS ONLY..")
End If
End Sub

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.

If user click button Function Returns Value

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

Private Sub cancel_Click(sender As Object, e As EventArgs) Handles Cancel.Click


MsgBox("Its Cancelled...", MsgBoxStyle.OkCancel, "Cancelled...")
MessageBox.Show("Cancelled...", "Error...", MessageBoxButtons.OKCancel,
MessageBoxIcon.Error)
End Sub

Private Sub abortbt_Click(sender As Object, e As EventArgs) Handles abortbt.Click


MsgBox("Its Aborted..", MsgBoxStyle.AbortRetryIgnore, "Aborted......")
MessageBox.Show("Aborted...", "Pls Abort....", MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Stop)
End Sub

Private Sub retrybt_Click(sender As Object, e As EventArgs) Handles retrybt.Click


MsgBox("Please Retry..", MsgBoxStyle.AbortRetryIgnore, "Retry......")
MessageBox.Show("Pleas Retry...", "Retry....", MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Error)

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

Private Sub ysbt_Click(sender As Object, e As EventArgs) Handles ysbt.Click


MsgBox("YES/NO..", MsgBoxStyle.YesNo, "YES/NO....")
MessageBox.Show("YES/NO...", "QUESTION....", MessageBoxButtons.YesNo,
MessageBoxIcon.Question)

End Sub

Private Sub bttnn_Click(sender As Object, e As Event Args) Handles bttnn.Click


MsgBox("YES/NO..", MsgBoxStyle.YesNo, "YES/NO....")
MessageBox.Show("YES/NO...", "QUESTION....", MessageBoxButtons.YesNo,
MessageBoxIcon.Question)
End Sub

95
19. Input Box
 It’s a dialog box used to accept values from user

Private Sub Form1_Load(sender As Object, e As EventArgs)


Handles MyBase .Load
Dim a As Integer
a = InputBox ("please enter integer value for a:")
End Sub

96
97
98

You might also like