Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Problem Solving and Control Statements.

Similar presentations


Presentation on theme: "Introduction to Problem Solving and Control Statements."— Presentation transcript:

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.

4 Writing Visual Basic Projects 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. Planning ◦ Design the User Interface. ◦ Plan the Properties. ◦ Plan the Basic Code; follow the language syntax rules; use pseudocode Programming (and use the same three-step process) ◦ Define the User Interface. ◦ Set the properties. ◦ Write the Basic code. 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 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 algorithms are not executed on computers.

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

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

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

10

11 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

12 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

13 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

15 Control Structures - Repetition Repetition Statements Visual Basic provides seven types of repetition statements (also called looping statements or loops) that enable programs to perform statements repeatedly based on the value of a condition. ◦ The Do While…Loop and While…End While repetition statements execute a set of statements while a condition—known as the loop- continuation condition—remains true. If the condition is initially false, the set of statements does not execute. ◦ The Do Until…Loop repetition statement executes a set of statements until a condition—known as the loop-termination condition—becomes true. If the condition is initially true, the set of statements does not execute. ◦ The Do…Loop While repetition statement executes a set of statements while its loop-continuation condition remains true. The set of statements is guaranteed to execute at least once.

16 Control Structures - Repetition ◦ The Do…Loop Until repetition statement executes a set of statements until its loop-termination condition becomes true. The set of statements is guaranteed to execute at least once. ◦ The For…Next repetition statement executes a set of statements a specified number of times—this is known as counter-controlled (or definite) repetition. ◦ The For Each…Next repetition statement (introduced in Chapter 7) performs a set of statements for every element of a so-called array or collection of values.

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

18 The Do and Loop Statements — General Form Do {While |Until} condition ' Statements in loop. Loop --OR-- Do ' Statements in loop. Loop {While | Until} condition Top of Loop Condition, Pretest/Entry test Bottom of Loop Condition, Posttest/ Exit

19 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

20 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 <= 100 product = product * 3 ' compute next power of 3 Loop ◦ While product <= 100 product = product * 3 ' compute next power of 3 End While

21

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

23 ListBoxes and ComboBoxes (1 of 2) 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

24 List Boxes and Combo Boxes (2 of2) Various Styles of List and Combo boxes

25 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

26 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

27

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

29

30 Formulating Algorithms: Counter- Controlled Repetition The String class’s Format method performs the formatting. The first argument to the method ( "{0:F}" ) is the format string, which acts as a placeholder for the value being formatted. The numeric value that appears before the colon (in this case, 0 ) indicates which of Format ’s arguments will be formatted— 0 specifies the first argument after the format string passed to Format

31 Formulating Algorithms: Counter- Controlled Repetition The value after the colon (in this case, F ) is known as a format specifier, which indicates how a value is to be formatted. The format specifier F indicates that a fixed-point number should be rounded to two decimal places by default. You can change the number of decimal places to display by placing an integer value after the format specifier—for example, the string "{0:F3}" rounds the number to three decimal places.

32 Using the Debugger: Locating a Logic Error Visual Studio has tools to help you find and correct logic errors (also called bugs). Such errors do not prevent a program from compiling successfully, but can cause a running program to produce incorrect results. Visual Studio includes a tool called a debugger that can be used to monitor the execution of your programs so you can locate and remove logic errors. A program must successfully compile before it can be used in the debugger.

33 Using the Debugger: Locating a Logic Error Some of the debugger’s features include: ◦ suspending program execution so you can step through the program one statement at a time ◦ examining and setting variable values ◦ watching the values of variables and expressions ◦ viewing the order in which methods are called


Download ppt "Introduction to Problem Solving and Control Statements."

Similar presentations


Ads by Google