CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091.

Slides:



Advertisements
Similar presentations
Chapter 6 - VB 2005 by Schneider1 Do Loop Syntax Do While condition statement(s) Loop Condition is tested, If it is True, the loop is run. If it is False,
Advertisements

RAPTOR Syntax and Semantics By Lt Col Schorsch
Introduction to Computing Science and Programming I
CS0004: Introduction to Programming Repetition – Do Loops.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
 Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design.
Computer Science 1620 Loops.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CSE 113 Week 5 February , Announcements  Module 2 due 2/15  Exam 3 is on 2/15  Module 3 due 2/22  Exam 4 is on 2/25  Module 4 due 2/29.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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.
Function.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Visual Basic Games: Week 3 Global variables, parameters, Select, KeyDown Enable, Visible, Focus State of Game Read chapter 3.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Chapter 12: How Long Can This Go On?
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# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
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 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
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.
By Chad Blankenbeker.  The for-loop is best used when you know how many times it is going to be looped  So if you know you want it to only loop 10 times,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
CS320n –Visual Programming
6.2 For…Next Loops General Form of a For…Next Loop
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
259 Lecture 11 Spring 2013 Advanced Excel Topics – Loops.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
# 1# 1 Nested If Statements in VBA What is a compound condition that we evaluate? What is a Nested If statement? How do we use ElseIf? CS 105 Spring 2010.
Controlling Program Flow with Decision Structures.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
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.
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.
CS 105 – Fall CS105 Lab 10 – Arrays Announcements MP 2: Please make sure you have changed the “Author” field according to your NetIDs Please READ.
CE En 270 Brigham Young University Norm Jones
Chapter 4 Repetition Statements (loops)
Advanced Excel Topics – Loops
Chapter 5: Loops and Files.
Introducing Do While & Do Until Loops & Repetition Statements
Unit 2 Programming.
Outline Altering flow of control Boolean expressions
3 Control Statements:.
3.1 Iteration Loops For … To … Next 18/01/2019.
PROGRAM FLOWCHART Iteration Statements.
CSC1401 Manipulating Pictures 2
Presentation transcript:

CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Objectives Develop a more sophisticated paint program Nested-For-Loop Do-Until-Loop Generate a random number CS 105 – Fall 20092

What we will do CS 105 – Fall Last week, we:  Painted  Horizontally/Right  Vertically/Down  Diagonally/Right  Changed the Brush Size This week, we will do Paint Up and Left Paint a Square Paint a line until it hits something Paint with random color and length

For intOffset = 0 To mintBrushSize – 1 Cells(intRow + intOffset, intCol).Interior.Color = vbBlue Next intOffset Last week REMEMBER: We used a For-Loop to paint a line How do we draw a line downward? How do we draw a line rightward? CS 105 – Fall For intOffset = 0 To mintBrushSize – 1 Cells(intRow, intCol + intOffset).Interior.Color = vbBlue Next intOffset “+intOffset” ?

REMEMBER: What we call an IF inside an IF? In a similar way, we call a For-Loop inside a For- Loop a Nested-For-Loop Paint a square To paint a 5x5 square, we need to draw 5 lines, each containing 5 cells We need a For-Loop to paint lines 1 through 5: AND for each line, we need another For-Loop to draw 5 cells CS 105 – Fall Nested-If

Paint a square For intRowOffset = 0 To 4 For intColOffset = 0 To 4 Cells(intRow + intRowOffset, intCol + _ intColOffset).Interior.Color = vbBlue Next intColOffset Next intRowOffset CS 105 – Fall Dim intRowOffset As Integer Dim intColOffset As Integer Tip: to observe the nested For-Loop step by step you can call the “Delay” sub here

Paint a square, with the ActiveCell in the center For intRowOffset = -2 To 2 For intColOffset = -2 To 2 Cells(intRow + intRowOffset, intCol + _ intColOffset).Interior.Color = vbBlue Next intColOffset Next intRowOffset CS 105 – Fall  The previous code painted a square to the right and bottom of the ActiveCell. How can we draw it around the ActiveCell?

Paint until it hits a blue cell. We want to paint a line from the the ActiveCell, until it hits another cell that is painted blue. How do we do this? We don’t know how many cells we have to paint. Therefore we cannot use a For-Loop. But we do know “when to stop”. We STOP when the cell we are painting is already blue We should use a: Do-Until-Loop CS 105 – Fall 20098

9 Start Is loop Condition true? End Yes Execute statements in loop body No Do-Until Loop Flowchart

