Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
5.04 Apply Decision Making Structures
1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Microsoft Visual Basic 2008: Reloaded Fourth Edition
CIS 115 Lecture 7 Selection / Decisions.
Chapter 4 The If…Then Statement
Decision Structures and Boolean Logic
Computer Science Selection Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture Set 5 Control Structures Part A - Decisions Structures.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Flow of Control Part 1: Selection
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
CONTROLLING PROGRAM FLOW
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
22/11/ Selection If selection construct.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
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.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 1 Unit 4 Decisions Chapter 4 Making Decisions and Working.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CPS120: Introduction to Computer Science Decision Making in Programs.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Computer Science Up Down Controls, Decisions and Random Numbers.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
5.04 Apply Decision Making Structures
Chapter 5 The if Statement
Chapter 4 The If…Then Statement
Visual Basic 6 (VB6) Data Types, And Operators
UNIT 4 Lesson 13 If statements.
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Chapter 3: Introduction to Problem Solving and Control Statements
Introduction to Problem Solving and Control Statements
Chapter 3: Selection Structures: Making Decisions
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition is true  The condition is a Boolean expression  The condition takes the form: If condition Then statements End If

Slide 2 If… Then Statement… For example, the statement: If intX = 5 Then intY = 20 End If assigns the value 20 to y only if x is equal to 5. Example 2: If intGuess = 7 Then Me.lblMessage.Text = “You guessed it!” End If

Slide 3 Chapter 4 Relational Operators – can be used to form Boolean expressions OperatorMeaning = equal to < less than <= less than or equal to > greater than >= greater than or equal to <> not equal to

Slide 4 Review: TestGrade – Part 1 of 5

Slide 5 Chapter 4 The If…Then…Else Statement Contains an Else clause that is executed when the If condition evaluates to false. Takes the form: If Condition Then Statements Else Statements End If

Slide 6 If…Then…ElseIf…Then…Else For example, the statement If intX = 5 Then intY = 20 Else intY = 10 End If assigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5. Example2 – If intGuess = int SECRECTNUMBER Then Me.lblMessage.Text = “You Guessed It!” Else Me.lblMessage.Text = “Try again” End If

Slide 7 Review: TestGrade – part 2 of 5 Review: Circle Area – part 2 of 2

Slide 8 Chapter 4 Nested If…Then…Else Statements Should be indented to make the logic clear. An If… Then.. Else statement can contain another If..Then… Else or If…Then statement Nested statement executed only when the branch it is in is executed. For example, the statement If intX = 5 Then intY = 20 Else If intX > 5 Then intY = 10 Else intY = 0 End If End If evaluates the nested If…Then…Else only when x is not equal to 5.

Slide 9 Nested… If…Then…Else Statements Example 2: If intGuess = intSECRETNUMBER Then Me.lblMessage.Text = “You guessed it!” Else If intGuess < intSECRETNUMBER Then Me.lblMessage.Text = “Too low” Else Me.lblMessage.Text = “Too hight.” End If * Nested statements should be indented…. As it is good programming style.

Slide 10 Chapter 4 The If…Then…ElseIf Statement   Used to decide among three or more actions. Takes the form: If condition Then statements ElseIf condition Then statements Else statements End If There can be multiple ElseIf clauses and the last Else clause is optional.

Slide 11 Chapter 4 The If…Then…ElseIf Statement  Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement If intX < 5 Then intY = 20 ElseIf intX < 10 Then intY = 40 ElseIf intX < 15 Then intY = 80 End If would give very different results if the conditions were ordered differently.

Slide 12 The If…Then…ElseIf Statement Example 2: If intGuess = intSECRETNUMBER Then Me.lblMessage.Text = “You guessed it!” ElseIf intGuess < intSECRETNUMBER Then Me.lblMessage.Text = “Too low” Else Me.lblMessage.Text = “Too high.” End If

Slide 13 Review: TestGrade part 3 of 5

Slide 14 Chapter 4 The Select…Case Statement  The result of an expression determines which statements to execute.  The Case Else code is optional and is executed when none of the previous cases are met: Select…Case takes the form: Select expression Case value statements Case Else statements End Select

Slide 15 Chapter 4 The Select…Case Statement Example 1: Select Case intScore Case 0, 10 ‘score is 0 or 10 Me.lblMessage.Text = “Nice Try" Case 20 To 23 ‘score is 20, 21, 22, or 23 Me.lblMessage.Text = “Great!" Case Else Me.lblMessage.Text = “Invalid Score" End Select

