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.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Select Case Statements and Selection Input.
Advertisements

5.04 Apply Decision Making Structures
1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
30/04/ Selection Nested If structures & Complex Multiple Conditions.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
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.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
JavaScript, Third Edition
C++ for Engineers and Scientists Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter 5 new The Do…Loop Statement
Visual Basic Chapter 1 Mr. Wangler.
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Chapter 4 The If…Then Statement
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
CHAPTER FIVE Specifying Alternate Courses of Action: Selection Statements.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
JavaScript, Fourth Edition
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
31/01/ Selection If selection construct.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Controlling Program Flow with Decision Structures.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Visual Basic/ Visual Studio Brandon Large. Connecting to prior knowledge In your notes write down what the two main parts of the computer are. The “software”
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
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.
© 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
A variable is a name for a value stored in memory.
Chapter 4 The If…Then Statement
Visual Basic 6 (VB6) Data Types, And Operators
UNIT 4 Lesson 13 If statements.
CHAPTER FIVE Decision Structures.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4: Making Decisions.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
CHAPTER FIVE Decision Structures.
Chapter 3: Introduction to Problem Solving and Control Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
CHAPTER FOUR VARIABLES AND CONSTANTS
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

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 is True. Takes the form: If condition Then statements End If

If…Then cont… Condition = What the computer evaluates to be either True or False Statements = What the computer does if the condition is True Example: If intGuess = 7 Then Me.lblMessage.Text = “You guessed it!” End If

Relational Operators Relational Operators can be used to form Boolean expressions (remember Boolean is either True or False) OperatorMeaning =Equal to <Less than <=Less than or equal to >Greater than >=Greater than or equal to <>Not equal to

If…Then…Else Statement Includes an optional Else clause that is executed when the If condition evaluates to False. Takes the form: If condition Then statements Else statements End If

Nested If…Then…Else Statement An If…Then…Else statement can contain another If…Then…Else or If…Then statement, which is said to be nested. Nested statements should be indented for good programming style. Continued on next page..

Nested If…Then…Else Statement cont… Takes the form: If condition Then statements Else If condition Then statements Else statements End If

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

The If…Then…ElseIf Statement cont… There can be multiple ElseIf clauses, and the last Else clause is optional. Comments are a must to keep complicated code much more readable. This is especially important for the last branch of a decision structure which usually does not include an explicit condition.

The Select…Case Statement The Select…Case Statement is a decision structure that uses the result of an expression to determine which block of code to execute. The Select…Case Statement is sometimes preferable to the If…Then…ElseIf statement because code may be easier to read.

Select…Case Statement cont… Select…Case takes the form: Select expression Case value statements … Case Else statements End Select

Example of Select…Case Example: Select Case intScore Case 0, 10‘Score is 0 or 10 statements Case 20 To 25‘Score is 20,21,22,23,24 statementsor 25 Case Else‘Score other than 0,10,20, statements21,22,23,24 or 25 End Select

Select…Case Statement cont… The Select…Case must evaluate to a built-in data type. Ex. Integer, single, … Their can be multiple Case clauses, and the Case Else clause is optional. IMPORTANT: The value type should match the expression type and can be a single value, a list separated by commas, of a range separated by the keyword To.

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. Example: Select Case intScore Case Is < 10 statements Case Is < 25 statements Case Is >= 25 statements End Select

Generating Random Numbers The phrase Randomize() must be included at the beginning of the procedure that is using the random number. To generate a random number use the built in Rnd() function The formula for a random number is: (High Number – Low Number) * Rnd() + Low Number Which is set equal to some variable.

Message Box Can be displayed to alert the user of invalid data or as a reminder of options required for an application to continue. How to code a message box? MessageBox.Show(“message”,”title”) The “title” is optional.

Counter Variables A counter is a variable storing a number that is incremented by a constant value. Counters are useful for keeping track of the number of times a user clicks a button, enters a guess, or types a password. Updating a counter takes the form: counter = counter + constant

Counter Variables cont… Counter is the numeric variable that is updated. Constant is the number that is added to the current value of counter. A counter should be initialized when it is declared and then updated by an unchanging amount.

Static Variables Anytime a counter variable is declared it should be declared as a static variable instead of dimensional. A variable declared as a local variable is redeclared every time through the event procedure unless it is declared as a static. Ex: Static intCounter As Integer

Static Variables cont… Extends the lifetime of a local variable. The value of a static variable is not redeclared and reinitialized each time the Click event occurs. Useful when assigning random numbers to variables.

The CheckBox Control Name = prefix = chk Text = text displayed next to the box Checked = can be set to either True or False to display the check box with or without a check, respectively. Related check boxes are sometimes placed together in a GroupBox object. As with radio buttons, a group box should be added to the form before adding check boxes.

Line-Continuation Character Statements that are long can be typed onto two or more lines when the line- continuation character is used. The underscore ( _ ) is the line- continuation character. Must have a space before it and nothing after it and cannot occur within quotation marks.

Logical Operators A Boolean expression can be formed using the logical operators And and Or. A logical operator joins two expressions to create an expression that evaluates to either True or False. A third logical operator used is Not. Not changes an expression either from True to False or from False to True.

Logical Operators cont… Logical Operators And Or Expression1 Expression2 Result True True True True True True True False False True False True False True False False True True False False False False False False Not Expression Result True False False True

String Concatenation Two or more strings can be joined together in a process call concatenation. The & operator can be used to concatenate string. The & operator is used in an expression similar to the following: newString = string1 & string2 newString is a String variable that will store the result of the expression. string1 and string2 are String variables or string enclosed in quotation marks.

& cont… Example Dim strFirstName As String Dim strLastName As String Dim strFullName as String strFirstName = “Al” strLastName = “Bundy” strFullName = strFirstName & “ “ & strLastName StrFullName will = Al Bundy

& cont… Concatenation can also be used to combine two labels into one. Ex: Me.lblMessage.text = “Your answer is “ & sngAnswer & “ inches”