+ All Categories
Home > Documents > Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many...

Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many...

Date post: 13-Jan-2016
Category:
Upload: cory-anderson
View: 224 times
Download: 0 times
Share this document with a friend
Popular Tags:
27
Problem Solving and Control Statements
Transcript
Page 1: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Problem Solving and Control Statements

Page 2: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

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, Do…Loop While, Do Until…Loop or Do…Loop 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 For…Next and While…End While loops, respectively.

The Exit Select statement causes immediate exit from a Select…Case statement.

Page 3: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

For Counter As Integer = 0 To 5‘ Exit the loop if Counter is 3

If Counter = 3 ThenExit For

End If

MessageBox.Show(“Current Counter = “ & Counter.ToString)

Next Counter

Page 4: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

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 While…Loop, Do…Loop While, Do Until…Loop or Do…Loop Until statement.

Similarly, the Continue For statement and Continue While statement can be used in For…Next and While…End While statements, respectively.

Page 5: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

For Counter As Integer = 0 To 5‘ Exit the loop if Counter is 3

If Counter = 3 ThenContinue For

End If

MessageBox.Show(“Current Counter = “ & Counter.ToString)

Next Counter

Page 6: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

For…Next Repetition Statement Counter-controlled repetition requires:

◦ the name of a control variable (or loop counter) that’s 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).

Page 7: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
Page 8: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
Page 9: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Examples Using the For…Next Statement

The following examples demonstrate different ways of varying the control variable in a For…Next statement.

In each case, we write the appropriate For…Next 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

Page 10: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Nested For/Next Example

For RowInteger As Integer= 0 To 2For ColumnInteger As Integer= 0 To 3

' Initialize each element.NameString(RowInteger, ColumnInteger) = " "

Next ColumnIntegerNext RowInteger

Page 11: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Select…Case 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 Select…Case multiple-selection statement handles such decision making.

Page 12: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Select…Case Multiple-Selection StatementTypes of Case Statements

Case statements also can use relational operators to determine whether the controlling expression satisfies a condition.

For exampleCase 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 5–9.

Also, Cases can be used to test String values.

Page 13: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Select…Case Multiple-Selection Statement

Select Case GradeString

Case “A”

MessageBox.Show(“Super”)

Case “B”

MessageBox.Show(“Good”)

Case “C”

MessageBox.Show(“Average”)

Case “D”, “F”

MessageBox.Show(“Better luck next time”)

End Select

Select Case GradeInteger

Case 100

MessageBox.Show(“Super”)

Case 90 To 99

MessageBox.Show(“Very good”)

Case 80 To 89

MessageBox.Show(“Good”)

Case 70 To 79

MessageBox.Show(“Average”)

Case 60 To 69

MessageBox.Show(“Poor”)

Case Else

MessageBox.Show(“Better luck next time”)

End Select

Page 14: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Logical Operators To make a decision that relied on the evaluation of multiple conditions, we performed these tests in separate statements or in nested If…Then or If…Then…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.

Page 15: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Logical Operator - And

If expression1 And expression2 Then

Page 16: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Logical OperatorsLogical 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 += 1End If

This If…Then statement contains two simple conditions.

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

Page 17: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Logical Operators - Or

If expression1 Or expression2 Then

Page 18: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Logical OperatorsLogical Or Operator (Also Called the Logical Inclusive Or Operator) Now let’s 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.

Page 19: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Logical OperatorsLogical 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 exception—an expression containing AndAlso or OrElse operators is evaluated only until its truth or falsity is known.

Page 20: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Logical Operators – AndAlso/OrElseexpression 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

Page 21: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

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.

Page 22: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

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

Page 23: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Logical Operator - Not

If Not expression Then

Page 24: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Logical OperatorsLogical 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.

Page 25: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Logical Operators The logical negation operator is demonstrated by the following program segment:

If Not (value = 0) Then resultLabel.Text = "The value is " & valueEnd If

The parentheses around the condition value = 0 are necessary because the logical negation operator (Not) has a higher precedence than the equality operator.

Page 26: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

App: Dental Payment Calculator ◦ A dentist’s office administrator wishes to create an app that

employees can use to bill patients. The app must allow users to enter the patient’s 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 patient’s name is entered, an error message will be displayed informing the user that necessary input is missing.

Page 27: Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.

Recommended