Introduction to Problem Solving and Control Statements Introduction
Introduction to Problem Solving and Control Statements
Introduction Before writing an app to solve a problem, you should have a thorough understanding of the problem and a carefully planned approach. When writing an app, it’s also important to know the available building blocks and to use proven program-construction principles.
Algorithms Any computing problem can be solved by executing a series of actions in a specific order. ◦ A procedure for solving a problem, in terms of the actions to be executed and the order in which these actions are to be executed is called an algorithm. There is a three-step process when writing a Visual Basic application—you set up the user interface, define the properties, and then create the code.
Pseudocode Algorithm Pseudocode is an informal language that helps you develop algorithms. It’s similar to everyday English; it’s convenient and user friendly, but not an actual computer programming language. The pseudocode we present is particularly useful for developing algorithms that will be converted to Visual Basic apps. pseudocode Main Procedure Monopoly_Game Hand out each player's initial money. Decide which player goes first. Repeat Call Procedure Monopoly_Move for next player. Decide if this player must drop out. Until all players except one have dropped out. Declare the surviving player to be the winner.
The Boolean Data Type Revisited Can help when searching a list for a specific value Boolean variable is always in one of two states: True or False. ◦ When a particular situation occurs, set Boolean variable to True. ◦ Use a loop to check for True Many programmers refer to Boolean variables as switches or flags. ◦ Switches have two states — on or off. ◦ Flags are considered either up or down.
Control Structures Sequence Structure ◦ Unless directed otherwise, the computer executes statements sequentially. ◦ You can have as many actions as you want in a sequence structure. ◦ Anywhere a single action may be placed, you may place several actions in sequence.
Control Structures - Selection Statements Visual Basic provides three types of selection statements. o The If…Then selection statement either performs an action if a condition is true, or skips the action if the condition is false. Called a single-selection statement because it selects or ignores a single action. o The If…Then…Else selection statement performs an action if a condition is true, and performs a different action if the condition is false. Called a double -selection statement because it selects between two different actions. o The Select…Case selection statement performs one of many possible actions, depending on the value of an expression. Called a multiple-selection statement.
Decision Making: Equality and Relational Operators The If…Then statement allows a program to make a decision based on the truth or falsity of some expression. The expression in an If…Then statement is called a condition. o If the condition is met (that is, the condition is true), the statement in the If…Then statement’s body executes. o If the condition is not met (that is, the condition is false), the body statement does not execute. Conditions in If…Then statements can be formed by using the equality operators and relational operators (also called comparison operators).
Equality and Relational Operators
If…Then Selection Statement Suppose that the passing grade on an examination is 60 (out of 100). Then the pseudocode statement ◦ If student’s grade is greater than or equal to 60 then Display “Passed” determines whether the condition “student’s grade is greater than or equal to 60” is true or false. The preceding pseudocode If statement may be written in Visual Basic as ◦ If student. Grade >= 60 Then result. Label. Text = "Passed" ' display "Passed" End If
If…Then…Else Selection Statement The If…Then…Else selection statement allows you to specify that a different action (or sequence of actions) is to be performed when the condition is true than when the condition is false. For example, the pseudocode statement If student’s grade is greater than or equal to 60 then Display “Passed” Else Display “Failed” The preceding pseudocode If…Else statement may be written in Visual Basic as If student. Grade >= 60 Then result. Label. Text = "Passed" ’ display "Passed" Else result. Label. Text = "Failed" ’ display "Failed" End If
Nested If…Then…Else Selection Statements Nested If…Then…Else statements test for multiple conditions by placing If…Then …Else statements inside other If…Then…Else statements.
Repetition - Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations is unknown A Do/Loop terminates based on a specified condition. ◦ Execution of the loop continues while a condition is True or until a condition is True. ◦ The condition can be placed at the top (pretest)or the bottom (posttest) of the loop.
The Do and Loop Statements — General Form Do {While |Until} condition ' Statements in loop. Loop Top of Loop Condition, Pretest/Entry test --OR— Do ' Statements in loop. Loop {While | Until} condition Bottom of Loop Condition, Posttest/ Exit
Pretest vs. Posttest Pretest — loop may never be executed since tested BEFORE running. ◦ Do While … Loop ◦ Do Until … Loop Posttest — loop will always be executed at least once. ◦ Do … Loop While ◦ Do … Loop Until
Repetition Statements Performing a Calculation in a Do While…Loop Repetition Statement o Consider an app segment designed to find the first power of 3 larger than 100. o Suppose that the Integer variable product is initialized to 3. o When the following Do While…Loop statement finishes executing, product contains the result: Do While product <= 100 product = product * 3 ' compute next power of 3 Loop
Exiting Loops In some situations, you may need to exit the loop prematurely. Click on the form’s close box or use the VB menu bar or toolbar to stop the program; or Ctrl+Break. Use the Exit Do/For statement inside the loop structure. Generally, the Exit Do/For statement is part of an If statement.
List. Boxes and Combo. Boxes Have most of the same properties and operate in a similar fashion ◦ An exception is that a combo box control has a Drop. Down. Style property Provide the user with a list of items to select from Various styles — choose based on ◦ Space available ◦ Need to select from an existing list ◦ Need to add to a list
List Boxes and Combo Boxes Various Styles of List and Combo boxes
The Items Collection List of items in a List. Box or Combo. Box is a collection. VB Collections are objects that have properties and methods that allow ◦ ◦ ◦ Adding items Removing items Referring to individual elements Counting items Clearing the collection
Filling a List/Using the Properties Window Design time in Properties window ◦ Items property ◦ Click on ellipses to open String Collection Editor. ◦ Type list items, end each line with Enter key. Run time methods ◦ Items. Add ◦ Items. Insert --OR--
Formulating Algorithms: Counter. Controlled Repetition Pseudocode Algorithm with Counter-Controlled Repetition o Let’s use pseudocode to list the actions to execute and specify the order of execution for calculating the class average. o After the user enters the grades and presses the Calculate Average Button, we use counter-controlled repetition to get the grades from the List. Box and process them one at a time. o This technique uses a variable called a counter (or control variable) to specify the number of times that a set of statements will execute. o This is also called definite repetition because the number of repetitions is known before the loop begins executing.
- Slides: 25