Microsoft Visual Basic 2005 BASICS yo Microsoft Visual Basic 2005 BASICS Lesson 8 If Statements
Objectives Explain how decisions are made in a computer. yo Objectives Explain how decisions are made in a computer. Use the conditional operators. Use If statements to make decisions in programs. Create and read basic flowcharts. Microsoft Visual Basic 2005 BASICS
Objectives (cont.) Use If…Else statements. Use check box controls. yo Objectives (cont.) Use If…Else statements. Use check box controls. Use the logical operators. Microsoft Visual Basic 2005 BASICS
The Building Blocks of Decision Making yo The Building Blocks of Decision Making Programs are limited without the ability to make decisions. Programs must have a means to make decisions. Decision making in computers Generally done in terms of a comparison that returns a True or False response Microsoft Visual Basic 2005 BASICS
The Building Blocks of Decision Making (cont.) Microsoft Visual Basic 2005 BASICS
Using the Conditional Operators yo Using the Conditional Operators The first step to making a decision in a program is to make a comparison. Comparisons are made using the conditional operators. Boolean variables Can be used to store the results of a conditional expression Microsoft Visual Basic 2005 BASICS
Using the Conditional Operators (cont.) Microsoft Visual Basic 2005 BASICS
Using the Conditional Operators (cont.) Microsoft Visual Basic 2005 BASICS
Using If Statements If statement yo Using If Statements If statement Allows you to execute specified code when the result of a conditional expression is True The End If statement marks the end of the If statement. Microsoft Visual Basic 2005 BASICS
Using If Statements (cont.) Code in between the If statement and End If statement is executed if the conditions specified in the If statement are true. Otherwise, the code is skipped. Microsoft Visual Basic 2005 BASICS
Creating and Reading Flowcharts yo Creating and Reading Flowcharts Flowcharts Help you to plan and document program code There are many symbols used in creating flowcharts. The rectangle represents processing data. Use the diamond for making decisions. Use the parallelogram to represent input and output. Microsoft Visual Basic 2005 BASICS
Creating and Reading Flowcharts (cont.) Microsoft Visual Basic 2005 BASICS
Creating and Reading Flowcharts (cont.) Microsoft Visual Basic 2005 BASICS
Using If…Else Statements yo Using If…Else Statements A one-way selection structure Program structure in which the decision is to go “one way” The If…Else statement Allows you to choose between two paths Microsoft Visual Basic 2005 BASICS
Using If…Else Statements (cont.) In an If…Else statement One block of code is executed if the result of an expression is True. Another block is executed if the result is False. Microsoft Visual Basic 2005 BASICS
Using If…Else Statements (cont.) Microsoft Visual Basic 2005 BASICS
Using If…Else Statements (cont.) Microsoft Visual Basic 2005 BASICS
yo Using Check Boxes Check boxes are an important part of the Windows interface. Check boxes allow the programmer to Ask the user a Yes or No question Turn an option on or off Microsoft Visual Basic 2005 BASICS
Using Check Boxes (cont.) Microsoft Visual Basic 2005 BASICS
Checked Property Each check box has a Checked property. yo Checked Property Each check box has a Checked property. Set to True if the box is checked Set to False if the box is not checked A check box has an integrated label. Appears beside it Text property specifies its label Microsoft Visual Basic 2005 BASICS
Checked Property (cont.) Microsoft Visual Basic 2005 BASICS
Setting the Default Checked Property Check boxes can be set to be either checked or unchecked by default. Use the Checked property Microsoft Visual Basic 2005 BASICS
Setting the Checked Property with Code yo Setting the Checked Property with Code The Checked property can be set and/or read from code. Example chkHeart.Checked = True Microsoft Visual Basic 2005 BASICS
Setting the Checked Property with Code (cont.) Microsoft Visual Basic 2005 BASICS
Using the Logical Operators yo Using the Logical Operators Logical operators Can be used to combine several comparisons into one statement Used with True and False values Microsoft Visual Basic 2005 BASICS
Using the Logical Operators (cont.) Microsoft Visual Basic 2005 BASICS
Order of Logical Operators yo Order of Logical Operators Logical operators Processed after the mathematical and conditional operators Logical operators are processed in this order Not And Or Microsoft Visual Basic 2005 BASICS
Order of Logical Operators (cont.) Microsoft Visual Basic 2005 BASICS
yo Summary Decisions are reached by making comparisons. Comparisons in a computer generally return either a True or False value. The conditional operators compare two values and return either True of False, depending on whether the expression is True or False. Microsoft Visual Basic 2005 BASICS
Summary (cont.) A Boolean variable can be used to store the results of an expression that includes conditional operators. The If statement is the most common way to make a decision in a program, and it is a one-way selection structure. The code between the If and the End If is executed if the conditions in the If statement are met. Microsoft Visual Basic 2005 BASICS
Summary (cont.) Flowcharts allow programmers to plan and document program code using symbols connected by lines. An If…Else statement makes a decision between two paths, and it is a two-way selection structure. Microsoft Visual Basic 2005 BASICS
Summary (cont.) Check boxes allow your program to ask the user Yes or No questions or to turn an option on or off. The Checked property of a check box is set to True when the check box is checked and False when the check box is not checked. A check box can be set to be checked or unchecked by default. Microsoft Visual Basic 2005 BASICS
Summary (cont.) Logical operators can be used to combine several comparisons into one statement. They are used with True and False values. The Not operator reverses the value of a Boolean variable or expression. The And operator returns True if the values connected by the And operator are both True. Microsoft Visual Basic 2005 BASICS
Summary (cont.) The Or operator returns True if either value connected by the Or operator is True. The logical operators are last in the order of operations. Of the logical operators, Not comes first, then And, then Or. Microsoft Visual Basic 2005 BASICS