This compiler first executes the Statement
or Statements. After executing the Statement(s)
section, the compiler checks the Condition. If the Condition
is true, then the compiler returns to the Statement(s) and execute(s)
it(them). The compiler keeps doing this check-execution gymnastic. AS LONG AS the Condition is true,
the Statement(s) section will be executed and the
Condition will be tested again. If the Condition is false or once the condition becomes false, the statement will not be
executed and the the program will move on. Here is an example:

Are we there yet (1=Yes/0=No)? 2
Are we there yet (1=Yes/0=No)? g
Are we there yet (1=Yes/0=No)? 7
Are we there yet (1=Yes/0=No)? 3
Are we there yet (1=Yes/0=No)? k
Are we there yet (1=Yes/0=No)? 1
Wonderful, we have arrived

As you may guess already,
the Condition must provide a way for it to be true and to be false.
Otherwise, the looping would be executed over and over again.

Once again, the Statement(s) section
executes first. After executing the Statement(s), the
compiler checks the Condition. If the Condition is
true, the compiler returns to the Statement(s) section to
execute it. This will continue until the Condition is false.
Once the Condition becomes false, the compiler gets out of
this loop and continues with the section under the Loop Until
line.

As mentioned above, the Do While... Loop
expression executes a statement first before checking a condition
that would allow it to repeat. If you want to check a condition
first before executing a statement, you can use another version as Do
While... Loop. Its formula is:

Do WhileConditionStatement(s)Loop

In this case, the compiler checks the Condition first. If
the Condition is true, the compiler then executes the Statement(s)
and checks the Condition again. If the
Condition is false, or when the Condition becomes
false, the compiler skips the Statement(s)
section and continues the code below the Loop keyword.

Here is an example:

Inports System
Module Module1
Sub Main()
Dim Number As Short
Do While Number < 46
Console.Write("{0} ", Number)
Number += 4
Loop
Console.WriteLine()
Console.WriteLine("Counting Stopped at: {0}", Number)
Console.WriteLine()
End Sub
End Module

This would produce:

0 4 8 12 16 20 24 28 32 36 40 44
Counting Stopped at: 48

The Do Until... Loop Statement

An alternative to the Do While... Loop loop
uses the following formula:

Do UntilConditionStatement(s)Loop

This loop works like the Do While... Loop
expression. The compiler examines the Condition first. If the
condition is true, then it executes the Statement(s) section.

The looping statements we reviewed above are used
when you don't know or can't anticipate the number of times a
condition needs to be checked in order to execute a statement. If you know
with certainty how many times you want to execute a statement, you can use
another form of loops that use the For...Next expression.

The For...To...Next Loop

One of the loop counters you can use is For...To...Next.
Its formula is:

ForCounter=StartToEndStatement(s)Next

Used for counting, the expression begins counting at
the Start point. Then it examines whether the current value (after
starting to count) is lower than End. If that's the case,
it then executes the Statement(s). Next, it increments the value of
Counter by 1 and
examines the condition again. This process goes on until the value
of Counter becomes equal to the End value. Once this
condition is reached, the looping stops.

Here is an example:

Inports System
Module Module1
Sub Main()
Dim Number As Short
For Number = 5 To 16
Console.Write("{0} ", Number)
Next
Console.WriteLine()
Console.WriteLine("Counting Stopped at: {0}", Number)
Console.WriteLine()
End Sub
End Module

This would produce:

5 6 7 8 9 10 11 12 13 14 15 16
Counting Stopped at: 17

Stepping the Counting Loop

The formula above will increment the counting by
1 at the end of each statement. If you want to control how the
incrementing processes, you can set your own, using the Step option.
Here is the formula:

ForCounter=StartToEndStepIncrementStatement(s)Next

You can set the incrementing value to your
choice. If the value of Increment is positive, the Counter will be
added its value. Here is an example:

Inports System
Module Module1
Sub Main()
Dim Number As Short
For Number = 5 To 42 Step 4
Console.Write("{0} ", Number)
Next
Console.WriteLine()
End Sub
End Module

This would produce:

5 9 13 17 21 25 29 33 37 41

You can also set a negative value to the Increment
factor, in
which case the Counter will be subtracted the set value.

Techniques of Writing Conditional Statements

Conditional Nesting

A condition can be created inside of another to write a more effective statement. This is referred to as nesting
one condition inside of another. Almost any condition can be part of another and
multiple conditions can be included inside of others.

As we have learned, different conditional statements are applied in specific circumstances. In some situations, they are interchangeable or one can be applied just like another, which becomes a matter of choice. Statements can be combined to render a better result with each playing an appropriate role.

