1 / 27

Problem Solving and Control Statements

Problem Solving and Control Statements. 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

lynettew
Download Presentation

Problem Solving and Control Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Solving and Control Statements

  2. 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).

  3. 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

  4. 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.

  5. 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

  6. 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.

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

  8. 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.

  9. Select…Case 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 5–9. Also, Cases can be used to test String values.

  10. Select…Case Multiple-Selection Statement If GradeString = “A” Then MessageBox.Show(“Super”) ElseIf Gradestring = “B” Then MessageBox.Show(“Good”) ElseIf GradeString = “C” Then MessageBox.Show(“Average”) ElseIf GradeString = “D” MessageBox.Show(“Better luck next time”) ElseIf GradeString = “F” Then MessageBox.Show(“Better luck next time”) End If 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

  11. Select…Case Multiple-Selection Statement If GradeInteger = 100 Then MessageBox.Show(“Super”) ElseIf GradeInteger >=90 And GradeInteger <=99 Then MessageBox.Show(“Very good”) ElseIf GradeInteger >= 80 And GradeInteger <=89 Then MessageBox.Show(“Good”) ElseIf GradeInteger >=70 And GradeInteger <=79 Then MessageBox.Show(“Average”) ElseIf GradeInteger >=60 And GradeInteger <=69 Then MessageBox.Show(“Poor”) Else MessageBox.Show(“Better luck next time”) End If 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

  12. 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.

  13. Logical Operator - And • If expression1 And expression2 Then

  14. 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 += 1End If This If…Then statement contains two simple conditions. The readability can be improved by adding redundant parentheses: (gender = "F") And (age >= 65)

  15. Logical Operators - Or • If expression1 Or expression2 Then

  16. Logical Operators • Logical 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.

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

  18. Logical Operators – AndAlso/OrElse

  19. 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.

  20. Logical Operators – Xor • Xor – Exclusive Or • Used in Cryptography, Parity checks

  21. Logical Operator - Not • If Not expression Then

  22. 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.

  23. 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.

  24. 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.

More Related