Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Slides:



Advertisements
Similar presentations
Control Structures.
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.
Objectives In this chapter, you will learn about:
CS 106 Introduction to Computer Science I 09 / 25 / 2006 Instructor: Michael Eckmann.
Chapter 5: Control Structures II (Repetition)
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
Chapter 2 Control.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
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.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CS 100 Introduction to Computing Seminar October 7, 2015.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
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.
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 4 Repetition Statements (loops)
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
CS161 Introduction to Computer Science
Python: Control Structures
Logical Operators and While Loops
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.
More Repetition While and For Loops Sentinel-Controlled Loops
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Types, Truth, and Expressions (Part 2)
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Coding Concepts (Basics)
3. Decision Structures Rocky K. C. Chang 19 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
Intro to Nested Looping
Computer Science Core Concepts
Types, Truth, and Expressions (Part 2)
Flow of Control.
Chapter 5 Decisions.
CHAPTER 5: Control Flow Tools (if statement)
Repetition Statements (Loops) - 2
Types, Truth, and Expressions
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.
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions
Presentation transcript:

Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg

Today’s Agenda From last week  Selection statements  Conditional statements  Compound conditional statements Repetition  Reasons for repetition  Tools

if, elif, else, the Process Evaluate boolean expressions until:  The boolean expression returns True  None of the boolean expressions return True If a boolean returns True, run the corresponding suite. Skip the rest of the if If no boolean returns True, run the else suite, the default suite

Compound Expressions Logical Operators (lower case)  and  or  not

Compound Expressions Examples  if (a==b) and (c<d):  if (a==b) or (c<d):  if not (a==b): Remember, evaluate each statement first before looking at and, or, not 5

Chained Comparisons You are going to be tempted to write: 0 <= myInt <= 5 But you will need to be very careful

Compound Evaluation Logically 0 < X < 3 is actually (0 < X) and (X < 3) Evaluate using X with a value of 2: (0< X) and (X< 3) Parenthesis first: (True) and (True) Final value: True (Note: parentheses are not necessary in this case.)

Compound Evaluation BUT, I’ve seen students write: 3 < X < 0  Meaning they want to know if x is outside of that range. But this is actually (3 < X) and (X < 0) Does any number fit in this range?

How can we introduce error checking? Suppose that students are entering numbers into your miles per gallon program that aren’t valid

How can we introduce error checking? Suppose that students are entering numbers into your miles per gallon program that aren’t valid  Values not above 0  Ending value that is “smaller” than the starting value  Maybe even values above some upper limit How can we correct them and ask them to try again?

Repeating Statements Besides selecting which statements to execute, a fundamental need in a program is repetition  repeat a set of statements under some conditions Between selection and repetition, we have the two most necessary programming statements

While and For Statements The while statement is the more general repetition construct. It repeats a set of statements while some condition is True.  Often called a sentinel controlled loop The for statement is useful for iteration, moving through all the elements of data structure, one at a time.  Often called a count controlled loop

while Loop Top-tested loop (pretest)  test the boolean before running  Run the program suite  test the boolean before each iteration of the loop while boolean expression: statementSuite

Repeat While the Boolean is True while loop will repeat the statements in the suite while the boolean is True (or its Python equivalent) If the boolean expression never changes during the course of the loop, the loop will continue forever.

x_int = 0 while x_int < 10: print (x_int) x_int = x_int + 1 print() print( "Final value of x_int: ", x_int) What is the Final Value printed by this code? While Loop Example

General Approach to a While outside the loop, initialize the boolean somewhere inside the loop you perform some operation which changes the state of the program,  eventually leading to a False boolean and exiting the loop Have to have both!

For and Iteration One of Python’s strength’s is it’s rich set of built-in data structures The for statement is a common statement for manipulation of a data structure  for each element in the datastructure perform some operation on that element

For Loop Example numbers = [0,1,2,3,4,5,6,7,8,9] for xInt in numbers: print (xInt) print() print ("Final value of xInt: " + str(xInt) )

How can we introduce error checking? Suppose that students are entering numbers into your miles per gallon program that aren’t valid  Values not above 0  Ending value that is “smaller” than the starting value  Maybe even values above some upper limit How can we correct them and ask them to try again?