Conditional Statements
Conditional Statements
OPERATOR
Comparison operators
Operator
= (Equal to)
End Sub
End Module
Example 2
•Write a program that will print
True/False if 10 is equal, not equal,
greater than, less than, greater
than or equal to, less than or equal
to 20 at the same time.
Module Program
Sub Main(args As String())
Dim num1, num2 As Integer
num1 = 10
num2 = 20
Console.WriteLine(num1 = num2)
Console.WriteLine(num1 <> num2)
Console.WriteLine(num1 > num2)
Console.WriteLine(num1 < num2)
Console.WriteLine(num1 >= num2)
Console.WriteLine(num1 <= num2)
End Sub
End Module
QUIZ
•Write a program that will print
True/False if the first input number is
equal, not equal, greater than, less
than, greater than or equal, less than
or equal to the second input number.
Output must be printed at the same
time.
CONDITIONAL
STATEMENTS
If...Then
• The If...Then is the simplest form of control statement, frequently
used in decision making and changing the control flow of the program
execution.
Syntax
If condition Then
[Statement(s)]
End If
Syntax
With comparison operator
= <> > < >= <=
If condition Then
[Statement(s)]
End If Behavior if the condition is
satisfied
Example 1
End If
Output
End Sub Just blank, no output
End Module
Example 1
•Create a program that will
display “num1 is positive” if
the given number is
positive, otherwise “num1
is negative”.
Dim num1 As Integer = 7
Syntax
If condition Then
[ statement(s) ]
Else
[ elsestatement(s) ]
End If
Example 2
Else
Console.WriteLine("It is an odd number")
End If
If count = 0 Then
message = "There are no items."
Else
message = "There are” & count & “items."
End If
Console.WriteLine(message)
End Sub
End Module
•Write a program that will
print both the larger and
smaller among two numbers.
Module Program
Sub Main(args As String())
Dim a, b As Integer
If a > b Then
Console.WriteLine(" larger number = {0} and smaller number = {1}
", a, b)
Else
Console.WriteLine(" larger number = {0} and smaller number = {1}
", b, a)
End If
Console.ReadKey()
End Sub
End Module
If...Then...ElseIf Statements
• In some cases, we need to use a sequence of
If...Then structures or multiple If...Then...Else
statements, where the Else clause is a new If
structure.
If...Else If
Syntax
If (condition 1) Then
[Executes if condition 1 is true ]
ElseIf (condition 2) Then
[ Executes if condition 2 is true ]
ElseIf (condition 3) Then
[ Executes if condition 3 is true ]
Else
[ executes when the none of the above condition is true ]
End If
Module decisions
Sub Main()
Dim a As Integer = 20
If (a = 10) Then
Console.WriteLine(“10") '
ElseIf (a = 20) Then
Console.WriteLine("20") '
ElseIf (a = 30) Then
Console.WriteLine("30")
ElseIf (a = 20) Then
Console.WriteLine(“3490")
Else
Console.WriteLine("None of the values is matching")
End If
Console.ReadKey()
End Sub
End Module
Public Sub Example3()
Dim marks As Integer
marks = Console.ReadLine()