Mastery Objective: Students will understand how to use while loops in computer programming.

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

While loops.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: 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.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
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.
The University of Texas – Pan American
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
Guide to Programming with Python Chapter Three Branching, while Loops, and Program Planning: The Guess My Number Game.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
CS 100 Introduction to Computing Seminar October 7, 2015.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
ITEC 109 Lecture 18 Looping. Review Questions? Conditionals –if / elif / else –and / or / not.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
ITM © Port, Kazman1 ITM 352 Flow-Control: Loops Lecture #8.
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.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Structured Programming The Basics
FOP: While Loops.
REPETITION CONTROL STRUCTURE
Guide to Programming with Python
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.
Chapter 5: Loops and Files.
ITM 352 Flow-Control: Loops
Python - Loops and Iteration
Agenda Control Flow Statements Purpose test statement
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
Iteration with While You can say that again.
Chapter 3 – Control Structures
Computer Science Core Concepts
ICT Programming Lesson 3:
A LESSON IN LOOPING What is a loop?
CHAPTER 6: Control Flow Tools (for and while loops)
Repetition Statements (Loops) - 2
Python While Loops.
How to allow the program to know when to stop a loop.
Iteration – While Loops
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Mastery Objective: Students will understand how to use while loops in computer programming

Agenda If-else and if-elif-else assignment due tomorrow (Wednesday) by the end of class with no scheduled in class time. If you haven’t finished the assignment, plan to stay after or come in early to finish the assignment. Notes: While loops Programming assignments

While loops Basic principle: While something is true, repeat something Basic structure: while response != “Because.”: response = raw_input(“Why? “)

Similarities and differences between if and while structure In both structures, if the condition is true, the block is executed. What is different: In the while structure, the block is executed until it is false.

Sentry Variable Often, sentry variables control the while loop response variable in example code while response != “Because.”: response = raw_input(“Why? “) Must be intialized prior to use, usually right before while loop. Usually initialized to empty variable response = “ “

example response = “” while response !=“No”: response = “raw_input(“Try again!”): print “All done!”

Infinite Loop Loop that never ends. Example counter = 0 while counter <=10 print counter Forgot to increase counter variable Counter +=1

How to Stop an Infinity Loop Control key +C

Setting values as true or false When using numbers: 0 is false other numbers are true if money: print “Thank you!” else: print “I am sorry there are no seats available.”

Intentional Infinity loops There is a place for infinite loops Count = 0 While Ture: count +=1 if count >10: break (breaks loop) if count ==5; continue (jump back to top of loop) print count

Break and Continue Used in any type of loop Should be used sparingly

Using compound conditions Uses logical operators

not Logical Operator Example username = “ “ while not username: username = raw_input(“Username: “) Loop continues to ask for a username until the user enters something

And Logical Operator elif username == “S. Meier” and password ==“civilization”:

Or Logical operator example elif username==“guest” or password ==“guest”: print “Welcome, guest.”