ItsMods

Full Version: [TUTORIAL] Various Statements
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Statements

If...Then...Else - Do { While | Until } - For Each...Next - For...Next

If...Then...Else
"Conditionally executes a group of statements, depending on the value of an expression."

Now let us say that you have more than one variable (Which 99.99 % of the time, you are going to), and you want to see what that variable is equal to or maybe compare it to another variable. This would be accomplished by using an If statement. First let's declare our variables...


Code:
Dim str As String = "123"
Dim anotherStr As String = "1234"

Next let's have a brief overview of the If statement.

This is how it will look (without all the brackets):

Code:
If condition [ Then ]
   [ statements  ]
[ ElseIf elseifcondition [ Then ]
   [ elseifstatements ] ]
[ Else
   [ elsestatements ] ]
End If

As you can tell the If statement contains 2 other things within it. These are Else and ElseIf, then of course you have the End If which ends the If statement.

This is how you would compare the 2 previous variables that we declared:

Code:
If str = anotherStr Then
MsgBox("Holy shit! They're the same!")
Else
Application.Exit()
End If

In this case since str = 123 and anoterStr = 1234 it would not pop up the MsgBox, it would close the application because the strings are not the same.

Now, in this case, we really only need Else, but say that you wanted to see if str = "abc" and if it did it would execute something instead of Application.Exit()...that is when ElseIf comes into play. This is how we could to that:

Code:
If str = anotherStr Then
MsgBox("Holy shit! They're the same!")
ElseIf str = "abc" Then
MsgBox("OMFG str = abc.")
Else
Application.Exit()
End If

here is the thing when using If statements though. If the initial check is true it will not check the ElseIf or execute the Else.




Do { While | Until }...
"Repeats a block of statements while a Boolean condition is True or until the condition becomes True."

This is how one of these statements would look:
Do { While | Until } condition
[ statements ]
[ Exit Do ]
[ statements ]
Loop


-or-

Do
[ statements ]
[ Exit Do ]
[ statements ]
Loop { While | Until } condition


The break down
  • While
    • This is required unless you use Until. While will repeat the Loop until the condition that you supplied becomes False.
  • Until
    • This is required unless you use While. Until will repeat the Loop until the condition that you supplied becomes True.
  • condition
    • This is an optional Boolean variable that will be used to evaluate the expression for True or False.
  • statements
    • This is also optional and would be one or more statements that would repeat while or until the condition becomes True or False.

Optional Remarks for the Do { Until | While } statement
Courtesy or MSDN.
The Exit Do statement transfers control immediately to the statement following the Loop statement. Any number of Exit Do statements can be placed anywhere in the Do loop. Exit Do is often used after evaluating some condition, for example with If...Then...Else.



For Each...Next
"Repeats a group of statements for each element in a collection."

This is how one of these statements would look:
For Each element [ As datatype ] In group
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]


The break down
  • element
    • Any type of variable that is used to iterate, or, loop through the elements of the collection that you supplied. Please note: the datatype of element must be an element that the data type of the elements in the group that you supplied can be converted too. Basically you cannot use a String for something that returns an Integer.
  • datatype
    • Required if element is not declared. If element is declared you cannot re-declare it using the As clause.
  • group
    • This is required and should be an Object variable that must refer to an object collection or an array.
  • statements
    • You can optionally add in various statements that will be executed while the loop is in progress. This will be executed for each element in the group.

Optional remarks for For Each...Next
Courtesy of MSDN
If element has not been declared outside this loop, you can declare it within the For Each statement. In this case, the scope of element is the body of the loop. However, you cannot declare element both outside and inside the loop.

The For Each...Next loop is entered if there is at least one element in group. Once the loop has been entered, the statements are executed for the first element in group; if there are more element(s) in group, the statements in the loop continue to execute for each element (That's why this os called a For Each loop.). When there are no more element(s), the loop is terminated and execution continues with the statement following the Next statement.

Any number of Exit For statements may be placed anywhere in the loop as an alternative way to exit. Exit For is often used after evaluating some condition, for example with an If...Then...Else statement, and transfers control to the statement immediately following Next.

You can nest For Each...Next loops by placing one loop within another. Each loop must have a unique element variable.



For...Next
"Repeats a group of statements a specified number of times."

This is how one of these statements would look:
For counter [ As datatype ] = start To end [ Step step ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]


The break down
  • counter
    • This expression is required. The counter variable has to be a numeric datatype that supports the greater-than or equal to (>=), less-than or equal to (<=), and the addition (+) operators.
  • datatype
    • This is required if counter is not already declared. This expression is the datatype of counter. (Ex: Integer) If datatype is already declared then you cannot use the As clause to re-declare it.
  • start
    • This expression is required and is the initial value of counter. The start expression is usually an Integer but can be any data type as long as it widens to the type of counter.
  • end
    • This expression is required and is the final value of counter. The end expression is usually an Integer but can be any data type as long as it widens to the type of counter.
  • step
    • This expression is optional. step would be the value to increase the counter by each time it passes through the loop. The step expression is usually an Integer but can be any data type as long as it widens to the type of counter. If step's value is not declared; the default is one (1).
  • statements
    • You can optionally add in various statements that will be executed while the loop is in progress. These statements will be executed a specific number of times.

Optional remarks for the For...Next statement
Courtesy of MSDN
If counter has not been declared outside this loop, you can declare it within the For statement. In this case, the scope of counter is the body of the loop. However, you cannot declare counter both outside and inside the loop.

The step argument can be either positive or negative. The value of the step argument determines loop processing as follows:
  • Step value
    • Positive (1 and up) or Zero (0)
      • Loop executes if counter <= end.
    • Negative (0 and down)
      • Loop executes if counter >= end

Credits:
SomeWhiteGuy - Original Post
COOL AND USEFUL BUT
a.)why do repost somewhiteguy
b.)http://www.youtube.com/playlist?list=PL42055376AE25291E is all you need
Code:
If str = anotherStr Then

Shouldn't be
Code:
If str == anotherStr Then

?