Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Conditional Statements

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Conditional Statements

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

COMPARISON

OPERATOR
Comparison operators
Operator

< (Less than)

<= (Less than or equal to)

> (Greater than)

>= (Greater than or equal to)

= (Equal to)

<> (Not equal to)


Example 1

•Write a program that


will print True if 100 is
greater than 50.
Module Program
Sub Main(args As String())
Dim num1, num2 As
Integer
num1 = 100
num2 = 50
Dim num3 As Boolean
num3 = 100 > 50
Console.WriteLine(num3)
End Sub
End Module
Module Program
Sub Main(args As String())
Dim num1, num2 As
Integer
num1 = 100
num2 = 50 Data types is Boolean
Dim num3 As Boolean
num3 = 100 > 50
Console.WriteLine(num3) Output
End Sub True
End Module
Module Program
Sub Main(args As String())
Dim num1, num2 As Integer
num1 = 100
num2 = 50
Console.WriteLine( num1 > num2
)
End Sub
End Module
Module Program
Sub Main(args As String())

Console.WriteLine( 100 >


50 )

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

•Create a program that


will display “The numbers
are equal” if the two
numbers are equal.
Module Program
Sub Main(args As String())
Dim num1, num2 As Integer
num1 = -10
num2 = 10

If num1 = num2 Then

Console.WriteLine("The two numbers are equal")

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

If num1 > 0 Then


Console.WriteLine("num1 is a positive number.")
End If

If num1 < 0 Then


Console.WriteLine("num2 is a negative number.")
End If
Example 2

•Create a program that will


display which from the two
input number is greater.
Module if_statement2
Sub Main()
Dim num1, num2 As Integer
Console.WriteLine("Enter any two number:")
num1 = Console.ReadLine()
num2 = Console.ReadLine()
If num1 > num2 Then
Console.WriteLine("First number is greater than second number")
End If
If num1 < num2 Then
Console.WriteLine("Second number is greater than First number")
End If
Console.WriteLine("press any key to exit...")
Console.ReadKey()
End Sub
If...Then...Else
• In an If...Then...Else statement, if the condition evaluates to true, the
Body of the conditional statement runs. If the condition is false, the
else-statement runs.

Syntax
If condition Then
[ statement(s) ]
Else
[ elsestatement(s) ]
End If
Example 2

•Write a program to check


whether the number is even
or odd.
Module Program
Sub Main(args As String())
Dim num As Integer
Console.WriteLine("Enter the Number")
num = Console.ReadLine()

If (num Mod 2 = 0) Then


Console.WriteLine("It is an even number")

Else
Console.WriteLine("It is an odd number")
End If

Console.WriteLine("press any key to exit...")


Console.ReadKey()
End Sub
Dim randomizer As New Random()
Dim count As Integer = randomizer.Next(0,
10)
minimum maximum
Module Program
Sub Main(args As String())
Dim randomizer As New Random()
Dim count As Integer = randomizer.Next(0, 10)

Dim message As String

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

Console.WriteLine("Enter the first number : ")


a = Console.ReadLine()
Console.WriteLine("Enter the second number : ")
b = Console.ReadLine()

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()

If marks >= 90 Then


Console.WriteLine("A+")
ElseIf marks >= 80 Then
Console.WriteLine("A")
ElseIf marks >= 70 Then
Console.WriteLine("B")
ElseIf marks >= 60 Then
Console.WriteLine("C")
ElseIf marks >= 50 Then
Console.WriteLine("D")
Else
Console.WriteLine("F")
End If
End Sub
Between numbers – use And
Module Program
Sub Main(args As String())
Dim grade As Integer
Console.WriteLine("Enter grade")
grade = Console.ReadLine
If grade >= 75 Then
Console.WriteLine("Pass")
ElseIf (grade <= 74) And (grade >= 70) Then
Console.WriteLine("Remediation")
ElseIf (grade <= 69) And (grade >= 65) Then
Console.WriteLine("Fail")
ElseIf (grade <= 64) Then
Console.WriteLine("Drop out")
End If
End Sub
End Module
Performance Tasks 1 &2 – (2 periods)

Class name – ScoreAutomationApollo1


Create a Console and Windows forms application to
solve a score based on the input criteria below.

Relevance to the Theme – 50% (input)


Originality – 25% (input)
Creativity – 25% (input)

Perfect score is 30.


SCORE PERCENTAGE
Class name – ScoreAutomationApollo2 COVERAGE
2 >49
Create a Console application to solve a
15 50 - 55
score based on the input criteria below.
18 56 - 64
Relevance to the Theme – 50% 20 65 - 74
22 75 - 82
Originality – 25%
24 83 - 88
Creativity – 25% 26 89 - 93
28 94 - 97
• Final score must be based on the table.
30 98 - 100

You might also like