Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Examples

 

Jacopini and Bohm

 

Example 1

While

Example 2

While

Example 3

Do While   Loop

Example 4

Do Until   Loop

Example 5

If ElseIf  End If

Example 6

Select Case

 

 

 

 

Example 1

Using While to Find the sum of integers

 

' Calculates the sum of the integers from 1 to 10.
Module Calculate
    Sub Main()
        Dim sum As Integer = 0
        Dim number As Integer = 1

        While number <= 10
            sum += number
            number += 1
        End While

        Console.WriteLine("The sum is: " & sum)
    End Sub
End Module


 

 

Example 2

Using While to Find Powers of 3

 

' Demonstration of While statement.
Module PowersOfThree
    Sub Main()
        Dim product As Integer = 3

        ' statement multiplies and displays product while product is less than or equal to 100
        While product <= 100
            Console.Write(product & " ")
            product = product * 3 ' compute next power of 3
        End While

        Console.WriteLine() ' write blank line

        ' print result
       
Console.WriteLine("First power of 3 " & "larger than 100 is " & product)
    End Sub
End Module ' PowersOfThree

 

Example 3

Do While ... Loop Statement to Find Powers of 3

 

' Demonstration of the Do While...Loop statement.
Module DoWhile
    Sub Main()
        Dim product As Integer = 3   ' initialize product

        ' statement multiplies and displays the product while it is less than or equal to 100
        Do While product <= 100
            Console.Write(product & " ")
            product = product * 3
        Loop

        Console.WriteLine()   ' write blank line

        ' print result
        Console.WriteLine("First power of 3 " & "larger than 100 is " & product)
    End Sub
End Module

 

 

Example 4

Do Until...Loop in Finding Powers of 3

 

' Demonstration of the Do Until...Loop statement.
Module DoUntil
    Sub Main()
        Dim product As Integer = 3

        ' find first power of 3 larger than 100
        Do Until product > 100
            Console.Write(product & " ")
            product = product * 3
        Loop

        Console.WriteLine() ' write blank line

        ' print result
        Console.WriteLine("First power of 3 " & "larger than 100 is " & product)
    End Sub
End Module

 

Example 5
If Structures

 

Module Module1

    Sub Main()

        Dim Grade As Integer

        Console.WriteLine("Please enter the student's grade")

        Grade = Console.ReadLine()

        If Grade >= 90 Then

            Console.WriteLine("A")

        ElseIf Grade >= 80 Then

            Console.WriteLine("B")

        ElseIf Grade >= 70 Then

            Console.WriteLine("C")

        ElseIf Grade >= 60 Then

            Console.WriteLine("D")

        Else

            Console.WriteLine("F")

        End If

    End Sub

End Module

 

 


 

 

Example 6
Select Case

 

Module Module1

    Sub Main()

        Console.WriteLine("Please enter a grade")

        Dim Grade As Integer = Console.ReadLine()

        Select Case Grade

            Case 90 To 100

                Console.WriteLine("A")

            Case 80 To 89

                Console.WriteLine("B")

            Case 70 To 79

                Console.WriteLine("C")

            Case 60 To 69

                Console.WriteLine("D")

            Case Else

                Console.WriteLine("F")

        End Select

    End Sub

End Module