Title | Visual Basic If Statement the coding guys |
---|---|
Author | Bea Eloine |
Course | Computer Science |
Institution | Negros Oriental State University |
Pages | 4 |
File Size | 95.3 KB |
File Type | |
Total Downloads | 78 |
Total Views | 147 |
Download Visual Basic If Statement the coding guys PDF
1/7/2018
VisualBasicIfStatement|thecodingguys
VisualBasicIfStatement|thecodingguys IfyouhaveprogrammedbeforeinlanguageslikeC#,PHP,orJavaScript,thenyouwillbefamiliarwithIf Statements.AnIfStatementissimplyadecisionstatement,andthecodecontainedinsideitwillexecute iftheconditionevaluatestotrue.
Syntax IfconditionThen 'Yourcodehere.. EndIf Tip:IntheVSstudioIDE,ifyoutypeifuntilit'shighlightedandthenhittabtwice,thesyntax willautomaticallybeinserted. CreateanewVBConsoleApplicationandnameitIfStatements.Thencopythefollowing: SponsoredLinks SubMain() DimageAsInteger Console.WriteLine("Pleaseenteryourage:") age=Console.ReadLine().ToString() Ifage=15Then Console.WriteLine("Youarenotoldenoughtodrink") EndIf Console.ReadLine() EndSub
CodeExplained: Firstwecreatedavariablecalled"age"withthedatatypeinteger. Nextweprintedtotheconsole:"Pleaseenteryourage:" Wethenreadtheuser'svalue. Ifyouenter15youwillseethemessage:"Youarenotoldenoughtodrink". Ifyouenteranyothervaluetheprogramwilljustterminatewithoutanynotification. ThisisasimpleIfStatement.TheproblemwiththisIfStatementisthatiftheconditionevaluatestofalse (theageisnot15),theprogramwilldonothing.InthiscasewecanuseanIfElseStatement.
IfElse
Syntax IfconditionThen code Else othercode https://www thecodingguys net/tutorials/visualbasic/vb if statement
1/4
1/7/2018
VisualBasicIfStatement|thecodingguys
EndIf CommentouttheotherIfStatement(CTRL+K+C)andcopythefollowingcode: DimusernameAsString DimpasswordAsInteger username=Nothing password=Nothing Console.WriteLine("EnterYourUserName") username=Console.ReadLine() Console.WriteLine("EnterYourPassword") password=Console.ReadLine() Ifusername="Asim"Andpassword=243Then Console.WriteLine("WelcomeAsim!") Else Console.WriteLine("Accessisdenied") EndIf Console.ReadLine() ThisissimilartotheaboveIfStatement.Youhavetwovariables(string/integer)whichweusetostore thevaluestheuserenterswhenprompted.ThenwecomparethosevaluestothevaluesinourIf Statement.Soifusername="Asim"ANDpassword=243,then"WelcomeAsim"isdisplayed;otherwise "AccessDenied"isdisplayed.YouarealsousingthelogicaloperatorAndwhichwillbeexplainedlater.
NestedIfElse YoucanalsohaveanestedIfElseStatement. DimmessageAsString message="Welcome"&username Ifusername="Asim"Andpassword=243Then Console.WriteLine(message) Else Ifusername="Dave"Andpassword=12345Then Console.WriteLine(message) Else Ifusername="Admin"Andpassword=2012Then Console.WriteLine(message) Else Console.WriteLine("Oopscouldnotfindyou!") EndIf EndIf
EndIf Theprocessisthesameasbefore,butwehavemoredecisions.However,wehavemadeanewstring variablecalled"message";thiswasmadetoavoidrepeatingtheconsolemessage.TheIfElse https://www thecodingguys net/tutorials/visualbasic/vb if statement
2/4...