Download presentation
Presentation is loading. Please wait.
Published byBenny Setiabudi Modified over 6 years ago
1
Introduction to Problem Solving and Control Statements
2
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.
3
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.
4
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.
5
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. Boolean variables can be very useful when setting and testing conditions for a loop. Can be used to search through a list for a specific value Three step process — dimension a variable; set its initial value and then set a variable to True.
6
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.
7
Control Structures - Selection
Selection Statements Visual Basic provides three types of selection statements. 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. 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. The Select…Case selection statement performs one of many possible actions, depending on the value of an expression. Called a multiple-selection statement.
8
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. If the condition is met (that is, the condition is true), the statement in the If…Then statement’s body executes. 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).
9
Equality and Relational Operators
10
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 studentGrade >= 60 Then resultLabel.Text = "Passed" ' display "Passed" End If
11
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 studentGrade >= 60 Then resultLabel.Text = "Passed" ’ display "Passed" Else resultLabel.Text = "Failed" ’ display "Failed" End If
12
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.
14
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 computer is capable of repeating a group of instructions many times without calling the procedure for each new set of data—the process is called looping.
15
The Do and Loop Statements — General Form
Do {While |Until} condition ' Statements in loop. Loop --OR— Do Loop {While | Until} condition Top of Loop Condition, Pretest/Entry test Bottom of Loop Condition, Posttest/ Exit
16
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 *Next slide shows examples of prettest and posttest loops.
17
Repetition Statements
Performing a Calculation in a Do While…Loop Repetition Statement Consider an app segment designed to find the first power of 3 larger than 100. Suppose that the Integer variable product is initialized to 3. When the following Do While…Loop statement finishes executing, product contains the result: Do While product <= product = product * 3 ' compute next power of 3 Loop
19
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. If in an endless loop that can be caused by changing the value of a loop index variable, the program execution may need to be broken manually. The Exit For and Exit Do transfer control to the statement following the loop termination—the Next or Loop statement at the bottom of the loop structure.
20
ListBoxes and ComboBoxes
Have most of the same properties and operate in a similar fashion An exception is that a combo box control has a DropDownStyle 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 The Windows ListBox and ComboBox controls can be used to display lists on a form. Items may be added to a list during design time, run time, or perhaps a combination of both.
21
List Boxes and Combo Boxes
When adding a list control to a form, choose the style according to the space that is available and how the box is to operate. At design time, the behavior of list boxes and combo boxes differs—for list boxes, VB displays the Name property in the control; for combo boxes, the Text property displays, which is blank by default Combo boxes have a Text property which can be set at design time; List boxes also have a Text property but it can only be accessed at run time. Various Styles of List and Combo boxes
22
The Items Collection List of items in a ListBox or ComboBox 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 Items in a collection can be referred to by an index, which is zero based—for example, if a collection holds 10 items, the indexes to refer to the items range from 0 to 9; the first item in the Items collection would be Item 0.
23
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 OR-- Items.Insert Several methods can be used to fill the Items collection of a List box and Combo box. If the list contents are known at design time and the list never changes, it can be defined in the Properties window. If items must be added to the list during program execution, the Items.Add or Items.Insert method in an event procedure is used.
24
Formulating Algorithms: Counter-Controlled Repetition
Pseudocode Algorithm with Counter-Controlled Repetition Let’s use pseudocode to list the actions to execute and specify the order of execution for calculating the class average. After the user enters the grades and presses the Calculate Average Button, we use counter-controlled repetition to get the grades from the ListBox and process them one at a time. This technique uses a variable called a counter (or control variable) to specify the number of times that a set of statements will execute. This is also called definite repetition because the number of repetitions is known before the loop begins executing.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.