- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Flow control
In this part of the Visual Basic tutorial, we will talk about the flow control. We will define several keywords that enable us to control the flow of the Visual Basic program.
In Visual Basic language there are several keywords that are used to alter the flow of the program. When the program is run, the statements are executed from the top of the source file to the bottom. One by one. This flow can be altered by specific keywords. Statements can be executed multiple times. Some statements are called conditional statements. They are executed only if a specific condition is met.
The If statement
The If
statement has the following general form:
If (expression) statement End If
The If
keyword is used to check if an expression is true. If it is true, a statement is then executed. The statement can be a single statement or a compound statement. A compound statement consists of multiple statements enclosed by the If/End If block.
Option Strict On Module Example Dim num As Byte = 31 Sub Main() If (num > 0) Console.WriteLine("num variable is positive") End If End Sub End Module
We have a num
variable. It is assigned 31. The If
keyword checks for a boolean expression. The expression is put between square brackets. The 31 > 0
is true, so the statement inside the block is executed.
$ ./ifstatement.exe num variable is positive
The condition is met and the message is written to the console.
Option Strict On Module Example Dim num As Byte = 31 Sub Main() If (num > 0) Console.WriteLine("num variable is positive") Console.WriteLine("num variable equals {0}", num) End If End Sub End Module
More statements can be executed inside the block, created by the If
, End If
keywords.
We can use the Else
keyword to create a simple branch. If the expression inside the square brackets following the If
keyword evaluates to false, the statement following the Else
keyword is automatically executed.
Option Strict On Module Example Dim sex As String Sub Main() sex = "female" If (sex = "male") Console.WriteLine("It is a boy") Else Console.WriteLine("It is a girl") End If End Sub End Module
We have a sex variable. It has "female" string. The boolean expression evaluates to false and we get "It is a girl" in the console.
$ ./branch.exe It is a girl
We can create multiple branches using the Else If
keyword. The Else If
keyword tests for another condition if and only if the previous condition was not met. Note that we can use multiple Else If
keywords in our tests.
Option Strict On Module Example Dim a As Byte = 0 Sub Main() If (a < 0) Console.WriteLine("a is negative") Else If (a = 0) Console.WriteLine("a equals to zero") Else Console.WriteLine("a is a positive number") End If End Sub End Module
We have a numerical variable and we test it if it is a negative number or positive or if it equals to zero. The first expression evaluates to false. The second condition is met. The program prints 'a equals to zero' to the console. The rest of the branch is skipped.
Select statement
The Select
statement is a selection control flow statement. It allows the value of a variable or expression to control the flow of program execution via a multi way branch. It creates multiple branches in a simpler way than using the combination of If
, Else If
statements.
We have a variable or an expression. The Select
keyword is used to test a value from the variable or the expression against a list of values. The list of values is presented with the Case
keyword. If the values match, the statement following the Case
is executed. There is an optional Case Else
statement. It is executed if no other match is found.
Option Strict On Module Example Dim domain As String Sub Main() domain = Console.ReadLine() Select domain Case "us" Console.WriteLine("United States") Case "de" Console.WriteLine("Germany") Case "sk" Console.WriteLine("Slovakia") Case "hu" Console.WriteLine("Hungary") Case Else Console.WriteLine("Unknown") End Select End Sub End Module
In our program, we have a domain variable. We read a value for the variable from the command line. We use the Case
statement to test for the value of the variable. There are several options. If the value equals for example to "us" the "United States" string is printed to the console.
$ ./selectcase.exe hu Hungary
We have entered "hu" string to the console and the program responded with "Hungary".
The Select
keyword enables to validate a range of numerical cases.
Option Strict On Module Example Dim age As Byte Sub Main() Try age = Console.ReadLine() Catch Console.WriteLine("Invalid value") End End Try Select age Case 0 To 21 Console.WriteLine("Junior") Case 22 To 60 Console.WriteLine("Adult") Case Else Console.WriteLine("Senior") End Select End Sub End Module
The preceding program uses range of numerical values to identify an age group of a person.
Try age = Console.ReadLine() Catch Console.WriteLine("Invalid value") End End Try
A value is read from the console. We can use only numerical data. The Try
, Catch
, End Try
keywords are used for exception handling. If an exception is thrown, the statements following the Catch
keyword are executed. The End
statement terminates the program.
Case 0 To 21 Console.WriteLine("Junior")
Here we specify a range of values. If the value entered by the user is in between 0 and 21, inclusive, then the program prints "Junior" to the console.
$ ./agerange.exe 43 Adult
We have entered 43 and the program responded with the "Adult" string.
The While statement
The While
statement is a control flow statement that allows code to be executed repeatedly based on a given boolean condition.
This is the general form of the While
loop:
While (expression): statement End While
The While
keyword executes the statements inside the block enclosed by the While
, End While
keywords. The statements are executed each time the expression is evaluated to true.
Option Strict On Module Example Sub Main() Dim i As Integer = 0 Dim sum As Integer = 0 While i < 10 i = i + 1 sum += i End While Console.WriteLine(sum) End Sub End Module
In the code example, calculate the sum of values from a range of numbers.
The While
loop has three parts: initialization, testing, and updating. Each execution of the statement is called a cycle.
Dim i As Integer = 0
We initiate the i variable. It is used as a counter.
While i < 10 ... End While
The expression following the While
keyword is the second phase, the testing. The statements in the body are executed, until the expression is evaluated to false.
i = i + 1
The last, third phase of the While
loop. The updating. We increment the counter. Note that improper handling of the While
loops may lead to endless cycles.
It is possible to run the statement at least once. Even if the condition is not met. For this, we can use the Do
, Loop While
keywords.
Option Strict On Module Example Sub Main() Dim count As Integer = 0 Do Console.WriteLine(count) Loop While (count <> 0) End Sub End Module
First the iteration is executed and then the truth expression is evaluated.
The For Next statements
When the number of cycles is know before the loop is initiated, we can use the For Next
statements. In this construct we declare a counter variable, which is automatically increased or decreased in value during each repetition of the loop.
Option Strict On Module Example Sub Main() For i As Integer = 0 To 9 Console.WriteLine(i) Next End Sub End Module
In this example, we print numbers 0..9 to the console.
For i As Integer = 0 To 9 Console.WriteLine(i) Next
We initiate the counter i to zero. The Next
statement increases the counter by one until the counter equals to 9.
Visual Basic has an optional Step
keyword. It controls how the counter variable is going to be increased or decreased.
Option Strict On Module Example Sub Main() For i As Integer = 9 To 0 Step -1 Console.WriteLine(i) Next End Sub End Module
In the above example, we print numbers 0..9 in the reverse order.
For i As Integer = 9 To 0 Step -1 Console.WriteLine(i) Next
The step may be a negative number too. We initiate the counter to 9. Each iteration the counter is decreased by the step value.
The For Each statement
The For Each
construct simplifies traversing over collections of data. It has no explicit counter. The For Each
statement goes through the array or collection one by one and the current value is copied to a variable defined in the construct.
Option Strict On Module Example Sub Main() Dim planets() As String = { "Mercury", "Venus", _ "Earth", "Mars", "Jupiter", "Saturn", _ "Uranus", "Neptune" } For Each planet As String In planets Console.WriteLine(planet) Next End Sub End Module
In this example, we use the For Each
statement to go through an array of planets.
For Each planet As String In planets Console.WriteLine(planet) Next
The usage of the For Each
statement is straightforward. The planets
is the array that we iterate through. The planet
is a temporary variable that has the current value from the array. The For Each
statement goes through all the planets and prints them to the console.
$ ./planets.exe Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune
Running the above Visual Basic program gives this output.
The Exit, Continue statements
The Exit
statement can be used to terminate block defined by While
, For
or Select
statements.
Option Strict On Module Example Dim val As Integer Sub Main While (True) val = CType((30 * Rnd), Integer) + 1 Console.Write(val.ToString & " ") If (val = 22) Exit While End If End While Console.Write(vbNewLine) End Sub End Module
We define an endless While
loop. There is only one way to jump out of a such loop. We must use the Exit While
statement. We choose a random value from 1 to 30. We print the value. If the value equals to 22, we finish the endless while loop.
$ ./exitstm.exe 30 12 13 20 19 4 2 9 6 9 22
We might get something like this.
The Continue
statement is used to skip a part of the loop and continue with the next iteration of the loop. It can be used in combination with Do
, For
and While
statements.
In the following example, we will print a list of numbers that cannot be divided by 2 without a remainder.
Option Strict On Module Example Dim num As Integer = 0 Sub Main() While (num < 1000) num = num + 1 If ((num Mod 2) = 0) Continue While End If Console.Write(num.ToString() + " ") End While Console.Write(vbNewLine) End Sub End Module
We iterate through numbers 1..999 with the While
loop.
If ((num Mod 2) = 0) Continue While End If
If the expression num Mod 2
returns 0, the number in question can be divided by 2. The Continue
statement is executed and the rest of the cycle is skipped. In our case, the last statement of the loop is skipped and the number is not printed to the console. The next iteration is started.
In this part of the Visual Basic tutorial, we were talking about control flow structures.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论