Lesson 9 Decision Making in Programming

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

THE DECISION MAKING

IN PROGRAMMING
MR. ANGELITO G.
SALAC
ICT INSTRUCTOR
•Decision making process is an important part of
programming because it can solve practical problems
intelligently and provide effective output to the user.
Programs that can ask the computer to perform certain
task until a certain condition is met.
•Decision making structures require that the programmer
specifies one or more conditions to be evaluated or tested
by the program, along with a statement or statements to
be executed if the condition is determined to be true, and
optionally, other statements to be executed if the condition
is determined to be false.
Conditional Operators
Conditional Operators are
powerful tools that resemble OPERATOR DESCRIPTION

mathematical operators. They = EQUAL TO


> GREATER THAN
allow a Visual Basic program < LESS THAN

to compare data values and >= GREATER THAN


EQUAL TO

decide what actions to take, <= LESS THAN EQUAL TO


<> NOT EQUAL TO
whether to execute a program
or to terminate the program
and more.
LOGICAL OPERATORS

Logical Operators are used OPERATOR DESCRIPTION


to make more than one And Both sides must be true
Or One side or other must be
comparison before a true
Xor One side or other must be
decision can be made and an true but not both
Not Negates true
action take.
IF Statement

The IF Statement is the simplest control flow structure.


It is used to ask the computer to perform an action
specified by VB expressions, if the condition is true.
But when the condition is false, no action will be
performed.
THE IF CONDITIONS
THE IF…THEN
STATEMENT

• An If…statement consists of a Syntax:


Boolean expression followed If condition Then
by one or more statements. VB Expression
End If
EXAMPLE 1: IF…STATEMENT

Dim myNumber As Integer


myNumber = TextBox1.Text
If myNumber > 100 Then
Label2.Text = “The number is greater than 100”
End If
THE IF…THEN…ELSE…
STATEMENT

•An If...Then statement can be Syntax:


followed by an optional Else If condition Then
statement, which executes when
VB Expression
the Boolean expression is false.
Else
VB Expression
End If
EXAMPLE 1: IF…THEN… ELSE… STATEMENT

Dim myNumber As Integer


myNumber = TextBox1.Text
If myNumber > 100 Then
Label2.Text = “The number is Greater Than 100”
Else
Label2.Text = “The number is Less Than 100”
End If
THE IF…ELSE LADDER
STATEMENT
Syntax:
• The if-else if-else statement is an extension If condition1 Then
to the if-else statement. It is used in the
VB Expression
scenario where there are multiple cases to be
performed for different conditions. If a Else if condition2 Then
condition is true then the statements VB Expression
defined in the if block will be executed,
Else if condition3 Then
otherwise if some other condition is true
then the statements defined in the else-if VB Expression
block will be executed, at the last if none of Else
the condition is true then the statements
defined in the else block will be executed. VB Expression
End If
EXAMPLE 1: IF… ELSE LADDER STATEMENT
Dim Mark As Integer
Dim Grade As String
Mark = Textbox1.Text
If Mark >= 80 Then
Grade = “A”
Label1.Text = Grade
ElseIf Mark >= 60 And Mark < 80 Then
Grade = “B”
Label1.Text = Grade
ElseIf Mark >= 40 And Mark < 60 Then
Grade = “C”
Label1.Text = Grade
Else
Grade = “D”
Label1.Text = Grade
End If
THE SELECT-CASE
STATEMENT
THE SELECT-CASE
STATEMENT
Syntax:
• The Select-Case control structure is somewhat Select Case test expression
Case Expression list 1
different from the if…else… Ladder control
Block of one or more VB
structure. It only makes a decision on one expression Statements
or dimension, while the if…else… ladder statement Case Expression list 2

may evaluate all given expressions. In addition, each Block of one or more VB
Statements
if…else… ladder statement may also compute
Case Expression list 3
entirely different dimensions. The Select Case is Block of one or more VB
preferred when there exist many different conditions, Statements

such as using if…else…ladder statements that could Case Expression list 4


Block of one or more VB
be too messy in the program.
Statements
End Select
EXAMPLE 1: SELECT CASE STATEMENT
Dim mark As Single
mark = Textbox1.Text

Select Case mark


Case 0 To 49
Label1.Text = “Need to work harder”
Case 50 To 64
Label1.Text = “Average”
Case 65 To 79
Label1.Text = “Above Average”
Case 80 To 94
Label1.Text = “Good”
Case Else
Label1.Text = “Excellence”
End Select

You might also like