Conditional operator, positive & smaller Learn programming Visual Basic (VB.net)

Lesson:

Flow Control


Exercise:

Conditional operator, positive & smaller


Objetive:

Create a Visual Basic (VB.net) program that asks the user for two numbers and answers, using the conditional operator (?), for the following:

If the first number is positive
If the second number is positive
If both are positive
Which one is smaller


Code:

Imports System
Public Class exercise45
    Public Shared Sub Main()
        Dim a, b As Integer
        Dim answer As String
        Console.Write("Enter the first number: ")
        a = Convert.ToInt32(Console.ReadLine())
        Console.Write("Enter the second number: ")
        b = Convert.ToInt32(Console.ReadLine())

        If a > 0 Then
            Console.WriteLine("a is positive")
        Else
            Console.WriteLine("a is not positive")
        End If

        If a > 0 Then
            answer = "a is positive"
        Else
            answer = "a is not positive"
        End If

        Console.WriteLine(answer)
        answer = If(a > 0, "a is positive", "a is not positive")
        Console.WriteLine(answer)
        Console.WriteLine(If(b > 0, "b is positive", "b is not positive"))
        answer = If((a > 0) AndAlso (b > 0), "both are positive", "not both are positive")
        Console.WriteLine(answer)
        Dim smallest As Integer = If(a < b, a, b)
        Console.WriteLine("Smallest: {0}", smallest)
    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.