Visual Basic If Statement the coding guys PDF

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 PDF
Total Downloads 78
Total Views 147

Summary

Download Visual Basic If Statement the coding guys PDF


Description

1/7/2018

VisualBasicIfStatement|thecodingguys

VisualBasicIfStatement|thecodingguys IfyouhaveprogrammedbeforeinlanguageslikeC#,PHP,orJavaScript,thenyouwillbefamiliarwithIf Statements.AnIfStatementissimplyadecisionstatement,andthecodecontainedinsideitwillexecute iftheconditionevaluatestotrue.

Syntax IfconditionThen 'Yourcodehere.. EndIf Tip:IntheVSstudioIDE,ifyoutypeifuntilit'shighlightedandthenhittabtwice,thesyntax willautomaticallybeinserted. CreateanewVBConsoleApplicationandnameitIfStatements.Thencopythefollowing: SponsoredLinks SubMain() DimageAsInteger Console.WriteLine("Pleaseenteryourage:") age=Console.ReadLine().ToString() Ifage=15Then Console.WriteLine("Youarenotoldenoughtodrink") EndIf Console.ReadLine() EndSub

CodeExplained: Firstwecreatedavariablecalled"age"withthedatatypeinteger. Nextweprintedtotheconsole:"Pleaseenteryourage:" Wethenreadtheuser'svalue. Ifyouenter15youwillseethemessage:"Youarenotoldenoughtodrink". Ifyouenteranyothervaluetheprogramwilljustterminatewithoutanynotification. ThisisasimpleIfStatement.TheproblemwiththisIfStatementisthatiftheconditionevaluatestofalse (theageisnot15),theprogramwilldonothing.InthiscasewecanuseanIfElseStatement.

IfElse

Syntax IfconditionThen code Else othercode https://www thecodingguys net/tutorials/visualbasic/vb if statement

1/4

1/7/2018

VisualBasicIfStatement|thecodingguys

EndIf CommentouttheotherIfStatement(CTRL+K+C)andcopythefollowingcode: DimusernameAsString DimpasswordAsInteger username=Nothing password=Nothing Console.WriteLine("EnterYourUserName") username=Console.ReadLine() Console.WriteLine("EnterYourPassword") password=Console.ReadLine() Ifusername="Asim"Andpassword=243Then Console.WriteLine("WelcomeAsim!") Else Console.WriteLine("Accessisdenied") EndIf Console.ReadLine() ThisissimilartotheaboveIfStatement.Youhavetwovariables(string/integer)whichweusetostore thevaluestheuserenterswhenprompted.ThenwecomparethosevaluestothevaluesinourIf Statement.Soifusername="Asim"ANDpassword=243,then"WelcomeAsim"isdisplayed;otherwise "AccessDenied"isdisplayed.YouarealsousingthelogicaloperatorAndwhichwillbeexplainedlater.

NestedIfElse YoucanalsohaveanestedIfElseStatement. DimmessageAsString message="Welcome"&username Ifusername="Asim"Andpassword=243Then Console.WriteLine(message) Else Ifusername="Dave"Andpassword=12345Then Console.WriteLine(message) Else Ifusername="Admin"Andpassword=2012Then Console.WriteLine(message) Else Console.WriteLine("Oopscouldnotfindyou!") EndIf EndIf

EndIf Theprocessisthesameasbefore,butwehavemoredecisions.However,wehavemadeanewstring variablecalled"message";thiswasmadetoavoidrepeatingtheconsolemessage.TheIfElse https://www thecodingguys net/tutorials/visualbasic/vb if statement

2/4...


Similar Free PDFs