Slide 16 Review: Hurricane pg 97

Slide 17 Chapter 4 The Select…Case Is Statement  Compares the result of an expression to a range of values when a relational operator is part of the value. For example: Select Case score Case Is = 25 Me.lblMessage.Text = "Great!" End Select

Slide 18 Review: Test Grade part 4 of 5 Pg 98

Slide 19 Chapter 4 The Rnd() Function  Uses a formula to generate a sequence of numbers that are each greater than 0 and less than 1 and then returns one number from the sequence.  A random integer in a range is generated by using the formula: (highNum – lowNum + 1) * Rnd() + lowNum  Random integers are produced by using the Int() function along with the Rnd() function: Int(21 * Rnd() + 10)'10 to 30  The Randomize() statement initializes the random number generator.

Slide 20 Review: Random Numbers Pg 99

Slide 21 Chapter 4 Algorithms & Pseudocode  Algorithm -A set of steps that outline how to solve a problem.  Algorithm can be implemented in plain English or in a mix of English and program code called pseudocode.  Algorithms allow a programmer to think through a program before actually typing code, which may reduce errors in logic.

Slide 22 Algorithm Example (do not have to write) Algorithm for Number Guess Application: 1. Determine a secret number. 2. Get a number from the player. 3. Compare the player’s number with the secret number. 4. if the player’s number is the same as the secret number go to step 5, otherwise tell the player if the number entered was too low or too high and then go back to step Display a message telling the player the secret number was guessed.

Slide 23 Pseudocode for Number Guess Application: Sub btnCheckGuess_Click() intSecretNumber = 7 Get guess from text box If intGuess = intSecretNumber Then Display “You guessed it” ElseIf intGuess < intSecretNumber Then Display “Too low” Else Display “Too High” End If End Sub Pseudocode Example (do not have to write)

Slide 24 Chapter 4 Static Variables  Variables have a lifetime in which they exist in memory. Local variables: duration of the procedure in which it was declared. Global: is the duration of the program. Static Variables:  Declared with the keyword Static.  Have a lifetime the duration of the program's running time.  Used to extend the lifetime of local variables in a procedure.  Should be explicitly initialized when declared (have a value).  A better choice than a global variable because the scope of the variable should be as narrow as possible.

Slide 25 Complete Review: Guessing Game Part 1 of 4

Slide 26 Chapter 4 Compound Boolean Expressions  CBE – Use more than one Boolean expression in a single condition to determine if the condition is true or false.  Formed using logical operators: And, Or, or Not.

Slide 27 Chapter 4 And Truth Table And Exp1Exp2Result True False TrueFalse

Slide 28 Chapter 4 Or Truth Table Or Exp1Exp2Result True FalseTrue FalseTrue False

Slide 29 Chapter 4 Not Truth Table Not ExpResult TrueFalse True

Slide 30 Complete Review: Rock Paper Scissors Part 1 of 4 pg 105

Slide 31 Chapter 4 The MessageBox Class  A predefined dialog box that displays a message to the user.  Ex: Alert user of invalid data entered or a reminder of options required for an app to continue  Includes the Show() method for displaying the dialog box. For example: MessageBox.Show(message)

Slide 32 Complete Review: Guessing Game – Part 2 of 4 pg108 Complete Review: Test Grade – Part 5 of 5 pg 108

Slide 33 Chapter 4 Counter Variables  A variable that is incremented by a constant value.  Used for counting guesses, the numbers of values entered, the number of times a button was clicked, and so on.  The value of a counter is updated in a statement similar to: counter = counter + 1  Should be initialized when declared and updated by an unchanging amount.

Slide 34 Chapter 4 Assignment Operators OperatorOperation += addition and then assignment -= subtraction and then assignment Counters should be initialized when declared and declared as a Static variable so that it is initialized only once

Slide 35 Complete Review: Rock, Paper, Scissors Part 4 of 4 pg 109

Slide 36 Chapter 4 The CheckBox Control  (Name) should begin with chk.  Text is the text displayed next to the box.  Checked is set to True if the box should be displayed as checked. An If…Then statement is often used to determine if a check box is checked or cleared.

Slide 37 Chapter 4 Line-Continuation Character  The underscore character _ is the line-continuation character.  There must be a space before and nothing after and cannot be within quotation marks.  Used for dividing code to make it more readable.

Slide 38 Complete Review: Morning To Do Pg 110