More Repetition While and For loop variations

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CPS120 Introduction to Computer Science Iteration (Looping)
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
1 CS161 Introduction to Computer Science Topic #8.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
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.
The Accumulator Pattern Summing: Add up (“accumulate”), e.g. 1 2 plus 2 2 plus 3 2 plus … plus Variation: Form a product (instead of sum), e.g.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
1 Sections 7.2 – 7.7 Nested Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
FOP: While Loops.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Trace Tables In today’s lesson we will look at:
The switch Statement, and Introduction to Looping
Loop Structures.
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Chapter 5: Repetition Structures
Web Programming– UFCFB Lecture 16
Computer Science 101 While Statement.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
More Repetition While and For Loops Sentinel-Controlled Loops
Nate Brunelle Today: Pseudo-Code, Party
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
More Nested Loops and Lab 5
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Count Controlled Loops
Introduction to Object-Oriented Programming with Java--Wu
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Nate Brunelle Today: Repetition, Repetition
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Thinking about Strings
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Chapter 8: More on the Repetition Structure
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
CS150 Introduction to Computer Science 1
Loop Strategies Repetition Playbook.
Computing Fundamentals
Learning Intention I will learn about evaluating a program.
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Introduction to Strings
Chapter8: Statement-Level Control Structures April 9, 2019
do/while Selection Structure
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Introduction to Strings
Intro to Nested Looping
Types, Truth, and Expressions
Millennium High School Agenda Calendar
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
Introduction to Strings
Nate Brunelle Today: Pseudo-Code
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions
Presentation transcript:

More Repetition While and For loop variations Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Today’s Agenda Going over yesterday’s lab Exploring looping alternatives

Right and Wrong Challenge to new programmer Isn’t there a right way to do things? Well, there are definitely wrong ways to do things E.g., logic errors, wrong answers, crashes But, there are often many right ways to do something

Rules of Thumb So far Have the least amount of indents (nesting) as possible Try to have at most two boolean expressions in a conditional statement Use meaningful variable names

Loops What are the two ways we can generate repetition? while loop and for loop What is the purpose of a for loop? Count-controlled loop, which means we will know in advance how many times the loop will run Why?

For Loops for varName in iterableDataStructure: (next thing in DataStructure put in varName) suite of code

For Loops for varName in iterableDataStructure: (next thing in DataStructure put in varName) suite of code varName is often called an “iterator” Let’s take a look at quiz.py…

Range 3 versions range(r,s) – means range of r to s-1 range(x) – means range(0,x), which is 0 to (x-1) range(a,b,c) – like range(a,b), but c is the “step” value How about range(3, 10, 2)?

Range How many numbers are generated in range(0,8)? How many numbers are generated in range(x,y)?

Range How many numbers are generated in range(0,8)? How many numbers are generated in range(x,y)? y-x

Ranges in For Loops A range is an iteratable data structure Why was quiz.py broken? What are all the ways quiz.py could be fixed?

Let’s go through an example Annoying kid in the car (cartrip.py)

For loop for counter in range(age): print("Are we there yet?")

While loop counting up counter=0 while counter<age: print("Are we there yet?") #counter=counter+1 counter += 1

While loop counting down while age>0: print("Are we there yet?") age -= 1 #But now age is destroyed!