View
217
Download
0
Embed Size (px)
Citation preview
7/25/2019 Lecture 4_decisions and Conditions_EDITED
1/24
LECTURE 4
7/25/2019 Lecture 4_decisions and Conditions_EDITED
2/24
4- 2Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
Used to make decisions Always indent for readability & debugging
Then must be on same line as f or !lsef !nd f and !lse must a""ear alone on a line Notice that !lsef is # word$ !nd f is 2
words Always !nd with !nd f
7/25/2019 Lecture 4_decisions and Conditions_EDITED
3/24
4- %Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
Ifcondition' Thenstatements to "erform if condition(true
[ElseIfcondition' Then
statements to "erform if#st condition(false and2nd condition(true)
[Elsestatements to "erform ifboth conditions( false)
EndIf
7/25/2019 Lecture 4_decisions and Conditions_EDITED
4/24
4- 4Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
*reater Than > ess Than < !,ual To =
Not !,ual To *reater Than or !,ual To >= ess Than or !,ual to
7/25/2019 Lecture 4_decisions and Conditions_EDITED
5/24
4- Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
Negati.e numbers are always less than"ositi.e numbers
/trings can be com"ared also don0t
forget to enclose the strings in ,uotes'1AN is less than 13N
3! is less than 3!!//
1oan does not e,ual 1AN
Numbers are always less than letters %5567 is less than orsche
7/25/2019 Lecture 4_decisions and Conditions_EDITED
6/24
4- 8Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
int9redits(9ntt:t9redits;Te:t'Ifint9redits < %2 Then
rad=reshman;9hecked ( TrueElseIfint9redits < 84 Then
rad/o"homore;9hecked ( TrueElseIfint9redits < >8 Then
rad1unior;9hecked ( TrueElserad/enior;9hecked ( True
EndIf
7/25/2019 Lecture 4_decisions and Conditions_EDITED
7/244- ?Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
Ifbln/uccessful"eration ( TrueThen
; ; ;EndIf
is equivalent to
Ifbln/uccessful"eration Then; ; ;
EndIf
7/25/2019 Lecture 4_decisions and Conditions_EDITED
8/244- @
Ayatis Notes + Programming InVisual Basic.NET by Julia CaseBradley & Anita C. Millspaug
Use ToUpperand ToLowermethods of the/tring class to con.ert strings forcom"arisons
txtGender.Text contains Male
Ift:t*ender;Te:t;ToUpper( BA! Then; ; ;EndIf
7/25/2019 Lecture 4_decisions and Conditions_EDITED
9/244- >
Ayatis Notes + Programming InVisual Basic.NET by Julia CaseBradley & Anita C. Millspaug
1oin conditions using ogical "erators r either true e.aluates to true
And both true e.aluates to true
Not re.erses the condition$ so that
true will e.aluate false andfalse will e.aluate true
7/25/2019 Lecture 4_decisions and Conditions_EDITED
10/244- #5
Ayatis Notes + Programming InVisual Basic.NET by Julia CaseBradley & Anita C. Millspaug
IfradBale;9hecked ( True And9ntt:tAge;Te:t' < 2# ThenmintBinorBale9ount + ( mintBinorBale9ount
End If
radBale not checked =alset:tAge greater than 2# =alseradBale not checked$ t:tAge less than 2# =alseradBale checked$ t:tAge 2# or greater =alse
radBale checked$ t:tAge less than 2# True
7/25/2019 Lecture 4_decisions and Conditions_EDITED
11/244- ##
Ayatis Notes + Programming InVisual Basic.NET by Julia CaseBradley & Anita C. Millspaug
Ifrad1unior;9hecked ( True Orrad/enior;9hecked ( True ThenmintU""er9lass9ount + ( mintU""er9lass9ount
End If
rad1unior;Calue(True Truerad/enior;Calue(True Truerad1unior;Calue(=alse$ rad/enior;Calue(True Truerad1unior;Calue(True$ rad/enior;Calue(=alse True
rad1unior;Calue(=alse$ rad/enior;Calue(=alse =alse
7/25/2019 Lecture 4_decisions and Conditions_EDITED
12/244- #2
Ayatis Notes + Programming InVisual Basic.NET by Julia CaseBradley & Anita C. Millspaug
Ifrad1unior;9hecked ( True Orrad/enior;9hecked ( TrueAndradBale;9hecked ( True And9ntt:tAge;Te:t' < 2#
Ifrad1unior;9hecked ( True Orrad/enior;9hecked ( True'And (radBale;9hecked ( True And9ntt:tAge;Te:t''< 2#
Then
mintBinorBale9ount + ( mintBinorBale9ountmintU""er9lass9ount + ( mintU""er9lass9ount
End If
7/25/2019 Lecture 4_decisions and Conditions_EDITED
13/244- #%
Ayatis Notes + Programming InVisual Basic.NET by Julia CaseBradley & Anita C. Millspaug
IfintTem" D %2 ThenIfintTem" D @5 Then
lbl9omment;Te:t ( 3otElse
lbl9omment;Te:t(BoderateEndIf
Elselbl9omment;Te:t(=reeEing
End If
7/25/2019 Lecture 4_decisions and Conditions_EDITED
14/24
4- #4Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
nstead of coding the 9heck9hangede.ents$ use =s to see which are selected
lace the = code in the 9lick e.ent for aFutton$ such as btuG
ri.ate /ub btuHis"layI9lick'
IfchkFold;9hecked(TrueThen
lblBessage;=ontFold(TrueEndIf
!nd /ub
7/25/2019 Lecture 4_decisions and Conditions_EDITED
15/24
4- #Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
7/25/2019 Lecture 4_decisions and Conditions_EDITED
16/24
4-#8
Ayatis Notes + Programming InVisual Basic.NET by Julia CaseBradley & Anita C. Millspaug
=or longer$ more com"le: messages store themessage te:t in a /tring .ariable and use that
.ariable as an argument of the /how method
MessageBox.Show(Number of Orders: &_mOrderCount & _
ControlChars.NewLine& Total Sales &_!ormat"e#$mal(mde#Total% _
ControlChars.CrLf& _
'erage Sale: & !ormatNumber(mde#'g% _
Coffee Sales Summar)%
"$m strMessageas Str$ng
"$m strTotalOut as Str$ng
"$m str'gOut as Str$ng
strTotalOut * Total Sales & !ormat"e#$mal(mde#Total%
str'gOut * 'erage Sale: & !ormatNumber(mde#'g%
strNumOrder * Number of Orders: & mOrderCount
strMessage* strNumOrder & _
ControlChars.NewLine& _
strTotalOut _
ControlChars.CrLf& _
str'gOut _
MessageBox.Show(strMessage Coffee Sales Summar)%
7/25/2019 Lecture 4_decisions and Conditions_EDITED
17/24
4-#?
Ayatis Notes + Programming InVisual Basic.NET by Julia CaseBradley & Anita C. Millspaug
ControlChars.NewLine
Used to force to next line
J Bessage Fo: 9an ha.e Bulti"le ut"ut inesJ Kra" longer messages to a second line
J nclude 9ontrol9hars to control the line length and "osition of theline break
BessageFo:;/howLNumber of rdersM L & mrder9ount & I
9ontrol9hars;NewLine& ILTota l/ales & =ormatHecimalmdecTotal' I
9ontrol9hars;CrLf& ILA.erage /aleM L & =ormatNumbermdecA.g' I $ L9oOee /ales /ummary'
7/25/2019 Lecture 4_decisions and Conditions_EDITED
18/24
4-#@
Ayatis Notes + Programming InVisual Basic.NET by Julia CaseBradley & Anita C. Millspaug
Use BessageFo:Futtons9onstants to dis"laymore than one button inthe Bessage Fo:
PecallM Constants covered inCh 3: AbortRetryIgnore, OK,
OKCancel, RetryCancel, YesNo,
YesNoCancel)
MessageBoxButtons.YesNo
7/25/2019 Lecture 4_decisions and Conditions_EDITED
19/24
4- #>Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
Use BessageFo:Futtons 9onstants todis"lay more than one button in theBessage Fo:9onstants co.ered in 9h %M AbortPetrygnore$ G$G9ancel$ Petry9ancel$ QesNo$ QesNo9ancel'
Bessage Fo:0s /how Bethod returns aDialogResl!obRect that can be checkedto see which button the user clicked
Heclare a .ariable that can hold aninstance of the DialogResl!ty"e toca"ture the outcome of the /how Bethod
7/25/2019 Lecture 4_decisions and Conditions_EDITED
20/24
4- 25Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
Him dgrResl!as HialogPesultHim s!r"sgas /trings!r"sg= #Clear $rren! order%gresdgrResl!="essage'o)*how(s!r"sg+ ,
"essage'o'!!ons)-esNo+ ,
"essage'oI$on).es!ion +#Clear# /
f dgrResl!( HialogPesult;Qes
Then
De$laring anO01e$! 2aria0le for
!he "e!hodRe!rn
7/25/2019 Lecture 4_decisions and Conditions_EDITED
21/24
4- 2#Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
Using a diOerent signature for the Bessage Fo:/how method to s"ecify a default buttonM
Him dgrPesult As HialogPesultHim strBessage As /tring09onSrm clear of current order
strBessage ( 9lear the current order Sgures
dgrResl!( BessageFo:;/howstrBessage$ 9lear rder$ I
BessageFo:Futtons;QesNo$BessageFo:con;uestion$ I
BessageFo:HefaultFutton;Futton2$ I
BessageFo:"tions;PightAlign'
Add the
BessageFo:HefaultFutton argument
after theBessageFo:cons
argument
Bessage Fo:"tion Argument
f dgrResl!( HialogPesult;Qes Then09ode to clear the order
!nd f
7/25/2019 Lecture 4_decisions and Conditions_EDITED
22/24
4- 22Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
9hecking to see if .alid .alues wereentered by user in Te:tFo: 33use BessageFo: to notify user if in.alid
9hecking for re,uired data or not blankM f t:tName;Te:t
7/25/2019 Lecture 4_decisions and Conditions_EDITED
23/24
4- 2%Ayatis Notes + Programming In
Visual Basic.NET by Julia CaseBradley & Anita C. Millspaug
9ode f structure to determine if .alue
falls within a rangeof acce"table .alues Use nested f structure to .alidate
multi"le .alues on a form Use single f for each .alue$ !lse for message
to user$ and e:ecute o$s "e!hodto resetfocus to te:t bo: in ,uestion
rder of fs should match Tabrder of te:tbo:es on form
7/25/2019 Lecture 4_decisions and Conditions_EDITED
24/24
LECTURE 4