CHAPTER 6: Control Flow Tools (for and while loops)

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

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.
Python for Informatics: Exploring Information
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Chapter 5: Control Structures II (Repetition)
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Fundamentals of Python: From First Programs Through Data Structures
The University of Texas – Pan American
Fundamentals of Python: First Programs
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
For loops in programming Assumes you have seen assignment statements and print statements.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
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.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
INTRO2CS Tirgul 3 1. What we will cover today?  Boolean and logical expressions  Truth values  Conditional statements  while loop  for loop  range.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
More about Iteration Victor Norman CS104. Reading Quiz.
REPETITION CONTROL STRUCTURE
Python Loops and Iteration
Loops and Iteration Chapter 5 Python for Everybody
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.
Repetition-Counter control Loop
While Loops in Python.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Python - Loops and Iteration
Iterations Programming Condition Controlled Loops (WHILE Loop)
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Ruth Anderson UW CSE 160 Spring 2018
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.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Iteration with While You can say that again.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Loop Structures and Booleans Zelle - Chapter 8
Loops CIS 40 – Introduction to Programming in Python
More Looping Structures
Topics Introduction to Repetition Structures
3. Decision Structures Rocky K. C. Chang 19 September 2018
Repetition In today’s lesson we will look at:
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Introduction to Repetition Structures
Logical Operators and While Loops
Chapter 4: Repetition Structures: Looping
Introduction to Computer Science
Repetition Statements (Loops) - 2
COMPUTER PROGRAMMING SKILLS
Another Example Problem
Introduction to Computer Science
Python While Loops.
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.
More Looping Structures
While Loops in Python.
Control flow: Loops UW CSE 160.
Presentation transcript:

CHAPTER 6: Control Flow Tools (for and while loops) COMPUTER PROGRAMMING SKILLS 1439-1440

Outline Overview Loops – Example The range function The in operator For loops while loops Using break Using continue Exercises

Overview Loops are used to execute a block of code several times. A loop statement allows you to specify that an action is to be repeated while some condition remains true. There are different types of loops: for, while. Each of them has its specific uses.

Five-element sequence For loops - Examples The following program will print “Hello” five times: Iteration variable Five-element sequence for i in [1,2,3,4,5] : print('Hello') Hello Indentation

For loops - Examples The program below asks the user for a number and prints its square, then asks for another number and prints its square, etc. It does this three times and then prints that the loop is done for i in [1,2,3] : num = eval(input('Enter a number: ')) print ('The square of your number is', num*num) print('The loop is now done.') Enter a number: 3 The square of your number is 9 Enter a number: 5 The square of your number is 25 Enter a number: 23 The square of your number is 529 The loop is now done.

The range function Example: The value we put in the range function determines how many times we will loop. The way range works is it produces a list of numbers from zero to the value minus one. For instance, range(3) produces five values: 0, 1, and 2. Examples: range( Number) range( begin,end) range( begin,end,step) for i in range(3): print('Python') Python

The in operator The in operator is used with lists. The in operator is also used with strings. For example, it can be used to tell if a string contains something or not. for i in [3,4,5]: print(i) 3 4 5 Name= 'hello' if 'o' in Name: print('Your string contains the letter o.') Name= 'hello' if ';' not in Name: print('Your string doesn’t contain the letter a.') Your string contains the letter o. Your string doesn’t contain the letter a.

For loops For loops are also called "definite loops" because they execute an exact number of times. for i in [0,1,2,3,4]: print(i) print ('----End---') for x in range (5,7): print(x) print ('----End---') Friends =['Ahmad', 'Nawaf', Mohammad'] for x in Friends: print('Hi ', x) print ('----End---') 1 2 3 4 ----End--- 5 6 ----End--- Hi Ahmad Hi Nawaf Hi Mohammad ----End---

While loops Example: No Yes n = 5 print (n) n = n -1 n = 5 print ('End') Yes n = 5 n = 5 while n > 0 : print (n) n = n-1 print ('End') print (n) 5 4 3 2 1 End n = n -1

While loops While loops are called "indefinite loops" because they keep going until a logical condition becomes False. Loops (repeated steps) have iteration variables that change each time through a loop. Often these iteration variables go through a sequence of numbers.

While loops Infinite loop!!! No Yes n = 5 print (n) No Yes n = 5 n=n+1 print ('End') Yes n = 5 print (n) while n > 0 : Infinite loop (No end) 5 … n > 0 ? No print ('End') Yes n = 5 n=n+1 while n > 0 : print (n) Infinite loop (No end) 5 6 … 7 8 print (n)

Using break The break statement ends the current loop and jumps to the statement immediately following the loop. It is like a loop test that can happen anywhere in the body of the loop. X=5 while X>1: X=X-1 if X == 3 : break print (X) print ('End') 4 End

Using continue The continue statement ends the current iteration and jumps to the top of the loop and starts the next iteration. X=5 while X>1: X=X-1 if X == 3 : continue print (X) print ('End') 4 2 1 End

Tips with loops Counting in a Loop To count how many times we execute a loop we introduce a counter variable that starts at 0 and we add one to it each time through the loop. Lp=0 print ('How many loops before? ', Lp) for i in [21,9,7]: Lp = Lp +1 print ('How many loops after? ', Lp) How many loops before? 0 How many loops after? 3

Tips with loops Summing in a Loop To add up a value we encounter in a loop, we introduce a sum variable that starts at 0 and we add the value to the sum each time through the loop. Sum=0 print (' Sum before loop = ', Sum) for i in [21,9,7]: Sum = Sum + i print (' Sum of all elements = ', Sum) Sum before loop = 0 Sum of all elements = 37

Tips with loops Finding the Average in a Loop An average just combines the counting and sum patterns and divides when the loop is done. Count =0 Sum=0 print (' Count before loop = ', Count) print (' Sum before loop = ', Sum) for i in [21,9,7,3,10,50,12]: Count = Count +1 Sum = Sum + i print (' Sum of all elements = ', Sum) print (' Average of all elements = ', Sum/Count) Count before loop = 0 Sum before loop = 0 Sum of all elements = 112 Average of all elements = 16.0

Tips with loops Filtering in a Loop We use an if statement in the loop to catch / filter the values we are looking for. print (' Before ') for value in [21,9,7,3,10,50,12]: if value >20: print (value) print (' After loop ') Before 21 50 After loop

Exercises The code below prints the numbers from 1 to 50. Rewrite the code using a while loop to accomplish the same thing. Write a program that asks the user for their name and how many times to print it. The program should print out the user’s name the specified number of times. A good program will make sure that the data its users enter is valid. Write a program that asks the user for a weight and converts it from kilograms to pounds. Whenever the user enters a weight below 0, the program should tell them that their entry is invalid and then ask them again to enter a weight. [Hint: Use a while loop, not an if statement]. for i in range(1,51): print (i)

Exercises Write a program that asks the user to enter his name and a character. The program should print out the user’s name and specify how many times the character occur in his name.