27/05/20161 3.2 Iteration Loops Nested Loops & The Step Parameter.

Slides:



Advertisements
Similar presentations
30/04/ Selection Nested If structures & Complex Multiple Conditions.
Advertisements

Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
Introduction to Computing Science and Programming I
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Chapter 5: Loops and Files.
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.
An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
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.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
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.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
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.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Computer Programming TCP1224 Chapter 8 More On Repetition Structure.
22/11/ Selection If selection construct.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Control Structures RepetitionorIterationorLooping Part I.
18/12/ Arrays 2D Arrays Defining, Declaring & Processing.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Copyright © Curt Hill Loop Types and Construction Logical types and construction hints.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Multidimensional Arrays Computer and Programming.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
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.
REPETITION CONTROL STRUCTURE
Topics Introduction to Repetition Structures
While Loops BIS1523 – Lecture 12.
Chapter 7 Additional Control Structures
Do While (condition is true) … Loop
Chapter 8: More on the Repetition Structure
Do … Loop Until (condition is true)
3.1 Iteration Loops For … To … Next 18/01/2019.
Nested Loops & The Step Parameter
Topics Introduction to Repetition Structures
Review of Previous Lesson
The Step Parameter & Nested For … To … Next loops
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

27/05/ Iteration Loops Nested Loops & The Step Parameter

227/05/2016 Learning Objectives State what the step parameter is used for. State the general form of a nested For … To … Next iteration loop. How to output to the same line rather than always to a new line. How to start an empty new line. State how to enter spaces. Explain what constants are and their advantages.

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

427/05/2016 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 minute hand would be the outer loop. The second hand would be the inner loop. The second hand would be the inner loop.

527/05/2016 General Form of a nested For … To … Next Iteration Loop For (variable identifier = start value) To (end value) (Outer loop body statements) … (Outer loop body statements) … For (variable identifier = start value) To (end value) For (variable identifier = start value) To (end value) (Inner loop body statements) … Next (variable identifier) Next (variable identifier) (Outer loop body statements) … (Outer loop body statements) … Next (variable identifier) InnerLoop OuterLoop

627/05/2016 A nested For … To … Next Iteration Loop Dim OuterNumber As Integer Dim InnerNumber As Integer For OuterNumber = 1 To 4 Console.WriteLine(“OuterNumber variable is ” & OuterNumber) Console.WriteLine(“OuterNumber variable is ” & OuterNumber) For InnerNumber = 1 To 2 For InnerNumber = 1 To 2 Console.WriteLine(“InnerNumber variable is ” & InnerNumber) Next InnerNumber Next InnerNumber Next OuterNumber

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

827/05/2016 Constants Used when: The user does not provide the value. The user does not provide the value. You don’t want the value to change during the program. You don’t want the value to change during the program. e.g. Const TaxRate = 0.25 If you try to assign another value to a constant later in the program an error will occur.

927/05/2016 Constants Using a constant rather than a number presents two advantages: It allows the value to be changed by the programmer easily i.e. otherwise the programmer would have to go through and change the value every time it occurs. It allows the value to be changed by the programmer easily i.e. otherwise the programmer would have to go through and change the value every time it occurs. It makes the code more meaningful to anybody reading it. It makes the code more meaningful to anybody reading it.

To output to the same line / Create a new empty line / Display Multiple Spaces: Console.Write(“….”) Will output onto the same previous line so basically continues a line of outputs. If no previous line then it will output onto a line but without creating a new one. Console.WriteLine() Will start a new empty line. To enter spaces: Space(number)

1127/05/2016 Program 3.2a Addition Table Specification: Write a program to display the sum of row and column numbers. Write a program to display the sum of row and column numbers.

1227/05/2016 Program 3.2a Addition Table Create a new project named ‘Addition Table’.

1327/05/2016 Program 3.2a Addition Table 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. Console.Write("+" & Space(12) ) For ColNumber = 0 To Max 'Simple For... To... Next loop 'Enter column numbers with 8 spaces in between. 'Enter column numbers with 8 spaces in between. Console.Write(ColNumber & Space(8)) Console.Write(ColNumber & Space(8)) Next ColNumber Continued on next slide.

1427/05/2016 Program 3.2a Addition Table Console.WriteLine() ’Start a new empty line. For RowNumber = 0 To Max 'Start outer loop. 'Enter first number in the row. 'Enter first number in the row. Console.Write(RowNumber & Space(12)) Console.Write(RowNumber & Space(12)) For ColNumber = 0 To Max 'Start of inner loop. For ColNumber = 0 To Max 'Start of inner loop. Sum = ColNumber + RowNumber 'Enter addition Console.Write(Sum & Space(8)) Next ColNumber 'End of inner loop. Next ColNumber 'End of inner loop. Console.WriteLine() ’Start a new empty line. Console.WriteLine() ’Start a new empty line. Next RowNumber 'End of outer loop.

1527/05/2016 Program 3.2a Multiplication Table Run the program and test it.

Given pseudocode will use the following structure: FOR ← TO STEP ENDFOR

1727/05/2016 Extension “Multiplication” Program 3.2b 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 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. 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.

1827/05/2016 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) … (Loop Body statements) … Next (variable identifier)

1927/05/2016 Extension “Between Two Numbers” Program 3.2c Extend the “Between Two Numbers” Program from 3.1 For To Next Loops so that it has a 2 options: 3.1 For To Next Loops3.1 For To Next Loops 1.As before: to count up from a lower first number to a higher second number. 2.Count down from a higher first number to a lower second number. Hints: Ask the user for option 1 or 2, before the main code. Ask the user for option 1 or 2, before the main code. New If statement, just after this, to see which option has been chosen. New If statement, just after this, to see which option has been chosen. Leave the original main code for option 1. Leave the original main code for option 1. Copy and paste the original main code into the ElseIf for option 2 and adjust as necessary. Copy and paste the original main code into the ElseIf for option 2 and adjust as necessary. To count down write Step -1 at the end of the For … To … Step -1 To count down write Step -1 at the end of the For … To … Step -1Extension: Show only numbers between the two values not the values themselves. Show only numbers between the two values not the values themselves. What happens if the second number is higher than the first number when this new second button is clicked? 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 VB skipping the loop. The new “second” option should not allow the second number to be higher than the first number.

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

2127/05/2016 Plenary What is the general form of a nested For … To … Next loop? For (variable identifier = start value) To (end value) For (variable identifier = start value) To (end value) (Outer loop body statements) … For (variable identifier = start value) To (end value) (Inner loop body statements) … (Inner loop body statements) … Next (variable identifier) (Outer loop body statements) … Next (variable identifier) Next (variable identifier) InnerLoop OuterLoop