Here is an example of an if condition nested inside
of a do...while loop:

Imports System
Module Module1
Sub Main()
Dim SittingDown As Char
Dim SitDown As String
Do
Console.Write("Are you sitting down now(y/n)? ")
SitDown = Console.ReadLine()
SittingDown = CChar(SitDown)
If SittingDown <> "y" Then
Console.WriteLine("Could you please sit down for the next exercise? ")
End If
Loop While Not (SittingDown = "y")
Console.WriteLine()
End Sub
End Module

Here is an example of running the program:

Are you sitting down now(y/n)? n
Could you please sit down for the next exercise?
Are you sitting down now(y/n)? a
Could you please sit down for the next exercise?
Are you sitting down now(y/n)? k
Could you please sit down for the next exercise?
Are you sitting down now(y/n)? y

One of the reasons you would need to nest conditions is because one would lead to another. Sometimes, before checking one condition, another primary condition would have to be met.
Our ergonomic program asks the user whether she is sitting down. Once the user is sitting down, you would write an exercise she would perform. Depending on her strength, at a certain time, one user will be tired and want to stop while for the same amount of previous exercises, another user would like to continue. Before continuing with a subsequent exercise, you may want to check whether the user would like to continue.
Of course, this would be easily done with:

using System;
Module Module1
Sub Main()
Dim SittingDown As Char
Dim SitDown As String
Dim WantToContinue As String
Do
Console.Write("Are you sitting down now(y/n)? ")
SitDown = Console.ReadLine()
SittingDown = CChar(SitDown)
If SittingDown <> "y" Then
Console.WriteLine("Could you please sit down for the next exercise? ")
End If
Loop While Not (SittingDown = "y")
Console.WriteLine()
Console.Write("Do you want to continue(y=Yes/n=No)? ")
Dim ToContinue As String = Console.ReadLine()
WantToContinue = CChar(ToContinue)
Console.WriteLine()
End Sub
End Module

If the user answers No, you can stop the program. If she answers Yes, you would need to continue the program with another exercise. Because the user answered Yes, the subsequent exercise would be included in the previous condition because it does not apply for a user who wants to stop. In this case, one “if” could be inserted inside of another. Here is an example:

Imports System
Module Module1
Sub Main()
Dim SittingDown As Char
Dim SitDown As String
Dim WantToContinue As String
Do
Console.Write("Are you sitting down now(y/n)? ")
SitDown = Console.ReadLine()
SittingDown = CChar(SitDown)
If SittingDown <> "y" Then
Console.WriteLine("Could you please sit down for the next exercise? ")
End If
Loop While Not (SittingDown = "y")
Console.WriteLine()
Console.Write("Do you want to continue(1=Yes/0=No)? ")
WantToContinue = CChar(Console.ReadLine())
If WantToContinue = "1" Then
Dim LayOnBack As Char
Console.WriteLine("Good!" & vbCrLf & "For the next exercise, you should lay on your back")
Console.Write("Are you laying on your back(1=Yes/0=No)? ")
Dim lay As String = Console.ReadLine()
LayOnBack = CChar(Lay)
If LayOnBack = "1" Then
Console.WriteLine("Great!" & vbCrLf & "Now we will start the next exercise.")
Else
Console.WriteLine("Well, it looks like you are getting tired...")
End If
else
Console.WriteLine("We had enough today")
End If
Console.WriteLine("We will stop the session now." & vbCrLf & "Thanks.")
Console.WriteLine()
End Sub
End Module

Imagine you are in the middle of a Main procedure (or
another of the procedure we will learn to create in the new few lessons).
If a certain condition is met, you may need to get out of the procedure to
condition the application (out of the current procedure). To do this, the
Visual Basic language provides the Exit Sub expression. To use it, you
simply type Exit Sub once the undesired condition is met. Consider the
following example:

Imports System
Module Module1
Sub Main()
Dim i As Integer
For i = 5 To 18 Step 1
If i = 12 Then
Exit Sub
End If
Console.WriteLine("Value: {0}", i)
Next
End Sub
End Module

In this case, the compiler starts counting at 5 and displays
the corresponding value on the console. If the counter reaches 12, it gets out
of Main, which causes it to stop. Therefore, this would produce:

Value: 5
Value: 6
Value: 7
Value: 8
Value: 9
Value: 10
Value: 11

The Goto Statement

