vb.net中的循环语句汇总
工具/原料
visualstudio2015
for...next循环控制
1、fornext循环语句
2、'DeclarevariableDimintCountAs诔罨租磊Integer'ClearthelistClear讣嘬铮篌List()'调用其他程序'PerformaloopForintCount=1To5'AddtheitemtothelistlstData.Items.Add("I'mitem"&intCount.ToString&"inthelist!")NextEndSubPrivateSubClearList()'ClearthelistlstData.Items.Clear()EndSub
3、执行结果如下图:
4、使用step控制步长循环间隔PrivateSubbtnForNextLoopWithStep_Click(senderAsObject,eAsEventArgs)HandlesbtnForNextLoopWithStep.Click'ClearthelistClearList()'PerformaloopForintCountAsInteger=4To62Step7'AddtheitemtothelistlstData.Items.Add(intCount.ToString)NextEndSub
5、执行结果如下图所示
6、fornext循环,步长为负,循环控制
7、PrivateSubbtnBackwardsForNextLoop_Click(senderAsObject,eAsEventArgs)HandlesbtnBackwardsForNextLoop.Click'ClearthelistClearList()'PerformaloopForintCountAsInteger=10To1Step-1'AddtheitemtothelistlstData.Items.Add(intCount.ToString)NextEndSub
8、执行结果如下图所示:
方法/步骤2
1、ForEach…Next循环语句,控制流程介绍示例代码如下:
2、PrivateSubbtnForEachLoop_Click(衡痕贤伎senderAsObject,eAsEventArgs)信咆颊辑HandlesbtnForEachLoop.Click'ClearthelistClearList()'ListeachfolderattherootofyourCdriveForEachstrFolderAsStringInMy.Computer.FileSystem.GetDirectories("C:\")'AddtheitemtothelistlstData.Items.Add(strFolder)NextEndSub
dountil...loop循环
1、dountil...loop循环,示例代码如下:
2、PrivateSubbtnDoUntilLoop_Click(衡痕贤伎senderAsObject,eAsEventArgs)信咆颊辑HandlesbtnDoUntilLoop.Click'DeclarevariablesDimobjRandomAsNewRandomDimintRandomNumberAsInteger=0'ClearthelistClearList()'ProcesstheloopuntilintRandomNumber=10DoUntilintRandomNumber=10'Getarandomnumberbetween0and24intRandomNumber=objRandom.Next(25)'AddthenumbertothelistlstData.Items.Add(intRandomNumber.ToString)LoopEndSub
3、执行结果如下图所示:
dowhile...loop循环语句
1、示例代码如下:
2、PrivateSubbtnDoWhileLoop_Click(衡痕贤伎senderAsObject,eAsEventArgs)信咆颊辑HandlesbtnDoWhileLoop.Click'DeclarevariablesDimobjRandomAsNewRandomDimintRandomNumberAsInteger=0'ClearthelistClearList()'ProcesstheloopwhileintRandomNumber<15DoWhileintRandomNumber<15'Getarandomnumberbetween0and24intRandomNumber=objRandom.Next(25)'AddthenumbertothelistlstData.Items.Add(intRandomNumber.ToString)LoopEndSub
3、执行结果如下图所示;
循环的嵌套
1、示例代码如下:
2、PrivateSubbtnNestedLoops_Click(衡痕贤伎senderAsObject,eAsEventArgs)信咆颊辑HandlesbtnNestedLoops.Click'ClearthelistClearList()'ProcessanouterloopForintOuterLoopAsInteger=1To2'Processanested(inner)loopForintInnerLoopAsInteger=1To3lstData.Items.Add(intOuterLoop.ToString&","&intInnerLoop.ToString)NextNextEndSub
3、执行结果如下图所示: