Chapter 4: Repetition Structures: Looping

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Chapter 5 Repetition or loop structure. What is repetition or loop? repeat the execution of one or a group (block; instruction enclosed in a pair of braces)
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
Chapter 4: Looping Structures LECTURER : MRS ROHANI HASSAN
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
ECE Application Programming
Python Loops and Iteration
Chapter 2: Input, Processing, and Output
Repetition (While-Loop) version]
Topics Introduction to Repetition Structures
Python: Control Structures
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Loop Structures.
CHAPTER 5A Loop Structure
Chapter 4 Loops DDC 2133 Programming II.
Think What will be the output?
Chapter 5: Control Structure
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Chapter 5: Repetition Structures
Topic 5 for Loops -Arthur Schopenhauer
( Iteration / Repetition / Looping )
Chapter 4 – Control Structures Part 1
Programming Fundamentals
Lecture 07 More Repetition Richard Gesick.
ITM 352 Flow-Control: Loops
Lecture 4B More Repetition Richard Gesick
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Print slides for students reference
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 Control structures and Loops
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Looping and Repetition
Control Structure Senior Lecturer
Introduction to pseudocode
Introduction to Object-Oriented Programming with Java--Wu
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 4 Loops While loop The for loop do… while break and continue
Loops CIS 40 – Introduction to Programming in Python
Repetition Control Structure
Chapter 6: Repetition Statements
3.1 Iteration Loops For … To … Next 18/01/2019.
© 2016 Pearson Education, Ltd. All rights reserved.
Computing Fundamentals
A LESSON IN LOOPING What is a loop?
CHAPTER 6: Control Flow Tools (for and while loops)
Loops.
ECE 103 Engineering Programming Chapter 18 Iteration
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Conditional Loops Counted Loops
Python While Loops.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

Chapter 4: Repetition Structures: Looping

4.1 Computers Never Get Bored All programming language provide statement to create a loop. The loop is the basic component of repetition structure. These statements are block of code, which under certain conditions, will be executed repeatedly.

A simple Example of loop Declare Number As Integer Repeat Write “Please enter a number: “ Input Number Write Number Until Number == 0 Write “List Ended” The body of the loop is executed repeatedly until the user enters a 0. At that point the loop is exited, and the statement that follows the loop is executed. Note the indentation.

A simple Example of loop: Flow chart

Iterations Declare Name As String Repeat Write “Enter the name of your brother or sister: “ Input Name Write Name Until Name = “Done” How many times will the loop run? Each time the loop runs it is called an iteration. Jim has 2 brothers & 1 sister: 4 iterations Marie has 1 sister: 2 iterations Can we create a loop that doesn’t use the test condition as one of the iterations? Yes … we’ll see how later in this chapter.

Beware of the infinite loop The user should enters a positive number , hence ComputerNumber will always be one greater. The loop’s condition is never satisfied.

4.2 Types of Loops Loops are one of the most indispensable tools of any programmer. Loops are used to load data, manipulate data, interact with the user, and much more. Loops come in various types. One type may work well for one specific program’s need while another type fits a different program design. A programmer needs to understand be able to use the different types of loops. Can be divided into two fundamental types: pre-test loops and post-test loops.

Flowcharts for Pre-test and Post-Test Loops Figure 4.4 Pre- Test loop flowchart

Pre-Test Loop While Number != 0 End While This is a pre-test loop. The test condition appears at the top, before the body of the loop is executed the first time. Write “Enter a number: “ Input Number While Number != 0 Write Number End While Write “Done”

Trace A Loop (Walk through the loop manually) It is very hard to see what a loop is doing without tracing it to see how it works. Suppose the user enters 3, 1, -1. Trace the code with this input. Declare Number As Integer Write “Enter a number or 0 to quit:” Input Number While Number > 0 Write Number^2 End While Number Output 3 9 1 1 -1

Using a Pre-test Loop Wisely Previously the program (slide no 5) to list the brothers and sisters of a user always ended with the output “Done” (i.e, the test condition). A pre-test loop can be used to avoid having unwanted data: Declare Name As String Write “Enter the name of your brother or sister: “ Input Name While Name != “Done” Write Name Until Name = “Done”

Flowcharts for Pre-test and Post-Test Loops Figure 4.5 Post-test loop flowchart

Post-Test Loop Repeat Write “Enter a number:“ These are post-test loops. The test condition occurs after the body of the loop is executed. Repeat Write “Enter a number:“ Input Number Write Number Until Number == 0 Write “Done” Do Write “Enter a number: “ Input Number Write Number While Number != 0 Write “Done”

Flowcharts for Pre-test and Post-Test Loops Figure 4.6 Pre- and post-test loop flowcharts

Counter-controlled Loops Define a counter: the counter is a variable. It is always an integer Common variable names are counter, Count, I, or j. Initialize the counter: set the counter to a beginning value. To count by ones, the computer takes what it had before and adds one. The code for a computer to count by ones looks like: Count + 1. Store the new value: store it where the old value was. If we are counting up by ones, we store the new value by the statement: Count = Count + 1

Example: Use a Counter to Display the Squares of Numbers Declare PositiveInteger As Integer Declare Count As Integer Write “Please enter a positive integer: “ Input PositiveInteger Set Count = 1 While Count <= PositiveInteger Write Count + “ “ + Count^2 Set Count = Count + 1 End While Output if the user enters 5 for PositiveInteger:    Count Output 1 1 1 2 2 4 3 3 9 4 4 16 5 5 25

Counters Count Up and Down Here is an example of using a counter in a loop to count down. Countdown to Blastoff: Declare Count As Integer Set Count = 100 Write “Countdown in ...” While Count > 0 Write Count + “ seconds” Set Count = Count – 1 End While

Any Questions ?