ENGR 112 Decision Structures.

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
1 Chapter 6 Repetition. 2 Outline & Objectives Loop Structure Loop Structure Elements of a Loop Structure Elements of a Loop Structure Processing Lists.
5.05 Apply Looping Structures
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright by Ronald P. Kessler, Ph.D.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
Tutorial 6 The Repetition Structure
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Introduction to Problem Solving and Control Statements.
Repetition Structures
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Introduction to Programming Lecture 2 Msury Mahunnah, Department of Informatics, Tallinn University of Technology.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Introduction to Programming Lecture 2
UNIT 5 Lesson 15 Looping.
Visual Basic 6 (VB6) Data Types, And Operators
JavaScript: Control Statements I
Lists, Loops, Validation, and More
Control Structures: Part 2
3rd prep. – 2nd Term MOE Book Questions.
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements.
3rd prep. – 2nd Term MOE Book Questions.
Control Structures: Part 1
Making Decisions in a Program
Outline Altering flow of control Boolean expressions
حلقات التكرار.
Chapter (3) - Looping Questions.
Higher Computing Using Loops.
3 Control Statements:.
Introduction to Problem Solving and Control Statements
Problem Solving and Control Statements
Fundamentals of visual basic
Case & Repetitive Statements
Chapter 4 - Control Structures: Part 1
Visual Basic Programming
For...Next Statements.
Presentation transcript:

ENGR 112 Decision Structures

Control Structures Sequence structures Selection structures Built into Visual Basic Selection structures If/Then  Single selection If/Then/Else  Double selection Select Case  Multiple selection

Control Structures Repetition structures For/Next Do/While Loop

Selection Structures

Selection Structure Is Condition Met? NO YES Statements in Else Clause Statements in Then Clause

If/Then/Else Statement Allows us to execute one statement if TRUE and another if FALSE If A=0 Then msg = “ERROR...Division by ZERO” MsgBox(msg) Exit Sub Else C = B/A End If

If/Then/Else for checking To determine if user has entered a string in the from of a date If IsDate(variable) Then ‘do what you want with the date Else ‘provide a message to the user End If

Example Dim DT Private Sub Command1_Click() DT = Text1.Text If IsDate(DT) Then MsgBox "Good Job" Else MsgBox "please enter the text in the form of a date" End If End Sub

If/Then/Else for checking To determine if variable can be converted to a number If IsNumeric(variable) Then ‘do what you want with the number Else ‘provide a message to the user End If

Example Dim NT Private Sub Command2_Click() NT = Text2.Text If IsNumeric(NT) Then MsgBox "Good Job" Else MsgBox "please enter a number" End If End Sub

If/Then/Else Statement Nested conditional statements If A = 0 Then value = “x” Else If A=1 Then value = “y” If A=2 Then value = “z” End If

Select Case structure Practical when more than three levels of if/Then/Else are necessary Select Case inputNumber Case 0 value = “x” Case 1 value = “y” Case 2 value = “z” Case 3 value = “Null” End Select

Example Design a program to compute grades based on the average of 4 exams If the average is 90 or higher, the student gets an “A” If the average is 80 – 89, the student gets a “B” Etc.

If/Then/Else Pseudocode Version If Grade >= 90 Then YourGrade = “A” Else If Grade >= 80 Then YourGrade = “B” Else If Grade >= 70 Then etc

Case Pseudocode Select Case Grade Case is >= 90 YourGrade = “A” YourGrade = “B” Case is >= 70 YourGrade = “C” Etc.

Repetition Structures

Determinant Loops

For/Next Statement Also known as a For … Next loop Allows repetition of statements Syntax For CounterVar = StartNum to EndNum [Step StepNum] VB Statements Next CounterVar Optional Increments counter

Determinate Structure Set counter variable to starting value Is counter value greater than ending value? YES NO Increment counter variable Body of Loop Move to statements after loop

For/Next Statement Counter increments by 1 unless otherwise specified VB keyword Step is used to increment by a number other than 1 (including negative numbers) Can be terminated prematurely with keywords Exit For

E E For/Next Statement Examples For X=1 To 10 Next X Answ = 0 For X=1 To 10 Answ = Answ + X Next X For X=1 To 10 Step 2 If X > 10 Then Exit For E E

Indeterminate Loop

Do…Loop Statements Conditional loops are executed as long or until a condition exists Their key feature is the condition The condition can be A Boolean variable (True or False) Value of a property Expression (NumVal < 15)

Indeterminate Structure 1 Test at End of Loop Body of Loop Is condition met? YES Move to statements after loop NO

Do Loop – Test at End Example Dim password Do password= inputBox$(“Password please?”) Loop Until password = “Vanilla orange”

Indeterminate Structure 2 Test at Beginning of Loop Is condition met? YES Move to statements after loop NO Body of Loop

Do Loop –Test at beginning example Dim entry Dim NameCount As Integer Private Sub Command1_Click() NameCount = 0 entry = InputBox("Enter name") Do Until entry = "zzz" NameCount = NameCount + 1 Loop Print "The total number of names is "; NameCount End Sub

Do While Loop Do Loop Until variablename <> “” Loop While variablename = “” Loop Until variablename > 5 Loop While variablename <= 5 Equivalent Equivalent

Do...While/Loop Statement Example X = 10 Do While X > 0 Ht = (60 + 2.13) * X^4 Print Ht ‘Ht=Height of Rocket X = X - 1 Loop Condition Condition being affected

Problems with Loops What’s the problem with this loop? Dim i As Integer Do While i = 0 i = 0 Loop

Problems with Loops What’s the problem with this loop? Private Sub CmdDisplay_Click() Dim x As Single x = 1 Do While x > 0 x = x + 1 PicOutput.Print x Loop End Sub

Problems with Loops What’s the problem with this loop? Private Sub CmdDisplay_Click() Dim m As Single For m = 1 To 20.5 Step – 1 PicOutput.Print m Next m End Sub