BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.

Slides:



Advertisements
Similar presentations
COMPUTER PROGRAMMING I Understand Problem Solving Tools to Design Programming Solutions.
Advertisements

Repetition Control Structures
Repetition control structures
Repetition Control Structures School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 9, Friday 3/07/2003)
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
BACS 287 Programming Fundamentals 4. BACS 287 Programming Fundamentals This lecture introduces the following iteration control structure topics: – Do.
Chapter Chapter 6 Repetition Statements. Objectives Understand repetition control (loop ) statements in Java: while statement. do-while statement.
Chapter 5: Control Structures II (Repetition)
Repetition Control Structures
CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Lecture 4 Loops.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Pseudocode algorithms using sequence, selection and repetition
Logic Structure - focus on looping Please use speaker notes for additional information!
Chapter 7 Array processing. Objectives To introduce arrays and the uses of arrays To develop pseudocode algorithms for common operations on arrays To.
By the end of this session you should be able to...
CS1101X: Programming Methodology Recitation 3 Control Structures.
BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented.
30/10/ Iteration Loops Do While (condition is true) … Loop.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Discussion 4. Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
Count and add list of numbers From user input and from file.
Control Structures Looping Part 2 Part I The C++ for Statement Meaning: 1.Evaluate all initialization expressions 2.Evaluate expression, if true: a)Execute.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Control Structures RepetitionorIterationorLooping Part I.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Algorithms and Pseudocode
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2016, Fred McClurg, All Rights.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Understand Problem Solving Tools to Design Programming Solutions
Chapter 5: Control Structures II (Repetition)
PROGRAM CONTROL STRUCTURE
Understand Problem Solving Tools to Design Programming Solutions
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Computer Science Faculty
TOPIC 4: REPETITION CONTROL STRUCTURE
When I want to execute the subroutine I just give the command Write()
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
STRUCTURED CONTROL: BASIC CONTROL OPERATORS
Pseudocode algorithms using sequence, selection and repetition
Higher Computing Using Loops.
Iteration: Beyond the Basic PERFORM
Control Structures Repetition or Iteration Looping Part I.
A LESSON IN LOOPING What is a loop?
Iteration – While Loops
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

BACS 287 Programming Logic 3

BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In pseudocode there are 2 different iteration types: – DO WHILE - Test at top, 0 or more loops – DO UNTIL - Test at bottom, 1 or more loops

BACS 287 Do While Structure DO WHILE condition action 1... action n ENDDO This executes while the condition continues to be true

BACS 287 Do While Example 1 Get positive Value from user DOWHILE Value < 0 Print “Error, enter a positive value” Get positive Value from user ENDDO Print Value

BACS 287 Do While Example 2 sum = 0 cnt = 0 Display “Enter test score OR -999 to exit” Read input Do While input <> -999 sum = sum + input cnt = cnt + 1 Read input End Do avg = sum / cnt <-- why is this line a problem? Print avg

BACS 287 Do Until Structure DO UNTIL condition action 1... action n ENDDO This executes as long as the condition is not met

BACS 287 Do Until Example 1 Read system password DOUNTIL user password correct Get password from user ENDDO...

BACS 287 Do Until Example 2 num = 0 Read system password DOUNTIL user password correct OR num = 3 Get password from user num = num + 1 ENDDO If num = 3 then <-- why is this ‘IF’ a problem? print “Sorry - Too many tries” Exit Program EndIf

BACS 287 Comparison of Iteration Structures Do While Structure cnt = 1 Do While cnt < 11 Print cnt cnt = cnt + 1 End Do Do Until Structure cnt = 1 Do Until cnt > 10 Print cnt cnt = cnt + 1 End Do

BACS 287 How Do Iteration Structures Work? Do While Structure cnt = 1 Top: If cnt > 10 then goto End EndIf Print cnt cnt = cnt + 1 goto Top End: Do Until Structure cnt = 1 Top: Print cnt cnt = cnt + 1 If cnt < 11 then goto Top EndIf

BACS 287 Comparison of Iteration Structures Do While Structure Read input record Do While Not EOF Gross = Hours * Rate Print Gross Read input record End Do Print Summary What if the input file Do Until Structure Read input record Do Until EOF Gross = Hours * Rate Print Gross Read input record End Do Print Summary is empty?

BACS 287 Iteration and Selection Read input record Do While Not EOF If Balance < 0 then Print “Overdrawn Account” EndIf Read input Record EndDo

BACS 287 Iteration Problem 1 Write pseudocode to print “HI MOM” 100 times.

BACS 287 Iteration Solution 1 cnt = 1 Do While cnt < 101 Print “HI MOM” cnt = cnt + 1 End Do cnt = 1 Do Until cnt > 100 Print “HI MOM” cnt = cnt + 1 End Do

BACS 287 Iteration Problem 2 Write the pseudocode to play a simple “guess the secret number” game. The user has 10 tries to guess. No hints are given after each guess.

BACS 287 Iteration Solution 2 Generate random secret-number cnt = 0 Do Until Guess = secret-number OR cnt = 10 Print “Input your guess” Read Guess cnt = cnt + 1 End Do If Guess = secret-number then Print “Congratulations!! Endif

BACS 287 Iteration Problem 2a Modify the previous problem to give the player hints of “Too high” or “Too low” during the game. The user still has 10 tries to guess.

BACS 287 Iteration Solution 2a Generate random secret-number cnt = 0 Do Until Guess = secret-number OR cnt = 10 Print “Input your guess” Read Guess If Guess < secret-number then Print “Too Low” Elseif Guess > secret-number then Print “Too High” EndIf cnt = cnt + 1 End Do If Guess = secret-number then Print “Congratulations!! Endif

BACS 287 Iteration Problem 3 Sum the odd numbers between 1 and 100 and print the results.

BACS 287 Iteration Solution 3 sum = 0 cnt = 1 Do While cnt < 100 sum = sum + cnt cnt = cnt + 2 End Do Print sum

BACS 287 Iteration Problem 4 Ask the user for a starting positive number. Continuing asking until they enter a valid number. Next, print the numbers from this starting number to 0.

BACS 287 Iteration Solution 4 Start Ask user for start-num DoWhile start-num <= 0 display error message Ask user for start-num End Do Do While start-num >= 0 Print start-num start-num = start-num - 1 End Do End