Bucles (Ciclos) Visual Basic_ for Next Step, Do While Loop, Until
-
Upload
juan-hernandez -
Category
Documents
-
view
7 -
download
0
description
Transcript of Bucles (Ciclos) Visual Basic_ for Next Step, Do While Loop, Until
-
4/4/2015 Bucles(ciclos)VisualBasic:ForNextStep,DoWhileLoop,Until.Ejemplosyejerciciosresueltos(CU00326A)
http://aprenderaprogramar.com/index.php?option=com_content&view=article&id=263:buclesciclosvisualbasicfornextstepdowhileloopuntilejempl 1/7
Cursos CursoAprenderaprogramarenVisualBasicdesdecero
Bucles(ciclos)VisualBasic:ForNextStep,DoWhileLoop,Until.Ejemplosyejerciciosresueltos(CU00326A)
EscritoporMarioR.Rancel
Resumen:Entregan25delcursoAprenderaprogramarenVisualBasicdesdecero.
Codificacinaprenderaprogramar.com:CU00326A
ESTRUCTURASDEREPETICIN(BUCLES)CONVISUALBASIC
Vamos a ver cmo materializar con Visual Basic estructuras de repeticin que permitirn que en nuestros programas se realice unprocesonveces.Enconcretoveremos las instruccionesDesdeSiguiente (ForNext)consuclusulaPaso (Step), la instruccinMientrasHacer(DoWhileLoop)ylainstruccinHacerRepetirMientras(DoLoopWhile).
Muchasvecespodremosoptarindistintamenteporusarunainstruccinuotra.Enotroscasos,esrecomendabledecantarseporunadeellasporhacerelprogramamslegibleosencilloqueusandootrasopciones.
Inicio Cmoempezar Libros Cursos Empleo Humor!!! Divulgacin
Concenos Foros Freelancers Centrosformativos
-
4/4/2015 Bucles(ciclos)VisualBasic:ForNextStep,DoWhileLoop,Until.Ejemplosyejerciciosresueltos(CU00326A)
http://aprenderaprogramar.com/index.php?option=com_content&view=article&id=263:buclesciclosvisualbasicfornextstepdowhileloopuntilejempl 2/7
INSTRUCCINDESDE...SIGUIENTE(FOR...NEXT)YCLUSULAPASO(STEP)
LasintaxisaemplearconVisualBasiceslasiguiente:
ForVAR=ViToVfInstruccin1Instruccin2
InstruccinnNextVAR
Elincrementoquesufreelcontadores,pordefecto,unitario.Esdecir,elprimervalorquetomaVARenelbucleserVi,elsegundoVi+1,eltercero(Vi+1)+1,etc.LamodificacindedichovalordeincrementolarealizaremosatravsdelaclusulaStepdespusdelvalorVf.Step2implicarqueencadarepeticindelbucleelcontadorseincrementeendosunidades,Step5implicarqueencadarepeticindelbucleelcontadorseincrementeencincounidades.UnpasonegativodeltipoStep1suponequeelcontadordecreceenvezdeincrementarse.Sielpasoesnegativo,VinecesariamentehabrdesermayorqueVf,yaqueencasocontrarionoseproducirlaentradaenelbucle.
Conestecdigosenosmuestraenpantalla3veceshola(sehainvertidoelsentidodelbucle):
Cdigo(versionesVBmenosrecientes) Cdigo(versionesVBmsrecientes)
RemCursoVisualBasicaprenderaprogramar.comOptionExplicit
DimVARAsIntegerDimViAsIntegerDimVfAsIntegerPrivateSubForm_Load()Vi=1Vf=3ForVAR=VfToViStep1'[TambinsupondratresrepeticionesForVAR=VitoVf]MsgBox("hola")NextVAREndSub
REMCursoVisualBasicaprenderaprogramar.comOptionExplicitOn
PublicClassForm1PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.LoadDimVARAsIntegerDimViAsIntegerDimVfAsIntegerVi=1Vf=3ForVAR=VfToViStep1'[TambinsupondratresrepeticionesForVAR=VitoVf]MsgBox("hola")NextVAREndSubEndClass
ConVisualBasicresultaadmisibleusarNextsinindicarlavariablequeestsirviendodeguadelbucle,puestodobuclehadeteneruncierre.Sinembargo,nolocreemosrecomendablepuespuededificultarlalecturaydepuracindelosprogramas.
-
4/4/2015 Bucles(ciclos)VisualBasic:ForNextStep,DoWhileLoop,Until.Ejemplosyejerciciosresueltos(CU00326A)
http://aprenderaprogramar.com/index.php?option=com_content&view=article&id=263:buclesciclosvisualbasicfornextstepdowhileloopuntilejempl 3/7
LainstruccinFor...Nextesanidabledentrodesmismaodentrodeotrostiposdebuclesoestructuras,debiendoseguirlaspautasqueyahemoscomentado.
EJERCICIO
Transformarencdigoelsiguienteplanteamiento:queremosgenerarunprogramaquepidaunnmerodel1al10ynosmuestre latablademultiplicarcorrespondiente.
SOLUCIN
Hemos realizado la pregunta relativa al nmero cuya tabla sedesea conocer utilizandoun InputBox. Igualmente vlidohubiera sidohacerloconunLabelcontenedordelapreguntayunTextBoxdondeelusuariointroducelainformacin.LatablalamostramossobreunLabeldenominadoLabelTabla.
Cdigo(versionesVBmenosrecientes) Cdigo(versionesVBmsrecientes)
RemCursoVisualBasicaprenderaprogramar.comOptionExplicitDimTAsIntegerDimiAsIntegerPrivateSubForm_Load()Form1.Caption="Tabla"T=Val(InputBox("Qutablaquiereconocer?"&vbCrLf&_"(Introduzcaunnmerode1a10)","Nmero?"))LabelTabla.FontBold=TrueLabelTabla.Alignment=2LabelTabla="TABLADEL"&T&vbCrLf&vbCrLf
Fori=1To10LabelTabla=LabelTabla&T&"*"&i&"="&T*i&vbCrLfNextiEndSub
REMCursoVisualBasicaprenderaprogramar.comOptionExplicitOn
PublicClassForm1DimTAsIntegerDimiAsInteger
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.LoadMe.Text="Tabla"T=Val(InputBox("Qutablaquiereconocer?"&vbCrLf&_"(Introduzcaunnmerode1a10)","Nmero?"))LabelTabla.Font=NewFont("Arial",10,FontStyle.Bold)LabelTabla.TextAlign=ContentAlignment.MiddleCenterLabelTabla.Text="TABLADEL"&T&vbCrLf&vbCrLfFori=1To10LabelTabla.Text=LabelTabla.Text&T&"*"&i&"="&T*i&vbCrLfNextiEndSubEndClass
Aspectogrfico:
-
4/4/2015 Bucles(ciclos)VisualBasic:ForNextStep,DoWhileLoop,Until.Ejemplosyejerciciosresueltos(CU00326A)
http://aprenderaprogramar.com/index.php?option=com_content&view=article&id=263:buclesciclosvisualbasicfornextstepdowhileloopuntilejempl 4/7
EJERCICIO
Transformarencdigoelsiguienteplanteamiento:queremosgenerarunprogramaquemuestretodoslosenteroscomprendidosentreunnmerodefinidoporelusuarioy100.
SOLUCIN
Cdigo(versionesVBmenosrecientes) Cdigo(versionesVBmsrecientes)
RemCursoVisualBasicaprenderaprogramar.comOptionExplicit
DimNumAsIntegerDimiAsInteger
PrivateSubForm_Load()Form1.Caption="Enteros"Num=Val(InputBox("Introduzcaunnmeroenterocomprendidoentre1y99","Nmero?"))LabelTabla.Alignment=2LabelTabla.FontBold=TrueLabelTabla="ENTEROSENTRE"&Num&"y100"&vbCrLf&vbCrLfFori=NumTo100LabelTabla=LabelTabla&i&""Nexti
REMCursoVisualBasicaprenderaprogramar.comOptionExplicitOnPublicClassForm1
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.LoadDimNumAsIntegerDimiAsIntegerMe.Text="Enteros"Num=Val(InputBox("Introduzcaunnmeroenterocomprendidoentre1y99","Nmero?"))LabelTabla.TextAlign=ContentAlignment.MiddleCenterLabelTabla.Font=NewFont("Arial",10,FontStyle.Bold)LabelTabla.Text="ENTEROSENTRE"&Num&"y100"&vbCrLf&vbCrLfFori=NumTo100LabelTabla.Text=LabelTabla.Text&i&""Nexti
-
4/4/2015 Bucles(ciclos)VisualBasic:ForNextStep,DoWhileLoop,Until.Ejemplosyejerciciosresueltos(CU00326A)
http://aprenderaprogramar.com/index.php?option=com_content&view=article&id=263:buclesciclosvisualbasicfornextstepdowhileloopuntilejempl 5/7
EndSub EndSubEndClass
Aspectogrfico:
INSTRUCCINMIENTRAS...HACER(DOWHILE...LOOP)
Lasintaxisquehemosdeseguireslasiguiente:
DoWhile[condicin]Instruccin1Instruccin2...Instruccinn
Loop
-
4/4/2015 Bucles(ciclos)VisualBasic:ForNextStep,DoWhileLoop,Until.Ejemplosyejerciciosresueltos(CU00326A)
http://aprenderaprogramar.com/index.php?option=com_content&view=article&id=263:buclesciclosvisualbasicfornextstepdowhileloopuntilejempl 6/7
LasinstruccionestipoDoWhilesonanidablesdentrodesmismasodentrodeotrasestructuras.Esimportanteverificarquelosbuclesdiseadosconestainstruccindispongandeunacondicindesalidavlida.
Ejemplo:k=0:Dowhilek=3,yLoopUntili>=3seraequivalenteaLoopWhilei
-
4/4/2015 Bucles(ciclos)VisualBasic:ForNextStep,DoWhileLoop,Until.Ejemplosyejerciciosresueltos(CU00326A)
http://aprenderaprogramar.com/index.php?option=com_content&view=article&id=263:buclesciclosvisualbasicfornextstepdowhileloopuntilejempl 7/7
Paraaccederalainformacingeneralsobreestecursoyallistadocompletodeentregaspulsaenestelink:Vercursocompleto.Parahaceruncomentariooconsultautilizalosforosaprenderaprogramar.com,abiertosacualquierpersonaindependientementedesuniveldeconocimiento.
Bajararchivo...
Archivo Informacinadicional: Tamaodearchivo
CU00326ABuclesfornextstepdowhilelooploopwhileVisualBasicejemplo.pdf
Formatopdf 124Kb
Copyright20062015aprenderaprogramar.comLawebabiertaacualquierpersonainteresadaenlaprogramacin