The Step Parameter & Nested For … To … Next loops

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Introduction to Computing Science and Programming I
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
08/09/ Arrays Defining, Declaring & Processing.
For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
27/05/ Iteration Loops Nested Loops & The Step Parameter.
30/10/ Iteration Loops Do While (condition is true) … Loop.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items.
Controlling Program Flow with Looping Structures
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Chapter 6 Controlling Program Flow with Looping Structures.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Lesson #5 Repetition and Loops.
CprE 185: Intro to Problem Solving (using C)
COMPUTATIONAL CONSTRUCTS
Introduction To Repetition The for loop
Repetition Structures Chapter 9
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
Topics Introduction to Repetition Structures
Lesson #5 Repetition and Loops.
3rd prep. – 2nd Term MOE Book Questions.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 2.2 Control Structures (Iteration)
( Iteration / Repetition / Looping )
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
3rd prep. – 2nd Term MOE Book Questions.
Starter Write a program that asks the user if it is raining today.
Lecture 4B More Repetition Richard Gesick
While Loops BIS1523 – Lecture 12.
Control Structure Senior Lecturer
Lesson #5 Repetition and Loops.
Fundamentals of Programming
Chapter 7 Additional Control Structures
Chapter (3) - Looping Questions.
Do While (condition is true) … Loop
4.1 Strings ASCII & Processing Strings with the Functions
Do … Loop Until (condition is true)
Chapter 2.2 Control Structures (Iteration)
Text / Serial / Sequential Files
3.1 Iteration Loops For … To … Next 18/01/2019.
Nested Loops & The Step Parameter
Python programming exercise
Introduction to Repetition Structures
Alternate Version of STARTING OUT WITH C++ 4th Edition
Topics Introduction to Repetition Structures
Lesson #5 Repetition and Loops.
Review of Previous Lesson
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
MIS 3200 – Unit 5.1 Iteration (looping) Working with List Items
Group Boxes, Radio buttons and Checked List Boxes
3.2 Working with Data Scope of variables 29/07/2019.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

The Step Parameter & Nested For … To … Next loops 5.2 Iteration Loops The Step Parameter & Nested For … To … Next loops 18/07/2019

Learning Objectives State what the step parameter is used for. State the general form of a nested For … To … Next iteration loop. State how to: Keep the current text and add new text to a control. To enter or start a new line. To enter spaces. 18/07/2019

For … To … Next e.g. Display the numbers from 1, 3, 5, 7, 9 Note: Dim Number As Integer For Number = 1 To 10 Step 2 lblNumber.Text = Number Next Number Note: This loop will not display 11 as it is bigger than the 10 allowed 18/07/2019

A nested For … To … Next Iteration Loop When you have one loop inside another. Think of the outer loop as a large cog driving a smaller cog which is the inner loop. Every time the larger cog revolves once (one repetition of the outer loop), the inner cog usually revolves more than once. As a solid real life example think of the second and minute hand. The minute hand would be the outer loop. The second hand would be the inner loop. 18/07/2019

General Form of a nested For … To … Next Iteration Loop For (variable identifier = start value) To (end value) (Outer loop body statements) … (Inner loop body statements) … Next (variable identifier) O u t e r L o p I n e r L o p 18/07/2019

A nested For … To … Next Iteration Loop lstNumber.Items.Clear() Dim OuterNumber As Integer Dim InnerNumber As Integer For OuterNumber = 1 To 4 lstNumber.Items.Add(“OuterNumber variable is ” & OuterNumber) For InnerNumber = 1 To 2 lstNumber.Items.Add(“InnerNumber variable is ” & InnerNumber) Next InnerNumber Next OuterNumber 18/07/2019

A nested For … To … Next Iteration Loop The previous slide’s code will produce: OuterNumber variable is 1 InnerNumber variable is 1 InnerNumber variable is 2 OuterNumber variable is 2 OuterNumber variable is 3 OuterNumber variable is 4 18/07/2019

Note: To keep the current text and add new text to a control: lblTable.Text = lblTable.Text & … (new text) To enter or start a new line: vbNewLine To enter spaces: Space(number) 18/07/2019

Program 5.2 Addition Table Specification: Write a program to display the sum of row and column numbers. 18/07/2019

Program 5.2 Addition Table Create a new project named ‘Addition Table’. Change the form’s Text property to ‘Addition Table’. 18/07/2019

Addition Table One large label. To allow the enlargement of a label you need to turn the “AutoSize” property to False and then resize the label to be as large as you want. 18/07/2019

Program 5.2 Addition Table Name the label lblTable. 18/07/2019

Program 5.2 Addition Table Form1 code: Const Max = 5 Dim ColNumber As Integer Dim RowNumber As Integer Dim Sum As Integer 'Display '+' and '12' spaces in top right corner to start table. lblTable.Text = "+" & Space(12) For ColNumber = 0 To Max 'Simple For ... To ... Next loop 'Enter column numbers with 8 spaces in between. 'Note that 'lblTable.Text' keeps current text and '&' adds new text. lblTable.Text = lblTable.Text & ColNumber & Space(8) Next ColNumber 18/07/2019 Continued on next slide.

Program 5.2 Addition Table 'Enter a empty line. lblTable.Text = lblTable.Text & vbNewLine For RowNumber = 0 To Max 'Start outer loop. 'Start a new row. 'Enter first number in the row. lblTable.Text = lblTable.Text & RowNumber & Space(12) For ColNumber = 0 To Max 'Start of inner loop. Sum = ColNumber + RowNumber 'Enter addition lblTable.Text = lblTable.Text & Sum & Space(8) Next ColNumber 'End of inner loop. Next RowNumber 'End of outer loop. 18/07/2019

Program 5.1 Multiplication Table Run the program and test it. Save and publish the program. 18/07/2019

Extension “Multiplication” Program 1 Write a program to produce the following multiplication table. Note you will have a problem with columns due to numbers >99. Use Courier New font (as this a fixed width font which means each character has the same width). Use an If …Then … End If statement to add less spaces when necessary. Please note that I actually don’t expect you to fully solve this. If you can then great otherwise spend around 15 minutes and then just send me what you have. 18/07/2019

The Step Parameter Used to change the variable identifier in a For … To … Next iteration loop by something other than +1. For (variable identifier = start value) To (end value) Step (increment value) (Loop Body statements) … Next (variable identifier) 18/07/2019

Extension “Between Two Numbers” Program 2 Extend the “Between Two Numbers” Program from 5.1 Loops so that it has a new button which will count down from a higher first number to a lower second number. Hints: Copy and paste the code from the original button into the code template of the new Count Down button and adjust as necessary. To count down write Step -1 at the end of the For … To … Step -1 Extension: Show only numbers between the two values not the values themselves. Stop the user entering letters. What happens if the second number is higher than the first number when this new second button is clicked? The loop does not end and results in what is called an infinite loop, resulting in the program freezing. The new “second” button should not allow the second number to be higher than the first number. 18/07/2019

Plenary What is the step parameter used for? Used to increase the variable identifier in a For … To … Next loop by more than 1. 18/07/2019

Plenary What is the general form of a nested For … To … Next loop? For (variable identifier = start value) To (end value) (Outer loop body statements) … (Inner loop body statements) … Next (variable identifier) O u t e r L o p I n e r L o p 18/07/2019

Plenary How do you: Keep the current text and add new text to a control. To enter or start a new line. To enter spaces. 18/07/2019

Plenary To keep the current text and add new text to a control: lblTable.Text = lblTable.Text & … (new text) To enter or start a new line: vbNewLine To enter spaces: Space(number) 18/07/2019