Monday, November 16, 2009

VBScript: estructuras de control de f...

VBScript: estructuras de control de flujo



Este texto es sólo una referencia, un lugar donde consultar cuando no recuerdes cómo se hacía un if, ...

El que quiera algo más trascendente: www.google.es y está lleno de sitios dónde se lo ofrecerán.


Condicional:

if ...then ...ElseIf...Else
currValue = 5
If currValue < 0 Then
  WScript.Echo “The value is less than zero.”
ElseIf currValue = 0 Then
  WScript.Echo “The value is equal to zero.”
ElseIf currValue = 1 Then
  WScript.Echo “The value is equal to one.”
ElseIf currValue = 2 Then
  WScript.Echo “The value is equal to two.”
ElseIf currValue = 3 Then
  WScript.Echo “The value is equal to three.”
ElseIf currValue = 4 Then
  WScript.Echo “The value is equal to four.”
ElseIf currValue = 5 Then
  WScript.Echo “The value is equal to five.”
Else
  WScript.Echo “Value doesn’t match expected parameters.”
End If


Select ... case

currValue = 9
Select Case currValue
Case currValue < 0
  WScript.Echo “The value is less than zero.”
Case 0
  WScript.Echo “The value is equal to zero.”
Case 1
  WScript.Echo “The value is equal to one.”
Case 2
  WScript.Echo “The value is equal to two.”
Case 3
  WScript.Echo “The value is equal to three.”
Case 4
  WScript.Echo “The value is equal to four.”
Case 5
  WScript.Echo “The value is equal to five.”
Case Else
  WScript.Echo “Value doesn’t match expected parameters.”
End Select


Uso de cadenas de texto en condiciones:


Las comparaciones entre cadenas distinguen entre mayúsculas y minúsculas. Por eso, si lo que buscamos es la presencia de una palabra o nombre lo mejor es pasarlo todo a minúsculas antes de comparar.

m = “No”
n = “NO”
If m = n Then
  WScript.Echo “Anything? Nope, I didn’t think so.”
End If
If lcase(m) = lcase(n) Then
  WScript.Echo “Values are equal when converted to lowercase.”
End If
if ucase(m) = ucase(n) Then
  WScript.Echo “Values are equal when converted to uppercase.”
End If


Bucles:

For ... Next
For i = 20 to 0 Step -1
  myArray(i) = “Unknown”
Next


For Each ... In ... Next
'Llenamos un array
Dim myArray(9)
For i = 0 to 9
  myArray(i) = “Puesto número ” & i
Next

'Lo recorremos gracias al bucle for each
For Each i IN myArray
  WScript.Echo i
Next

Exit For
For Each i IN myArray
  WScript.Echo i
  If i = “Unknown” Then
    Exit For
  EndIf
Next


Do While ... Loop
Dim y
y=0
Do While continue = True
  y = y + 1
  If y < 7 Then
    WScript.Echo “Y es menor que 7.”
  ElseIf Y = 10 Then
    WScript.Echo “Y es igual a 7.”
  Else
    WScript.Echo “Exiting the loop.”
    continue = False
  EndIf
Loop

Do ... Loop While ...
Do
  y = y + 1
  If y < 10 Then
    WScript.Echo “Y is less than 10.”
  ElseIf Y = 10 Then
    WScript.Echo “Y equals 10.”
  Else
    WScript.Echo “Exiting the loop.”
  continue = False
  EndIf
Loop While continue = True

La diferencia entre Do ... Loop While y Do While ... Loop está en el número mínimo de veces que el bucle se ejecutará. Con "Do ... Loop" el contenido del bucle se ejecuta siempre como mínimo una vez, mientras que con "Do While...Loop" puede no ejecutarse ninguna vez si la condición inicial no se cumple.

En cualquier Do... podemos salir del bucle usando: Exit Do

While ... Wend
While x < 10
  ‘Execute this code
  x = x+1
  WScript.Echo x
WEnd













No comments: