Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

Introduction to Visual Basic

Arrays

Volgend Onderwerp

Boolean Datatype and Expressions

Vorig Onderwerp

Selections

|

Logical Operators And, Or and Not

Volgend Onderwerp
Default Values and Literals

Default Values and Literals



In both If and Do statements we need to work with conditions. These conditions are formed by conditional expressions.

In a statically types programming language like Visual Basic all expressions are of a - by the compiler know - datatype. Conditional expressions are of type Boolean, therefore they are also called Boolean expressions.

Boolean expressions can only evaluated to two possible values, True or False.

Just like any other datatype variables can be declared of type Boolean.


Module Example1
    Sub Main()
        Console.WriteLine("Value 1 ?")
        Dim value1 As Integer = Console.ReadLine()
        '
        Console.WriteLine("Value 2 ?")
        Dim value2 As Integer = Console.ReadLine()
        '
        Dim value1Higher As Boolean = value1 > value2
        '
        If value1Higher Then
            Console.WriteLine(value1 & " > " & value2)
        Else
            Console.WriteLine(value1 & " <= " & value2)
        End If
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Value 1 ?
 10
 Value 2 ?
 5
 10 > 5

Output :

 Value 1 ?
 5
 Value 2 ?
 10
 5 <= 10

Variable value1Higher is declared of type Boolean, and is assigned a Boolean value.

A iteration ( Do ) or decision ( If ) can use this variables of type Boolean to form the condition.

In the above example variable value1Higher represents a value ( True or False ) whether ( True ) or not ( False ) value1 is more than value2.

When we want to check equality ( is value1 equal to value2 ) the equality operator = can be used ( value1 = value2 ).


Module Example2
    Sub Main()
        Console.WriteLine("Value 1 ?")
        Dim value1 As Integer = Console.ReadLine()
        '
        Console.WriteLine("Value 2 ?")
        Dim value2 As Integer = Console.ReadLine()
        '
        Dim equal As Boolean = value1 = value2                             ' (1)
        '
        If equal Then
            Console.WriteLine(value1 & " = " & value2)
        Else
            Console.WriteLine(value1 & " <> " & value2)
        End If
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Value 1 ?
 5
 Value 2 ?
 5
 5 = 5

Output :

 Value 1 ?
 5
 Value 2 ?
 10
 5 <> 10

Don't confuse the equality operator '=' with the assignment operator '='.
The same symbol is used, but for a different purpose, depending on the syntactical use.

A more readable version of line (1) can be formed when using parentheses :


 Dim equal As Boolean = (value1 = value2)

Default Values and Literals


Module Example3
    Sub Main()
        Dim integerVariable As Integer
        Console.WriteLine("Integer default value : " & integerVariable)
        integerVariable = 5
        Console.WriteLine("Integer changed value : " & integerVariable)
        '
        Dim stringVariable As String
        Console.WriteLine("String default value  : " & stringVariable)
        stringVariable = "text"
        Console.WriteLine("String changed value  : " & stringVariable)
        '
        Dim booleanVariable As Boolean
        Console.WriteLine("Boolean default value : " & booleanVariable)
        booleanVariable = True
        Console.WriteLine("Boolean changed value : " & booleanVariable)
        booleanVariable = False
        Console.WriteLine("Boolean changed value : " & booleanVariable)
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Integer default value : 0
 Integer changed value : 5
 String default value  :
 String changed value  : text
 Boolean default value : False
 Boolean changed value : True
 Boolean changed value : False

The above example illustrates the default values of the different datatypes.
A default value is a value a variable hold after declaration ( without initialization ).

- 0 for Integer variables ( and all other numeric datatypes )
- Nothing for String variables ( printed out as "" ( no characters ) )
- False for Boolean variables

You can also see how literals ( constant expressions ) are formed with these datatypes :

- Integer literals are formed by an integral value ( between -2147483648 and +2147483647 )
- String literals are formed with Nothing or surrounding double quotes ( " )
- Boolean literals are formed with True or False


Updated On : 2008-11-06

Download Broncode

Published On : 2008-11-06

Boolean Datatype and Expressions

Vorig Onderwerp

Selections

|

Logical Operators And, Or and Not

Volgend Onderwerp

Introduction to Visual Basic

Arrays

Volgend Onderwerp
  Home  
Nederlands
Nederlands

Add to favorites (IE).


No printable version available.