COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.

Slides:



Advertisements
Similar presentations
5.04 Apply Decision Making Structures
Advertisements

DECISION MAKING STRUCTURES Computer Programming 2.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
C++ for Engineers and Scientists Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
5.05 Apply Looping Structures
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Apply Sub Procedures/Methods and User Defined Functions
Microsoft Visual Basic 2008: Reloaded Fourth Edition
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Chapter 4 The If…Then Statement
Decision Structures and Boolean Logic
Computer Science Selection Structures.
COMPUTER PROGRAMMING I Objective 7.03 Apply Built-in Math Class Functions.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
Lecture Set 5 Control Structures Part A - Decisions Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
Flow of Control Part 1: Selection
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
5.04 Apply Decision Making Structures
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
Decision Statements, Short- Circuit Evaluation, Errors.
Controlling Program Flow with Decision Structures.
Controlling Program Flow with Decision Structures.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
CPS120: Introduction to Computer Science Decision Making in Programs.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Computer Science Up Down Controls, Decisions and Random Numbers.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Welcome to Computer Programming II! Computer Programming II Summer 2015.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Chapter 4: Decisions and Conditions
Use TryParse to Validate User Input
5.03 Apply operators and Boolean expressions
5.04 Apply Decision Making Structures
Chapter 4: Decisions and Conditions
Objective 7.04 Apply Built-in String Functions (3%)
Objective 7.03 Apply Built-in Math Class Functions
Chapter 4 The If…Then Statement
UNIT 4 Lesson 13 If statements.
Control Structures: Part 2
Use TryParse to Validate User Input
Topics The if Statement The if-else Statement Comparing Strings
Chapter 5 – Control Structures: Part 2
CHAPTER FIVE Decision Structures.
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Microsoft Visual Basic 2005: Reloaded Second Edition
Presentation transcript:

COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures

Objective/Essential Standard Essential Standard: Apply Programming and Conditional Logic Indicator: 5.04 Apply decision-making structures. (3%)

What is a decision? Decisions are made by humans hundreds of times a day. Computers can make simple decisions also. What are some decisions you made today so far?

Boolean Logic Remember, from the previous section Boolean logic? (Ex: intNum = 5) Computers use it to make decisions! There are only two answers: TRUE or FALSE Computers do not know about Maybe!

The If Decision Structure

Decisions… Decisions So when you got up this morning what did you do?  This is a decision. Let’s put it in computer terms. IF the sun is out then I will walk to school

The If…Then Statement If…Then is a decision structure that executes a set of statements when a condition is true. Form: If condition Then Statements End If T

The If…Then Statement IF the sun is out (question) THEN I will walk to school. Remember the computer uses Boolean logic (T/F). So either the sun is out (true) or ANY other state (false). No maybes or in-betweens. When the question is True, the statements after THEN (and down to ENDIF) execute. They are skipped if the question is False.

IF..THEN..ELSE Statement In Visual Studio we use the IF..THEN..ELSE statement to tell the computer we want it to make a decision. For example If a = b then c= 10 Else c=13 End If T F

Use of the ELSE Else is optional- if omitted and the “question” is false no action is taken. If ELSE is used, the statements after the ELSE will execute if the “question” is NOT true (Boolean FALSE)

Nested If…Then…Else Statements If…Then…Else statement within an If…Then…Else statement is called Nested. Example: If intNum = 10 Then Me. lblMsg.text = “Ten” Else If IntNum > 10 Then Me.lblMsg.text = “More than 10” Else Me. lblMsg.text = “Less than 10” End If T F T F

The If…Then…ElseIf Statement An IF statement can have multiple else statements- each one a new question or condition to check. Also called an ElseIf Ladder The computer executes the statements below the first (or only) true condition and immediately exits the whole statement (ignores anything below the true statements).

The If…Then…ElseIf Statement Used to decide among three or more actions Form: If condition Then Statements ElseIf condition Then Statements ElseIf condition Then Statements … Else Statements End If Last Else clause is optional – executes when no True above.

If…Then…ElseIf Example If a = b Then c = 10 ElseIf a > b Then c = 14 ElseIf a < b Then c = 16 Else c = 12 End If

If…Then…ElseIf Example #2 If strStudentClass = “Senior” Then lbl.Answer.Text = “Seniors - start 1 hour late Fri” ElseIf strStudentClass = “Junior” Then lbl.Answer.Text = “Juniors - start 30 min late Fri” ElseIf strStudentClass = “Sophomore” Then lbl.Answer.Text = “Sophomores - start on time Fri” ElseIf strStudentClass = “Freshman” Then lbl.Answer.Text = “Freshmen - start 15 min early Fri” End If

The Select Decision Structure

Select Case Decision Another way to make a decision is a select case statement. This statement is used instead of multiple else if statements. The computer again executes the first (or only) true statement and ignores the rest of the statement.

Select Case Decision Decision structure that uses the result of an expression to determine which block of code to execute. FormExample Select Case expression Select Case intScore Case value Case 0,10 StatementsStatements…Case Else End Select End Case

Select Case Expression must evaluate to a built-in data type.  Integer, Double, Character, String… There can be multiple Case clauses. Case Else clause is optional Value type should match the expression type and can be  a single value,  Case 2  a list separated by commas, or  Case 1, 2, 3  a range separated by the keyword To.  Case 1 To 5

