Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

While loops.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Fundamentals of Python: From First Programs Through Data Structures
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Fundamentals of Python: First Programs
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Programming Logic and Design Fifth Edition, Comprehensive
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Controlling Program Flow with Looping Structures
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Follow up from lab See Magic8Ball.java Issues that you ran into.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Lesson 06: Functions Class Participation: Class Chat:
Lesson 03: Variables and Types
Exam #1 You will have exactly 30 Mins to complete the exam.
Lesson 10: Dictionaries Topic: Introduction to Programming, Zybook Ch 9, P4E Ch 9. Slides on website.
Week of 12/12/16 Test Review.
Introduction To Repetition The for loop
Control Structures II Chapter 3
Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.
Topics Introduction to Repetition Structures
Python: Control Structures
Lesson 05: Iterations Class Chat: Attendance: Participation
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.
Lesson 07: Strings Topic: Introduction to Programming, Zybook Ch 6, P4E Ch 6. Slides on website.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Chapter 5: Loops and Files.
Chapter 5: Repetition Structures
While Loops in Python.
Topics Introduction to Repetition Structures
Iterations Programming Condition Controlled Loops (WHILE Loop)
Control Structures - Repetition
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Iteration with While You can say that again.
TOPIC 4: REPETITION CONTROL STRUCTURE
MSIS 655 Advanced Business Applications Programming
Lesson 04: Conditionals Class Chat: Attendance: Participation
Lesson 08: Files Topic: Introduction to Programming, Zybook Ch 7, P4E Ch 7. Slides on website.
Lecture Notes – Week 3 Lecture-2
Lesson 09: Lists Topic: Introduction to Programming, Zybook Ch 8, P4E Ch 8. Slides on website.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Repetition Structures
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
IST256 : Applications Programming for Information Systems
Lesson 06: Functions Class Chat: Attendance: Participation
CS 200 Loops Jim Williams, PhD.
Do … Loop Until (condition is true)
Lesson 09: Lists Class Chat: Attendance: Participation
3. Decision Structures Rocky K. C. Chang 19 September 2018
Chapter 6: Repetition Statements
CMSC201 Computer Science I for Majors Lecture 09 – While Loops
CHAPTER 6: Control Flow Tools (for and while loops)
Loops.
Topics Introduction to Repetition Structures
Another Example Problem
Learning Plan 4 Looping.
While Loops in Python.
JavaScript 101 Lesson 8: Loops.
Iteration – While Loops
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.

Agenda Make our code execute in a non linear fashion. Definite loops (for loops) and iterators. Indefinite looping, infinite loops, and the break and continue statements How to build complex loops easily. You’ve Read: Zybook Ch4 P4E Ch5

Connect Activity Select the line number where the increment occurs:

Increment and Decrement Increment means to add one to a variable. X = X + 1 Decrement means to subtract one from a variable. X = X - 1 These are common patterns in iteration statements which you will see today.

Anatomy of a loop A Loop is a sequence of code that repeats as long as a Boolean expression is True. The sequence of code that repeats is known as the Body. The Boolean expression which is tested is known as the Test Condition or Exit Condition. Variables which are part of the Test condition are called Loop Control Variables or Iteration Variables. Our goal is to make the Test condition False so that the loop stops. This is accomplished through changing the loop control variable.

Watch Them Code 1: Andy Sherman – Come On Down! Say My Name: This program will say your name a number of times. This is an example of a Definite loop because the number of iterations are pre-determined.

For Loop The For Loop iterates over a python list, string, or range of numbers. It is the preferred statement for Definite loops, where the number of iterations are pre-determined. Definite loops do not require an exit condition. The for loop uses an iterator to select each item from the list or range and take action in the loop body. The range() function is useful for getting an iterator.

Watch Them Code 2 Joe Alfieri: Come on Down! Say My Name: Range() function Re-Written with a For Loop.

Check Yourself: For Range What is the value of k on line 4? j 1 2 ? k

Watch Them Code 3 Malik Khadija Count the "i"'s Definite Loop Ask for a work Count how many i’s are in the word

Indefinite,Infinite Loops and Break The Indefinite Loop has no pre-determined exit condition. There are no guarantees an indefinite loop will end, as it is typically based on user input. Infinite Loops are loops which can never reach their exit condition. These should be avoided at all costs. The break statement is used to exit a loop immediately. It is often used to force an exit condition in the body of the loop.

Indefinite Loops The Easy Way Determine the code to repeat Determine the loop control variables & exit conditions Write exit conditions as if statements with break Wrap the code in a while True: loop!

Watch Me Code 4 Sophia Kardaras, Come on Down! Guess My Name: This program will execute until you guess my name. Uses the indefinite loop approach

Check Yourself: Loop Matching Match the Definition… To its term. Add one to a variable A look where the test condition is never false Statement to exit the loop Statement for definite looping break Infinite for Increment

End-To-End Example Sophia Hernandez & John Augustine Password Program: 5 attempts for the password On correct password, print: “Access Granted”, then end the program On incorrect password “Invalid Password Attempt #” and give the user another try After 5 attempts, print “You are locked out”. Then end the program.

In Class Coding Lab: Iterations! Make sure you get it checked before you leave! Homework check while you are working: Now You Code 1,2 and 3 FOR NEXT WEEK: READ: ZyBook Ch 5: Functions & P4E: Ch 4: Functions Diagnostic Quiz 6 HW[05]: Now You Code 1,2 and 4 EXAM #1 AT BEGINNING OF NEXT CLASS DON’T BE LATE!!!! Covers Lessons 1-5