Sei sulla pagina 1di 27

Problem Solving

and Control
Statements
ForNext Repetition
Statement
Counter-controlled repetition requires:
the name of a control variable (or loop counter) thats
used to determine whether the loop continues to iterate
the initial value of the control variable
the increment (or decrement) by which the control
variable is modified each time through the loop
the condition that tests for the final value of the control
variable (that is, whether looping should continue).
Examples Using the ForNext
Statement
The following examples demonstrate different ways of
varying the control variable in a ForNext statement.
In each case, we write the appropriate ForNext
header using local type inference.
Vary the control variable from 1 to 100 in increments of 1.
For i = 1 To 100 or For i = 1 To 100 Step 1
Vary the control variable from 100 to 1 in decrements of 1.
For i = 100 To 1 Step -1
Nested For/Next Example

For RowInteger As Integer= 0 To 2


For ColumnInteger As Integer= 0 To 3
' Initialize each element.
NameString(RowInteger, ColumnInteger) = " "
Next ColumnInteger
Next RowInteger
Using Exit to Terminate
Repetition Statements
There are many forms of the Exit statement, designed
to terminate different types of repetition statements.
When the Exit Do statement executes in a Do While
Loop, DoLoop While, Do UntilLoop or DoLoop Until
statement, the program terminates that repetition
statement and continues execution with the first
statement after the repetition statement.
Similarly, the Exit For statement and the Exit While
statement cause immediate exit from ForNext and
WhileEndWhile loops, respectively.
The Exit Select statement causes immediate exit from a
SelectCase statement.
For Counter As Integer = 0 To 5
Exit the loop if Counter is 3
If Counter = 3 Then
Exit For
End If
MessageBox.Show(Current Counter = & Counter.ToString)
Next Counter
Using Continue in Repetition
Statements
A Continue statement terminates only the current
iteration of a repetition statement and continues
execution with the next iteration of the loop.
The Continue Do statement can be executed in a Do
WhileLoop, DoLoop While, Do UntilLoop or Do
Loop Until statement.
Similarly, the Continue For statement and Continue
While statement can be used in ForNext and While
EndWhile statements, respectively.
For Counter As Integer = 0 To 5
Exit the loop if Counter is 3
If Counter = 3 Then
Continue For
End If
MessageBox.Show(Current Counter = &
Counter.ToString)
Next Counter
SelectCase Multiple-
Selection Statement
Occasionally, an algorithm contains a series of decisions
that test a variable or expression separately for each
value that the variable or expression might assume.
The algorithm takes different actions based on those
values.
The SelectCase multiple-selection statement handles
such decision making.
SelectCase Multiple-
Selection Statement
Types of Case Statements
Case statements also can use relational operators to
determine whether the controlling expression satisfies a
condition.
For example
Case Is < 0
uses keyword Is along with the relational operator, <, to
test for values less than 0.
Multiple values can be tested in a Case statement by
separating the values with commas, as in
Case 0, 5 To 9
which tests for the value 0 or values in the range 59.
Also, Cases can be used to test String values.
SelectCase Multiple-
Selection Statement
Select Case GradeString Select Case GradeInteger
Case 100
Case A MessageBox.Show(Super)
MessageBox.Show(Super) Case 90 To 99
MessageBox.Show(Very good)
Case B
Case 80 To 89
MessageBox.Show(Good) MessageBox.Show(Good)
Case 70 To 79
Case C
MessageBox.Show(Average)
MessageBox.Show(Average) Case 60 To 69
Case D, F MessageBox.Show(Poor)
Case Else
MessageBox.Show(Better luck next time) MessageBox.Show(Better luck next time)
End Select End Select
Logical Operators

To make a decision that relied on the evaluation of


multiple conditions, we performed these tests in
separate statements or in nested IfThen or IfThen
Else statements.
To handle multiple conditions more efficiently, the
logical operators can be used to form complex
conditions by combining simple ones.
Logical operators are And, Or, AndAlso, OrElse, Xor and
Not.
Logical Operator - And

If expression1 And expression2 Then


Logical Operators

Logical And Operator


Suppose we wish to ensure that two conditions are both
True in a program before a certain path of execution is
chosen.
In such a case, we can use the logical And operator as
follows:
If gender = "F" And age >= 65 Then
seniorFemales += 1
End If

This IfThen statement contains two simple conditions.


The readability can be improved by adding redundant
parentheses:
(gender = "F") And (age >= 65)
Logical Operators - Or

If expression1 Or expression2 Then


Logical Operators

Logical Or Operator (Also Called the Logical Inclusive Or


Operator)
Now lets consider the Or operator.
Suppose we wish to ensure that either or both of two
conditions are True before we choose a certain path of
execution.
We use the Or operator as in the following program
segment:
If (semesterAverage >= 90 Or finalExam >= 90) Then
resultLabel.Text = "Student grade is A"
End If

This statement also contains two simple conditions.


Logical Operators

Logical AndAlso and OrElse Operators


The logical AND operator with short-circuit evaluation
(AndAlso) and the logical inclusive OR operator with
short-circuit evaluation (OrElse) are similar to the And
and Or operators, respectively, with one exceptionan
expression containing AndAlso or OrElse operators is
evaluated only until its truth or falsity is known.
Logical Operators
AndAlso/OrElse
expression 1 expression 2 expression 1 AndAlso expression 2
True True True
True False False
False Not evaluated False

expression 1 expression 2 expression 1 OrElse expression 2


True Not evaluated True
False True True
False False False
Logical Operators

For example, the expression


(gender = "F" AndAlso age >= 65)

stops evaluating immediately if gender is not equal to


"F" (that is, the entire expression is False); the second
expression is irrelevant because the first condition is
False.
Evaluation of the second condition occurs if and only if
gender is equal to "F" (that is, the entire expression
could still be True if the condition age >= 65 is True).
This performance feature for the evaluation of AndAlso
and OrElse expressions is called short-circuit evaluation.
Logical Operators Xor

Xor Exclusive Or
Used in Cryptography, Parity checks

expression 1 expression 2 expression 1 Xor expression 2


True True False
True False True
False True True
False False False
Logical Operator - Not

If Not expression Then


Logical Operators

Logical Not Operator


The Not (logical negation) operator enables you to
reverse the meaning of a condition.
Unlike the logical operators And, AndAlso, Or, OrElse
and Xor, which each combine two conditions, the
logical negation operator is a unary operator, requiring
only one operand.
The logical negation operator is placed before a
condition to choose a path of execution if the original
condition (without the logical negation operator) is
False.
Logical Operators

The logical negation operator is demonstrated by the


following program segment:
If Not (value = 0) Then
resultLabel.Text = "The value is " & value
End If

The parentheses around the condition value = 0 are


necessary because the logical negation operator (Not)
has a higher precedence than the equality operator.
App: Dental Payment
Calculator
A dentists office administrator wishes to create an app that employees can use to bill
patients. The app must allow users to enter the patients name and specify which services
were performed during the visit. The app will then calculate the total charges. If a user
attempts to calculate a bill before any services are specified, or before the patients name
is entered, an error message will be displayed informing the user that necessary input is
missing.