Case Is Compares the result of an expression to a range of values when a relational operator is part of the value. Must use Case Is with relational operators. Example: Case Is < 10

Select Case Example Select Case intGrades Case 100 strGrade= “A+” Case 90 To 99 strGrade= “A” Case 80 To 89 strGrade=“B” Case 70 To 79 strGrade=“C” Case 60 To 69 strGrade=“D” Case Is < 60 strGrade=“F” Else Messagebox.show(“Please input a valid number!”) End Select

If..Then vs. Select Case In most cases If..Then is good for a single decision and a Select Case (or If Elseif) is correct to use when there are multiple possible answers with only one correct answer. In the following example, remember the.Checked property of a radiobutton holds the values True or False! A statement such as AddMachine() is a block of code called a Function/Method that runs when its name is executed (like the Convert.ToDouble(..) Function). We will be writing them soon!

Which Radio Button is Clicked? Select Case True Case radAdd.Checked AddMachine() Case radMultiply.Checked MultipleMachine() Case radDivision.Checked DivisionMachine() Case Else Messagebox.Show(“Pick a button!”) End Select

Sample Program Write a program that allows the user to input a number. The computer should tell the user if that number is smaller, greater or equal to 15.

Sample Program Solution Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click Dim number As Integer Try number = Convert.ToInt32(txtNumber.Text) If number = 15 Then MessageBox.Show("The number is 15") ElseIf number < 15 Then MessageBox.Show("The number is less than 15") ElseIf number > 15 Then MessageBox.Show("The number is greater than 15") End If Catch ex As Exception MessageBox.Show(“Enter a numeric value”) End Try End Sub

Compound Boolean Expressions

Logical Operators Logical Operators are used to create compound Boolean expressions. Use logical operators And or Or to form a Boolean expression. Logical operators join two expressions to create an expression that evaluates to either True or False. Third operator is Not. The order of precedence is Not, then And, then Or.

Compound Boolean Expressions Use to check multiple values in a single statement. Compound expressions resolve to a single True or False. For example: If intNum1 = 15 And intNum2 > 2 Then statements End If

Compound Boolean Expressions - AND T AND T evaluates to T T AND F evaluates to F When using And or & with IF, both (all) conditions MUST be true for the entire IF (or else if) question to evaluate to true. If number 5 Then … If the number is not less than 15 AND greater than 5 this statement is false.

Compound Boolean Expressions - OR If number 5 Then Statements End If Now if any one of the conditions are true, the statement evaluates to true. T Or F evaluates to T A truth table shows the possible outcomes of the Boolean Expressions.

And Truth Table Expression1Expression2Boolean Expression True False TrueFalse Expression1 And Expression2

Or Truth Table Expression1Expression2Boolean Expression True FalseTrue FalseTrue False Expression1 Or Expression2

Advanced If Statements

Short Circuiting VB provides a way to “short circuit” long compound IF statements.  The computer looks at the first statement and uses that information to decide if it needs to look at the rest of the statement. AndAlso If intNum1 > 0 AndAlso intNum2 > 0 Then  So if inNum1 is not greater than 0 the first Boolean expression is false and the computer does not waste time looking at num2. OrElse If intNum1 > 0 OrElse intNum2 > 0 Then  If intNum1 is greater than 0 there is no need to check intNum2 because the first Boolean expression is true.

Using IF in Assignment Statements An IF can also be used in assignment statements to set the value of a variable. Example: Dim varName As Data Type = If(comparison, value if true, value if false) Example: 'Assign a value to the string strName based on the comparison given. Dim strName As String = If(a < b, "Less than 10", "Greater than 10")

Randomizing a Number

To generate a random integer, we can use the System Random class. First create your Random Number Generator Dim gen As New System.Random() You create the name (gen, numGen,…) to match the purpose of the generator. Use the next or nextDouble methods to generate the random numbers.

Randomizing a Number NextDouble  Returns a random number between 0.0 and 1.0.  Example: dblRndNum = gen.NextDouble Generates a number between 0.0 and up to, but not including 1.0+ Next  Returns a nonnegative random number between 0 and the MaxValue (2,147,483,647)  Example: intRndNum = gen.Next Generates a number between 0 and up to, but not including 2,147,483,647

Randomizing a Number Next (Int32)  Returns a nonnegative random number less than the specified maximum  Example: intRndNum = gen.Next(10) Generates a number between 0 and up to (but not including) 10 Next (Int32, Int32)  Returns a random number within a specified range  Example: intRndNum = gen.Next(0, 50) Generates a number between 0 and up to (but not including) 50

Random Numbers Example

Vocabulary & Code Logical Operators Compound Boolean Expression Truth Table Short Circuiting If..Then If..Then..Else If..Then..ElseIf Select..Case If..AndAlso If..OrElse

Conclusion This PowerPoint provided an overview of decision making statements in Visual Studio. For more information on Decision Statements  us/library/752y8abs(v=vs.100).aspx us/library/752y8abs(v=vs.100).aspx  us/library/cy37t14y(v=vs.100).aspx us/library/cy37t14y(v=vs.100).aspx For more information on Random  us/library/system.random.aspx us/library/system.random.aspx