CS 105 – Fall Syntax of a Do-Until Loops Do-Until has the following syntax: Do Until Loop Pseudo-code: Do Until “the current cell is blue” “paint the current cell blue” “move current cell one step to the right” Loop

Paint until hitting a blue cell. Do Until Cells(intRow,intCol+intColOffset).Interior.Color = vbBlue Cells(intRow, intCol+intColOffset).Interior.Color = vbBlue intColOffset = intColOffset + 1 Loop CS 105 – Fall ‘NOTE: intRow and intCol hold the position of ‘the current cell intRow = ActiveCell.Row intCol = ActiveCell.Column intColOffset = 0

Infinite Loop When does the Do-Until-Loop finish? ONLY when the loop condition is TRUE What happens if the loop condition is never TRUE? NOTE: The loop condition has to eventually be TRUE otherwise the loop will NEVER stop, becoming an infinite loop. Use Ctrl-Break or Esc to terminate an infinite loop CS 105 – Fall

Brush until hitting blue cells. We want to paint lines of mintBrushSize size until theywe come across blue cells How do we do this? We know how to draw one line, using a Do-Until-Loop We know how to do this mintBrushSize times, using a For-Loop We should use a: Do-Until-Loop nested inside a For-Loop CS 105 – Fall

Paint until hitting a blue cell. For intOffset = 0 to mintBrushSize -1 intCol = ActiveCell.Column Do Until Cells(intRow + intOffset, intCol).Interior.Color = vbBlue Cells(intRow +intOffset, intCol).Interior.Color = vbBlue intCol = intCol + 1 Loop Next intOffset CS 105 – Fall ‘NOTE: intRow and intCol hold the position of ‘the current cell intRow = ActiveCell.Row

Finally, make a masterpiece ! We want to paint with a random color VBA has 56 color index, ranging from 1 to 56. REMEMBER: To paint a particular cell with a color index we wrote: ….Interior.ColorIndex=[color_index] How do we get a random color_index between 1 and 56? CS 105 – Fall

Generate a random number VBA provides a built-in function called Rnd() that gives a random floating-point number in [0,1). How can we convert the random number provided by Rnd() into an integer between 1 and 56? Answer : Int( Rnd() * 56 ) + 1 CS 105 – Fall Rnd() Scaling up Color Index 12…

Generate a random number (cont) What if we want a random number between 2 and 10? Answer : Int( Rnd() * (10-2+1) ) + 2 CS 105 – Fall Rnd() Scaling up Color Index 23…

Finally, make a masterpiece ! Private Sub cmdPaintRandom_Click() Dim intRow As Integer Dim intCol As Integer intRow = ActiveCell.Row intCol = ActiveCell.Column 'TODO : call Randomize sub procedure Randomize 'Two variables to hold a random color index and random length Dim intRandomLength As Integer Dim intRandomColor As Integer Dim intOffset As Integer 'TODO : get a random integer between 1 and 56 and assign to intRandomColor intRandomColor = Int(Rnd() * 56) + 1 'TODO : get a random integer between 1 and 10 and assign to intRandomLength intRandomLength = Int(Rnd() * (10-2+1)) + 2 'This is the incrementing variable for the For-loop 'for loop to paint "mintBrushSize" cells at a time 'TODO : use intRandomLength here For intOffset = 0 To intRandomLength – 1 'TODO : use intRandomColor here Cells(intRow + intOffset, intCol).Interior.ColorIndex = intRandomColor Next intOffset End Sub procedure to make it more random CS 105 – Fall

What you should know? How to write a Nested-For-Loop? How to write a Do-Until-Loop How to nest it inside a For-Loop How to generate a random integer number? 19

Exercises Add more “Paint until Hit” buttons, each does a different direction : up, down, left, right Do the same for “Paint Randomly” Do the above, but this time, paint in 4 diagonal directions : North-East, South-East, South-West, North-West. 20

Exercises 21 Do-Loop-WhileDo-Loop-UntilDo-Until-Loop Sub Test1 (intA As Integer) Do intA = intA + 1 Loop While intA < 10 End Sub Sub Test2 (intA As Integer) Do intA = intA + 1 Loop Until intA < 10 End Sub Sub Test3 (intA As Integer) Do Until intA >= 10 intA = intA + 1 Loop End Sub Exercise # Timesvalue of intA?Exercise# Timesvalue of intA?Exercise# Timesvalue of intA? Test1(1)Test2(10)Test3(100) Test2(1)Test3(10)Test1(0) Test3(1)Test1(100)Test1(-10) Test1(10)Test2(100)Test2(9)