Intro to Computer Science CS1510 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

While loops.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CS0004: Introduction to Programming Repetition – Do Loops.
Conditional Statements Introduction to Computing Science and Programming I.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
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.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
FOP: While Loops.
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
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.
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
Conditions and Ifs BIS1523 – Lecture 8.
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
Types, Truth, and Expressions (Part 2)
More Repetition While and For loop variations
Logical Operators and While Loops
Chapter 5 Decisions.
CHAPTER 5: Control Flow Tools (if statement)
Repetition Statements (Loops) - 2
Another Example Problem
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.
Looping and Repetition
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions
Presentation transcript:

Intro to Computer Science CS1510 Dr. Sarah Diesburg Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg

Today’s Agenda From last week Repetition 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

Truth Tables

Truth Tables

Truth Tables

Truth Tables

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) Evaluate using X with a value of 5: (3< X) and (X< 0) Parenthesis first: (True) and (False) Final value: False

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) while boolean expression: 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.

While Loop Example 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?

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?