The Goto statement allows a program execution to jump to another section of
a procedure in which it is being used.
In order to use the Goto statement, insert a name on a particular section of your
procedure so you can refer to that name. The name, also called a label, is made of one word and follows the rules we have learned about
names (the name can be anything), then followed by a colon ":".

The following program uses a for loop to count from 2 to 18, but when it encounters
10, it jumps to a designated section of the program:

Imports System
Module Module1
Sub Main()
Dim i As Integer
For i = 2 To 18 Step 1
If i = 10 Then
GoTo StoppingHere
End If
Console.WriteLine("Value: {0}", i)
Next
StoppingHere:
Console.WriteLine("The execution jumped here.")
End Sub
End Module

-=- Multiple Choice Question -=-
To create a constant in your code, you can use the Constant keyword
Your choice (1=True/2=False)? 1
Wrong: The keyword used to create a constant is Const

Here is another example of executing the same program
with Answer = 2:

-=- Multiple Choice Question -=-
To create a constant in your code, you can use the Constant keyword
Your choice (1=True/2=False)? 2
Right: Constant is not a keyword

Conditional Conjunctions and Disjunctions

Introduction

A logical operation is one that is performed on one or
two expressions to check the truthfulness or falsity. The comparison is
performed using one of three special keywords Not, And, or Or.

Logical Not

When a variable is declared and receives a value (this
could be done through initialization or a change of value) in a program,
it becomes alive. When a variable is not being used or is not available
for processing (in visual programming, it would be considered as disabled)
to make a variable (temporarily) unusable, you can nullify its value. To
render a variable unavailable during the evolution of a program, apply the
logical not operator which is Not. Its syntax is:

NotValue

There are two main ways you can use the logical Not
operator. The most
classic way of using the logical Not operator is to check the state
of a variable.

When a variable holds a value, it is
"alive". To make it not available, you can "not" it.
When a variable has been "notted", its logical value has
changed. If the logical value was True, it would be changed to False
and vice versa. Therefore, you can inverse the logical value of a variable
by "notting" or not "notting" it.

A logical conjunction is an operation used to check
two conditions for absolute truthfulness. This operation uses the And
keyword.
The formula to use the And operator is

Condition1AndCondition2

The left condition, Condition1, is first
checked. If it is False, the whole expression is rendered False and the
checking process stopped. If the first condition, Condition1, is True, then the
second condition, Condition2, is checked. If the right condition is
False, the
whole expression is False, even if the first is True. In the same way, if
both conditions are false, the whole expression is False. Only if both
conditions are True is the whole condition true. This can be resumed as
follows:

Condition1

Condition2

Condition1 And Condition2

False

Don't Care

False

Don't Care

False

False

True

True

True

Here is an example:

Module Module1
Sub Main()
Dim SittingDown As Char
Dim SitDown As String
Dim WantToContinue As String
Console.Write("Are you sitting down now(y/n)? ")
SitDown = Console.ReadLine()
SittingDown = CChar(SitDown)
If (SittingDown <> "y") And (SittingDown <> "Y") Then
Console.WriteLine("Could you please sit down for the next exercise? ")
End If
Console.WriteLine()
End Sub
End Module

Here is one example of running the program:

Are you sitting down now(y/n)? n
Could you please sit down for the next exercise?
Are you sitting down now(y/n)? N
Could you please sit down for the next exercise?
Are you sitting down now(y/n)? d
Could you please sit down for the next exercise?
Are you sitting down now(y/n)? y
Good
We will stop the session now.
Thanks.

Logical Disjunction: OR

A logical disjunction is performed on two conditions
for a single truthfulness. This operation uses the Or keyword on the
following formula:

Condition1 OR Condition2

The left condition, Condition1, is first
checked. If Condition1 is True, then the whole expression is true,
regardless of the outcome of the second. If Condition1 is False,
then Condition2 is checked. If Condition2 is True, the whole
expression is True even if Condition1 was False. If
both conditions are True, the whole expression is True. The whole
expression is false only if both Condition1 and Condition2
are False. This can be resumed as follows:

Condition1

Condition2

Condition1 Or Condition2

True

Don't Care

True

Don't Care

True

True

True

True

True

False

False

False

Here is an example:

Module Module1
Sub Main()
Dim SittingDown As Char
Dim SitDown As String
Dim WantToContinue As String
Console.Write("Are you sitting down now(y/n)? ")
SitDown = Console.ReadLine()
SittingDown = CChar(SitDown)
If (SittingDown = "n") Or (SittingDown = "N") Then
Console.WriteLine("Could you please sit down for the next exercise? ")
End If
Console.WriteLine()
End Sub
End Module