Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
CS0004: Introduction to Programming Repetition – Do Loops.
Programming in Visual Basic
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.
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.
Control structures Part 2 iteration control To enable repetition of a statement block.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Apply Sub Procedures/Methods and User Defined Functions
Chapter 4 The If…Then Statement
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
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.
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
ENGR 112 Decision Structures.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Programming Test #1 Solutions. Multiple Choice 1. B) the grammar of the coding language 2. C) String 3. A) Single 4. C) 2Burgers4Me 5. B) Design Time.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
Tutorial 6 The Repetition Structure
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson.
Sub Procedures. A Sub procedure is a block of code that is executed in response to an event. There are two types of Sub procedures, general procedures.
6.2 For…Next Loops General Form of a For…Next Loop
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs.
String and General Procedures. Answer to the last question of Exam 1 Start Get a number Divide the Number By 2 Is the quotient Equal to 1? Print The Remain.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Arrays and others. Annoucement Today’s office hour move to Friday 1:00PM to 3:00PM Today’s office hour move to Friday 1:00PM to 3:00PM Today Today  Call.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
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.
Data and variables in Visual Basic. Annoucement Lecture on Thursday 7:30PM C106 Visual Basic download: 
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Selection Using IF THEN ELSE CASE Introducing Loops.
Program design Program Design Process has 2 phases:
UNIT 5 Lesson 15 Looping.
while Repetition Structure
Visual Basic 6 (VB6) Data Types, And Operators
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
3rd prep. – 2nd Term MOE Book Questions.
Chapter 4 – Control Structures Part 1
3rd prep. – 2nd Term MOE Book Questions.
Structured Program
Case & Repetitive Statements
SSEA Computer Science: Track A
ASP control structure BRANCHING STATEMENTS
COMPUTING.
Presentation transcript:

Loop and repetition

Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth

Passing value to a sub procedure Private Sub Add(num1 As Single, num2 As Single ) Call Add(x,y) The values of x and y are copied to num1 and num2

Passing value(s) back from sub procedure As in the previous example, if the variable num1 is changed in the Add sub procedure, will the variable x be changed? The value of the arguments will be copied back to the caller sub procedure. This provides a tunnel for outputting several results. –Private Sub Phasor(real as single, image as single, modulus as single, phase as single) –Call Phasor(x,y,norm,angle)

Complex number to its phasor form Private Sub Phasor(real as single, image as single, modulus as single, phase as single) ‘convert a complex number to its ‘phasor form modulus = sqr(real^2 + image^2) phase=atn(image/real) End Sub

What is really happened in the memory VB is not really copying back and forth. When passing the argument to the sub prodedure, it is actually the memory block of the variable that is passed. It is called passing by reference. (for more information, read book pp )

Option Buttons Have value of true (clicked) or false (unclicked) Use If statements to evaluate Only one can be true If button.Value Then –Tests for TRUTH (selected) Read pp

Using Option Buttons

Code If Fahrbutton.Value Then statements Elseif Celsiusbutton.Value Then statements Else statements End If

Code for Program Private Sub Convert_Click() Outpic.Cls 'Decare DegC and DegF as singles Dim DegC As Single, DegF As Single If (Fahrbutton.Value = True) Then 'Get value for Celsius Temp DegF = Val(inBox.Text) 'Calculate DegF from DegC DegC = (DegF - 32) / 1.8 'Output answer Outpic.Print DegF; " degrees F equals" Outpic.Print DegC; " degrees C. " Else … End If End Sub

Example Start Get a number Divide the Number By 2 Is the quotient Equal to 1? Print The Remainder to the left of previous remains No Print 1 To the left of Previous remainders End Yes Output number If number Equal to 1 or 0 Yes No

Convert the first decision to code IF number <> 0 OR number <> 1 THEN Do the conversion part END IF Call OutputNumber(number)

Convert second decision to code The “NO” branch includes the decision itself. IF quotient <> 1 THEN quotient = number \ 2 reminder = number mod 2 number = quotient call PrintReminder() IF quotient <> 1 THEN quotient = number \ 2 reminder = number mod 2 number = quotient call PrintReminder() IF quotient <> 1 THEN ………..

Math operator \ and mod The \ (backward slash) operator is used to divide two numbers and return an integer result. – 5 \ 2 = 2, 10 \ 3 = 3 –5 / 2 =2.5, 10 / 3 = The mod operator will return the reminder of the division. –5 mod 2 =1, 10 mod 3 =1

Two solutions Use Goto key word for unconditional jumping. –Using goto is a bad habit for programming. Use loop structure –Do while …loop –For…Next

Repetition Along with decisions (if-then-else), repetition is the other key to making programs working Do procedure over and over

Do While Do While condition Code goes here Loop When condition becomes false, loop ends

Code fragment for the example Do While quotient <> 1 quotient = number \ 2 reminder = number mod 2 number = quotient call PrintReminder() Loop

Exponential Population Growth Population increases by a reproductive FACTOR each year (r)

Exponential Growth A bacteria divides in two at each event. How many bacteria are there after four reproductive events? 1 * 2  2 * 2  4 * 2  8 * 2  16

Population Growth r = growth factor or rate ( growth ) N = population ( pop ) Change in population = growth rate * population Population = Population + Change pop = pop + growth * pop

Population Growth Two variables –pop for population –growth for growth rate Repeat equation until pop = 1000 –pop = pop + growth * pop

Do While Loop Flowchart Start Declare Variables Pop as Single Counter as Integer Do While Pop < Finish Initialize Variables Pop = 10 Read growth from Text1.Text Clear Picture1 Pop = Pop + growth * pop Output Population Pop < is FALSE T

Sub Procedure Private Sub Command1_Click() Dim pop As Single, growth As Single pop = 10 growth = Val(Text1.Text) Picture1.Cls Do While pop < pop = pop + growth * pop Picture1.Print pop Loop End Sub

For-Next Loops For-Next loops are used to do an operation a specific number of times Want to do it ten times –use a For-Next Want to do it until number > 10000? –Use a Do While

Syntax of For-Next Loop Dim counter As Integer For counter = 1 to 10 statements go here Next counter

Population Growth Instead of until population = 10000, let’s just let it run for 10 times and see what the maximum number we get is

For Loop Flowchart Start Declare Variables Pop as Single Counter as Integer For Counter = 1 to 10 Finish Initialize Variables Pop = 10 Read growth from Text1.Text Clear Picture1 Pop = Pop + growth * pop Output Population Counter > 10 Next Counter (Counter = Counter + 1)

Sub Procedure Private Sub Command1_Click() Dim pop As Single, growth As Single Dim counter As Integer pop = 10 growth = Val(Text1.Text) Picture1.Cls For counter = 1 To 10 pop = pop + growth * pop Picture1.Print pop Next counter End Sub